1、cv2.findContours()
To find the outline of the white area in a binary image with a black background, the image that has been grayscaled by cvtColor() and binarized by threshold() is generally used as input.
cv2. findContous( image, mode, method[ , contours[ , hierarchy[ , offset] ] ] )
''
1 ) image: 原始输入图像,为8bit的单通道二值图像
2 ) mode: 轮廓检索模式
cv2. RETR_EXTERNAL: 只检索外部轮廓
cv2. RETR_LIST: 检索所有轮廓,但不建立任何层次关系( 即父轮廓和子轮廓)
cv2. RETR_CCOMP: 检索所有轮廓,将其组织为2 层,top层为各部分的外层轮廓,第二层为内层轮廓
cv2. RETR_TREE: 检索所有轮廓,并建立嵌套轮廓的层次结构
3 ) method: 轮廓近似方法
cv2. CHAIN_APPROX_NONE: 输出轮廓的每个像素点
cv2. CHAIN_APPROX_SIMPLE: 只保留水平/ 垂直/ 斜线的端点
返回值为:
4 ) contours: 检测到的轮廓,为列表list 格式:list [ ndarray]
该列表的长度为L,表示有L个轮廓(顺序为先外层后内层进行排序)
ndarray的格式为( K, 1 , 2 ) , 表示各个轮廓中的K个点的位置( x, y)
5 ) hierarchy: 轮廓的层次结构,为( 1 , L, 4 ) 的ndarray
hierarchy[ 0 ] [ i] 表示的是第i个轮廓的层次结构:[ Next, Previous, First Child, First Parent] , 表示
第i个轮廓的同层的后一个轮廓编号、同层的前一个轮廓编号、第一个子轮廓、第一个父轮廓的编号
编号指的是轮廓在contours列表的序号,不存在则置为- 1
''
For example, the following contour detection results contours and hierarchy.
2、cv2.boundingRect()
Detect the bounding rectangle of the corresponding contour based on the contour points
cv2. boundingRect( points)
''
points: 为2D的像素点集合,即( K, 1 , 2 ) 的ndarray, 一般为cv2. findContours的检测出的轮廓
输出为( x, y, w, h) ,即左上角的点+ 矩形的宽高
''