[OpenCV; Python] Summary of findcontours function

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.

findcontours function

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.

Difference in contour extraction of black and white blobs

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.

Black blob contour

The outline of the black blob seems to extract the vertices of the outermost matrix of the blob counterclockwise. image

White blob contour

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. image

Priority

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.

What I noticed

--The outermost matrix of the image is not extracted as an outline. image 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.

Extraction mode

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. image

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. image

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". image

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". image

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

[OpenCV; Python] Summary of findcontours function
Python function argument summary
Summary of Python arguments
Summary of python file operations
Summary of Python3 list operations
[python] Value of function object (?)
[Python] Etymology of python function names
Environment construction of python and opencv
Python Summary
python function ①
[Python] function
A brief summary of Python collections
Python summary
Summary of Python indexes and slices
python function ②
Basic study of OpenCV with Python
[Python] Summary of how to use pandas
[Python] Summary of array generation (initialization) time! !! !!
[Python2.7] Summary of how to use unittest
Summary of built-in methods in Python list
Summary of how to use Python list
Easy introduction of python3 series and OpenCV3
Axis option specification summary of Python "numpy.sum (...)"
Introduction of Python
python enumerate function
Python2.7 + CentOS7 + OpenCV3
Python tutorial summary
Python> function> Closure
[Python] Generator function
Basics of Python ①
Basics of python ①
Copy of python
OpenCV Samples (Python)
[Note] openCV + python
Python> function> Inner function
python related summary
Python function decorator
Python basics summary
Draw a graph of a quadratic function in Python
Correspondence summary of array operation of ruby and python
Example of taking Python> function> * args as arguments
Performance comparison of face detector with Python + OpenCV
Get the caller of a function in Python
Summary of how to use MNIST in Python
Installation of Python3 and Flask [Environment construction summary]
[Python] Summary of S3 file operations with boto3
Summary of frequently used Python arrays (for myself)
Summary of studying Python to use AWS Lambda
I / O related summary of python and fortran
[Python + OpenCV] Whiten the transparent part of the image
Summary of Excel operations using OpenPyXL in Python
[Python] Operation of enumerate
List of python modules
Summary about Python scraping
[Language processing 100 knocks 2020] Summary of answer examples by Python
About function arguments (python)
python openCV installation (memo)
Python Django tutorial summary
Summary of tools needed to analyze data in Python
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Function execution time (Python)