[Reprinted] Introduction to the outline of the OpenCV-Python series (27)

This time we will discuss the contours in OpenCV. Contours are a very important part of OpenCV. At the same time, the Canny edge detection we have mentioned before will serve as the basis.

Contour detection

Function prototype:

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

Parameter meaning:
image represents the input picture. Note that the input picture must be a binary picture. If the input picture is a color picture, it must be grayscale and binarized first.

mode represents the retrieval mode of the contour, there are 4 types:

cv2.RETR_EXTERNAL means that only the outer contour is detected.

The contour detected by cv2.RETR_LIST does not establish a hierarchical relationship.

cv2.RETR_CCOMP establishes two levels of contours, the upper layer is the outer boundary, and the inner layer is the boundary information 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 builds the outline of a hierarchical tree structure.

Method is the approximate method of contour, there are 4 kinds:

cv2.CHAIN_APPROX_NONE stores all contour points, and the pixel position difference of two adjacent points does not exceed 1, that is, max(abs(x1-x2), abs(y2-y1))<=1.

cv2.CHAIN_APPROX_SIMPLE compresses elements in the horizontal, vertical, and diagonal directions, and only retains the end point coordinates in that direction. For example, a rectangular contour only needs 4 points to save the contour information.

cv2.CHAIN_APPROX_TC89_L1 and cv2.CHAIN_APPROX_TC89_KCOS use the teh-Chinl chain approximation algorithm.

return value:

The cv2.findContours() function returns two values, one is the contours of the contour itself, and the other is the attribute hierarchy corresponding to each contour.

Normally, we choose the parameter cv2.CHAIN_APPROX_SIMPLE, because we only need the simplest contour point information.

Contour drawing

Now we look at another function:

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

The first parameter is to specify which image to draw the contour on; image can only display the contour with three channels

The second parameter is the contour itself, which is a list in Python;

The third parameter specifies which contour in the contour list to draw, if it is -1, draw all contours in it. The following parameters are very simple. Thickness indicates the width of the contour line, if it is -1 (cv2.FILLED), it is the filling mode.

Next we take this picture as an example:
Insert picture description here

Now we come to the actual code combat:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("contours.png")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
contour = cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]  
cv2.drawContours(img,contour,-1,(0,0,255),2)  
cv2.imshow("res",img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

We can also change the parameters:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("contours.png")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
contour = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0]  
cv2.drawContours(img,contour,-1,(0,0,255),2)  
cv2.imshow("res",img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Change RETR_EXTERNAL to RETR_TREE to detect all contours (previously to detect external contours):
Insert picture description here

Now we can also select the contour, we need to modify the parameters of drawContours, the code:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("contours.png")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
contour = cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]  
cv2.drawContours(img,contour,1,(0,0,255),2)  
cv2.imshow("res",img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

By modifying the third parameter, we can customize which contour to select, which is very convenient for our other operations later.

Similarly, we can also calculate several contours. Apply Numpy. Let's look at the code:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("contours.png")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
contour = cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]  
print(np.array(contour).shape)  
cv2.drawContours(img,contour,1,(0,0,255),2)  
cv2.imshow("res",img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

The following output column will print the number of contours, change the parameters and try again:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("contours.png")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
contour = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0]  
print(np.array(contour).shape)  
cv2.drawContours(img,contour,1,(0,0,255),2)  
cv2.imshow("res",img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

This tutorial has made some basic introduction to contours, next time we will introduce some other content about contours.

Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/108572914