opencv
  1. opencv-contours

Contours - ( Drawing and Image Processing in OpenCV )

Heading h2

Syntax

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

Example

import cv2 as cv
import numpy as np

image = cv.imread('image.jpg')
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)

contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

Output

The contours and hierarchy variables contain the contours detected in the image and the hierarchy information, respectively.

Explanation

Contours are a useful tool in image processing for object detection and recognition. In OpenCV, contours can be detected using the cv.findContours() function, which takes in a binary image, and returns a list of contours and the hierarchy of the contours.

In the above example, we read an image and convert it to grayscale. We then apply a binary threshold to the image to create a binary image. Finally, we detect contours in the binary image using the cv.findContours() function with cv.RETR_TREE retrieval mode and cv.CHAIN_APPROX_SIMPLE approximation method.

Use

Contours can be used in image processing for object detection and recognition, shape analysis, pattern recognition, and many other applications. The cv.findContours() function is a powerful tool in OpenCV for detecting contours in an image.

Important Points

  • Contours are useful in image processing for various applications such as object detection and recognition, shape analysis, pattern recognition, etc.
  • The cv.findContours() function detects and returns the contours in an image
  • It takes in a binary image and returns a list of contours and the hierarchy of the contours
  • The retrieval mode and approximation method are important parameters in the cv.findContours() function

Summary

In conclusion, contours are a powerful tool in image processing for object detection and recognition, shape analysis, pattern recognition, and other applications. OpenCV provides the cv.findContours() function for detecting contours in an image, which takes in a binary image and returns the contours and hierarchy information. The cv.findContours() function is a versatile tool that can be used in a variety of image processing applications.

Published on: