标签:建立 direct vector dice poi sample nes canny header
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()
标签:建立 direct vector dice poi sample nes canny header
原文地址:https://www.cnblogs.com/shiqi17/p/12169710.html