Contour Finding and Drawing

Contour Finding and Drawing

1) What is a contour

The contour can be simply thought of as a curve connecting continuous points (connecting the boundary), with the same color or grayscale, and extracting the contour is to extract

These curves with the same color or grayscale, or connected domains, contours are very useful in shape analysis and object detection and recognition.
Precautions:

①In order to be more accurate, a binarized image should be used. Before looking for contours, thresholding or Canny boundary detection is performed

②The function of finding the contour will modify the original image. If you still want to use the original image after finding the contours, you should store the original image in other variable

in img.copy()

③In OpenCV, finding contours is like finding white objects in a black background. You should remember that the object you are looking for should be white and the background should be black.

Commonly used functions: cv2.findContours()-----find the contour cv2.drawContours()-----draw the contour

insert image description here
Find contours—cv2.findContours()
insert image description here
⚫image: input image, 8-bit single-channel image (usually a binary image)
⚫contours: detected contours, each contour is stored as a point vector, which is the vector representation of Point
type⚫ hierarchy: Optional output vector containing topological information of the image. As a representation of the number of contours, it contains many elements. Each contour contours[i] corresponds to 4 hierarchy elements hierarchy[i][0]~hierarchy[i][3], which respectively represent the next contour, the previous contour, The index number of the parent contour and the embedded contour. If there is no corresponding item, set it to a negative
number ⚫mode: Contour retrieval mode, the value is as follows:
cv2.RETR_EXTERNAL=0-----Indicates that only the outermost contour is detected cv2.RETR_LIST= 1------Extract all contours and place them in the list, contours do not establish a hierarchical relationship cv2.RETR_CCOMP=2------Extract all contours and organize them into a double-layer structure cv2.RETR_TREE =3---- --Extract all contours and re-establish the mesh contour structure
⚫method: Approximation method of the contour, see Figure 2 for values
​​⚫offset: optional offset of each contour, default value Point()

insert image description here
CV_CHAIN ​​APPROX NONE Store all contour points continuously, any two adjacent points are horizontal, vertical or oblique. That is to say

max(bs(x1-x2),ab (y2-y1))==1.

·CV CHAIN ​​APPROXSIMPLE compressed storage, for horizontal, vertical or oblique line segments, only endpoints are saved. For example, for a quadrilateral, only

store four vertices.

CHAIN ​​APPROXTC89L1, CV_CHAIN ​​QPPROXTC89KCOS use one of the Teh-Chin chain approximation algorithms

LINK RUNS is completely different from the above algorithm, linking all the contours of the horizontal hierarchy.

contours, hierarchy = cv2.findContours (mask, CV2.RETR EXTERNAL, CV2.CHAIN APPROX SIMPIE)

thresh,contours,hierarchy = cv2.findContours(thresh, CV2.RETR TREE,CV2.CHAIN APPROX SIMPIE)

Draw contours—cv2.drawContours()

insert image description here

⚫image: 目标图像, Mat类型对象即可
⚫contours: 所有的输入轮廓, 每个轮廓存储为一个点向量
⚫contourIdx: 轮廓绘制指示变量(索引), 若为负值, 则表示绘制所有轮廓
⚫color: 绘制轮廓的颜色
⚫thickness: 轮廓线条的粗细, 默认值1, 如果为负值, 则绘制轮廓内部, 可选宏 CV_FILLED
⚫lineType: 线条类型, 默认值8
⚫hierarcy: 可选的层次结构信息, 默认值noArray()
⚫maxLevel: 表示用于绘制轮廓的最大等级, 默认值INT_MAX
⚫offset: 可选的轮廓偏移参数, 默认值Point()
cv2.drawContours(img1, contours,-1, (0,0,255)2)

insert image description here

2) How to access each contour

for i in range (0,len (contours)) :
  #cv2.drawContours (img,contours[i],-1,(0,255,0) ,5)
   cv2 .drawContours (img,contours ,i,(0,255,0) ,5)
for cnt in contours:
    cv2.drawContours (img,cnt,-1, (0,255,0) ,5)

3) How to access all points of each contour

for i in range (0,len (contours)) :
    for j in range (0,len (contours[il)) :
       cv2.drawContours(img,contours[il,j,(0,255,0) ,3)
for cnt in contours:
    for j in range(0,len (cnt)) :
         cv2 .drawContours (img,cnt,j,(0,255,0) ,3)

len(contours)------the number of all contours
len(contours[i])------the number of all points of the i-th contour
insert image description here

4) Contour storage structure

mode: Contour retrieval mode, the values ​​are as follows CV2.RETR EXTERNAL=0-----indicates that only the outermost contour is detected
Cv2.RETR LIST=1------Extract all contours and place them in the list, the contours are not Establish a hierarchical relationship
cv2.RETR CCOMP=2------Extract all contours and organize them into a double-layer structure
CV2.RETR TREE =3------Extract all contours and re-establish a mesh contour structure

Guess you like

Origin blog.csdn.net/weixin_40911806/article/details/130053518