绘制轮廓 cv2.findContours函数及参数解释

cv2.findContours()

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

注意事项

1.输入为二值图像,黑色为背景,白色为目标

2.该函数会修改原图像,因此若想保留原图像在,则需拷贝一份,在拷贝图里修改。

mode参数

参数名称功能
cv2.RETR_EXTERNAL只检测外轮廓
cv2.RETR_LIST检测的轮廓不建立等级关系,都是同级
cv2.RETR_CCOMP建立两个等级的轮廓,上面一层为外边界,里面一层为内孔的边界信息
cv2.RETR_TREE建立一个等级树结构的轮廓

method参数

参数名称功能
cv2.CHAIN_APPROX_NONE存储所有边界点
cv2.CHAIN_APPROX_SIMPLE压缩垂直、水平、对角方向,只保留端点
cv2.CHAIN_APPROX_TX89_L1使用teh-Chini近似算法
cv2.CHAIN_APPROX_TC89_KCOS使用teh-Chini近似算法

offset:(可选参数)

offset:轮廓点的偏移量,格式为tuple,如(-10,10)表示轮廓点沿X负方向偏移10个像素点,沿Y正方向偏移10个像素点

返回值

contours:轮廓点。列表格式,每一个元素为一个3维数组(其形状为(n,1,2),其中n表示轮廓点个数,2表示像素点坐标),表示一个轮廓

hierarchy:轮廓间的层次关系,为三维数组,形状为(1,n,4),其中n表示轮廓总个数,4指的是用4个数表示各轮廓间的相互关系。第一个数表示同级轮廓的下一个轮廓编号,第二个数表示同级轮廓的上一个轮廓的编号,第三个数表示该轮廓下一级轮廓的编号,第四个数表示该轮廓的上一级轮廓的编号。

# ## -*- coding: utf-8 -*-
import cv2
import imutils
import numpy as np

def RGB_GRAY(img):
    img = cv2.resize(img, (640, 480))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    return img, gray

def edge(img):
    #  cv2.Canny(source_image,thresholdValue 1,thresholdValue 2)
    edged = cv2.Canny(img, 30, 200)
    cv2.imshow('img', edged)
    cv2.waitKey(0)
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE,
                                cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    return contours


def solve(img):
    img, gray = RGB_GRAY(img)
    contours = edge(gray)


if __name__ == '__main__':
    img_path = './22222.jpg'
    img = cv2.imread(img_path)
    solve(img)

  • 7
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滴滴滴'cv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值