I couldn't find a detailed explanation of the Python version of OpenCV findcontours on the net, so I'd like to note that I tried various things myself. If you make a mistake, please point it out.
If you want to extract the outline of an image with OpenCV, use a function called findContours. The input image is used as the first argument, the extraction mode is used as the second argument, and the approximation method is used as the third argument.
findcontours function
image, contours, hierarchy = cv2.findContours(Input image,Extraction mode,Approximation method)
This article does not explain the approximation method. We use the approximation method `` `cv2.CHAIN_APPROX_SIMPLE``` in a unified way.
A blob means "lump" and refers to a mass of black or white pixels. In contour extraction, it seems that the method of extracting the contour differs between the contour of the white blob and the contour of the black blob, so it will be explained below.
The outline of the black blob seems to extract the vertices of the outermost matrix of the blob counterclockwise.
The outline of the white blob seems to extract the outside of the outermost matrix of the blob clockwise as shown in the figure below.
When detecting contours from the outside, black is detected first. So, if you have a black blob on a white background, the black blob will be detected first, but if you have a black background, the black background will be detected first. Caution is required.
--The outermost matrix of the image is not extracted as an outline. The outermost matrix of the image (0 rows, 9 rows, 0 columns, 9 columns in the image above) was not extracted as an outline. In the image above, for example, when contour extraction is performed in the extraction mode `` `RETR_EXTERNAL```, the output is as follows.
output
[array([[[1, 1]],
[[1, 8]],
[[8, 8]],
[[8, 1]]])]
--Blob contour detection order
The order in which the contours of the blobs are detected seems to be in descending order of the y value of the coordinates of the start point of contour extraction. If the y values are equal, the order of the larger x values is earlier.
There are at least four extraction modes: RETR_EXTERNAL
, RETR_LIST
, RETR_CCOMP
, and RETR_TREE
.
RETR_EXTERNAL RETR_EXTERNAL is a mode that extracts only the outermost contour of the contour. Even if there is a contour inside the contour, it will not be extracted. Therefore, the outline of the white blob is not detected.
output
[array([[[7, 7]],
[[7, 8]],
[[8, 8]],
[[8, 7]]]), array([[[1, 1]],
[[1, 5]],
[[5, 5]],
[[5, 1]]])]
RETR_TREE RETR_TREE is a perfect representation of nested contours. In other words, first, the black blobs are detected in the order explained in "What I noticed", and when a nested blob is found, the outline of the blobs in that blob is extracted.
output
[array([[[7, 7]],
[[7, 8]],
[[8, 8]],
[[8, 7]]]), array([[[1, 1]],
[[1, 5]],
[[5, 5]],
[[5, 1]]]), array([[[1, 2]],
[[2, 1]],
[[4, 1]],
[[5, 2]],
[[5, 4]],
[[4, 5]],
[[2, 5]],
[[1, 4]]]), array([[[3, 3]]])]
RETR_LIST RETR_LIST gets contours in the same hierarchy regardless of white contours, black contours, inside or outside. Therefore, the order of acquiring contours is the order explained in "・ What I noticed".
output
[array([[[7, 7]],
[[7, 8]],
[[8, 8]],
[[8, 7]]]), array([[[3, 3]]]), array([[[1, 2]],
[[2, 1]],
[[4, 1]],
[[5, 2]],
[[5, 4]],
[[4, 5]],
[[2, 5]],
[[1, 4]]]), array([[[1, 1]],
[[1, 5]],
[[5, 5]],
[[5, 1]]])]
RETR_CCOMP RETR_CCOMP first extracts all the outlines of the black blobs and then extracts the outlines of the white blobs. The order will be the order explained in "・ What I noticed".
output
[array([[[7, 7]],
[[7, 8]],
[[8, 8]],
[[8, 7]]]), array([[[3, 3]]]), array([[[1, 1]],
[[1, 5]],
[[5, 5]],
[[5, 1]]]), array([[[1, 2]],
[[2, 1]],
[[4, 1]],
[[5, 2]],
[[5, 4]],
[[4, 5]],
[[2, 5]],
[[1, 4]]])]
Recommended Posts