Opencv-python 找到图像轮廓并绘制,cv2.findContours()函数,求轮廓外接矩形,cv2.boundingrect()

Posted c_G-17

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Opencv-python 找到图像轮廓并绘制,cv2.findContours()函数,求轮廓外接矩形,cv2.boundingrect()相关的知识,希望对你有一定的参考价值。

一、查找图像轮廓
  • opencv-python中查找图像轮廓的API为:findContours函数
    该函数接受二值图作为参数,根据参数,可查找物体外轮廓、内外轮廓,保存轮廓点、压缩等等...

  • 如:contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

def findContours(image, mode, method, contours=None, hierarchy=None, offset=None): # real signature unknown; restored from __doc__
    """
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
    .   @brief Finds contours in a binary image.
    .   
    .   The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours
    .   are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the
    .   OpenCV sample directory.
    .   @note Since opencv 3.2 source image is not modified by this function.
    .   
    .   @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero
    .   pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold ,
    .   #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one.
    .   If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
    .   @param contours Detected contours. Each contour is stored as a vector of points (e.g.
    .   std::vector<std::vector<cv::Point> >).
    .   @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has
    .   as many elements as the number of contours. For each i-th contour contours[i], the elements
    .   hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices
    .   in contours of the next and previous contours at the same hierarchical level, the first child
    .   contour and the parent contour, respectively. If for the contour i there are no next, previous,
    .   parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
    .   @param mode Contour retrieval mode, see #RetrievalModes
    .   @param method Contour approximation method, see #ContourApproximationModes
    .   @param offset Optional offset by which every contour point is shifted. This is useful if the
    .   contours are extracted from the image ROI and then they should be analyzed in the whole image
    .   context.
    """
    pass
参数 作用
cv2.RETR_EXTERNAL 只查找外轮廓
cv2.RETR_LIST 检测所有轮廓,保存到一个arry(链表)
cv2.RETR_CCOMP 建立两个等级的轮廓(外/内),只组织两层
cv2.RETR_TREE 检测所有轮廓,重构嵌套轮廓全部层次
cv2.CHAIN_APPROX_NONE 存储所有边界点
cv2.CHAIN_APPROX_SIMPLE 压缩垂直、水平、对角方向,只保留端点
cv2.CHAIN_APPROX_TX89_L1 使用teh-Chini近似算法
cv2.CHAIN_APPROX_TC89_KCOS 使用teh-Chini近似算法

cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset ]]]]])
第一个参数是指明在哪幅图像上绘制轮廓;
第二个参数是轮廓本身,在Python中是一个list。
第三个参数指定绘制轮廓list中的哪条轮廓,如果是-1,则绘制其中的所有轮廓。后面的参数很简单。其中thickness表明轮廓线的宽度,如果是-1(cv2.FILLED),则为填充模式

参考文章

二、外接矩形

找到轮廓

contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

contours多维数组,包含多个轮廓cnt

x, y, w, h = cv2.boudingrect(cnt) # 获得外接矩形

参数说明:x,y, w, h 分别表示外接矩形的x轴和y轴的坐标,以及矩形的宽和高, cnt表示输入的轮廓值

以上是关于Opencv-python 找到图像轮廓并绘制,cv2.findContours()函数,求轮廓外接矩形,cv2.boundingrect()的主要内容,如果未能解决你的问题,请参考以下文章

[OpenCV-Python] OpenCV 中的图像处理 部分 IV (四)

OpenCV-Python实战(11)——OpenCV轮廓检测相关应用

绘制 OpenCV 轮廓并保存为透明图像

opencv-python基础用法详细代码-图片加载-ROI-边缘滤波-二值化-轮廓提取-膨胀腐蚀等

OpenCV-Python17.OpenCV的图像轮廓——轮廓特征

3. OpenCV-Python——图像梯度算法边缘检测图像金字塔与轮廓检测直方图与傅里叶变换