OpenCV has findContour ()
which helps in extracting contours from an image. It works best with binary images, so we must first apply thresholding, Sobel edges, etc.
Below is the code to find outlines —
|
Output:
We can see that there are three essential arguments in the cv2.findContours ()
function. The first — original image, second — contour search mode, third — a contour approximation method that outputs the image, contours, and hierarchy. & # 39; contours & # 39; — this is a Python list of all the contours of the image. Each individual contour is a Numpy (x, y) array of coordinates of the object’s boundary points.
Path approximation method —
Above we can see that the paths are boundaries shapes with the same intensity. It stores the (x, y) coordinates of the shape’s border. But does it store all coordinates? This is set by this contour approximation method.
If we pass in cv2.CHAIN_APPROX_NONE
, all endpoints are preserved. But do we really need all the items? For example, if we have to find the outline of a straight line. We only need the two endpoints of this line. This is what cv2.CHAIN_APPROX_SIMPLE
does. It removes all unnecessary points and shrinks the path, thereby saving memory.