opencv contour related function

    • Find contours

contours, hierarchy = cv2.findContours(image,mode,method)

parameter:

  • image: The image used to find contours. (The search process does not modify this image)

  • mode: mode.

  • cv2.RETR_EXTERNAL only detects the outer contour;

  • The contours detected by cv2.RETR_LIST do not establish a hierarchical relationship;

  • cv2.RETR_CCOMP creates two levels of contours, the upper layer is the outer boundary, and the inner layer is the boundary of the inner hole. If there is a connected object in the inner hole, the boundary of this object is also on the top layer;

  • cv2.RETR_TREE creates an outline of a hierarchical tree structure.

  • method: Contour approximation method.

  • cv2.CHAIN_APPROX_NONE stores all contour points, and the maximum difference between the pixel coordinates xy of two adjacent points is 1;

  • cv2.CHAIN_APPROX_SIMPLE For the same straight line (horizontal, vertical and diagonal segments), only the endpoint coordinates are retained. For example, a rectangular outline only requires 4 points to save outline information;

  • cv2.CHAIN_APPROX_TC89_L1 I don’t quite understand

  • cv2.CV_CHAIN_APPROX_TC89_KCOS I don’t quite understand

return value:

  • contours: Contour information stored in the form of point sets , vector array, a list in python

  • hierarchy: [Output optional], it is a vector array, in python it is a list. Contains information about the image topology. It has as many elements as there are silhouettes. For each i-th contour contour[i], the elements hierarchy[i][0], hierarchy[i][1], hierarchy[i][2] and hierarchy[i][3] Indexes set to 0 in the contours of the next and previous contours respectively at the same hierarchy level (first child contour and parent contour). If there is no next, previous, parent or nested contour for contour i, the corresponding element of hierarchy[i] will be negative.

Quick to use

contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

2. Draw the outline

cv2.drawContours(image, contours, contourIdx, color, thickness, lineType, hierarchy, maxLevel, offset)

parameter:

  • image: target image for drawing outlines

  • contours: the contours to be drawn (data type is list)

  • contourIdx: Which contour in the contour list is drawn, -1 means drawing all.

  • color: The color of the drawn line. The data format is bgr tuple. Such as blue (255, 0, 0)

  • thicknessLine thickness (integer)

  • linetype: Line type. Generally not used. cv2.LINE_AA is anti-aliasing line

  • hierarchy: [optional] information about the hierarchy. It is only needed if you only want to draw certain contours

  • maxLevel: The maximum elevation for drawing the outline. If 0, only the specified outline is drawn. If 1, the function draws the contour and all nested contours. If 2, the function draws contours, all nested contours, all nested-to-nested contours, etc. This parameter will only be considered if a hierarchy is available .

  • offset: [optional] outline offset parameter. Moves all drawn contours by the specified contour

No return value.

Quick to use

cv2.drawContours(img, contours, -1, (255,255,0), 2)
Sometimes a for loop is used to split the contours returned by cv2.findContours() into individual contours. Before and after splitting, they are list point sets, so they can be fed into this function for drawing.

3. Contour circumscribed rectangle

Get the circumscribed square

x,y,w,h = cv2.boundingRect(contours)    # 传入轮廓list
# 返回值为坐标和宽高

Get the bounding rectangle

rect = cv.minAreaRect(contours)    # 传入轮廓list
# 返回旋转矩形
# 返回值rect包含重心坐标(x, y), 宽高(width, height), 旋转角度
# 前两个为元组,最后旋转角度为浮点

Find the four points of a rotated rectangle

box = cv2.boxPoints(rect)  # 查找旋转矩形的四个点。传入rect为旋转矩形
# 返回值box为包含四个点的list,浮点型

Returned box point set, find corner point example

# 获取四个顶点坐标
left_point_x = np.min(box[:, 0])    # 左为第几行,右为第几列
right_point_x = np.max(box[:, 0])
top_point_y = np.min(box[:, 1])
bottom_point_y = np.max(box[:, 1])

Quick application

rect = cv2.minAreaRect(contours)
center, size, rotation = rect    # 可拆开
box = cv2.boxPoints(rect)  # 查找旋转矩形的四个点。
box = np.int0(box)    # 浮点转为整型
cv2.drawContours(img,[box],0,(0,0,255),2)

The difference between the two: the green line represents the circumscribed square, and the red line represents the circumscribed rectangle.

4.Contour approximation

approx = cv2.approxPolyDP(contours,epsilon, closed)
# 返回值approx与contours类型一样
# 传入参数:1.轮廓点集;2.轮廓近似精度(double);3.所近似出的轮廓线是否闭合(bool)

Example: (the original picture is on the far left)

epsilon = 0.1*cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,epsilon,True)

Below, in the second image, the green line shows the approximate curve for accuracy epsilon = 10%. The third graph shows the situation when accuracy epsilon = 1%.

Guess you like

Origin blog.csdn.net/qq_35858902/article/details/128832358