Contour Analysis in OpenCV

Learn via video courses
Topics Covered

Overview

Contour analysis is a strong computer vision technique for object detection, recognition, and tracking. It permits vital information about objects and their properties to be extracted from an image's shape, size, and position. It can be used in conjunction with other approaches to improve accuracy and robustness, such as machine learning. Contour analysis has numerous uses in robotics, surveillance, and medical imaging.

Introduction

Contour analysis is a widely-used technique in OpenCV for recognizing and retrieving the boundaries of objects in an image or video stream. Contours are a collection of connected points that create an object's boundary and can be used to compute attributes such as area, perimeter, and centroid. With its many applications, including object detection, tracking, and segmentation, contour analysis is a fundamental tool in computer vision.

OpenCV provides a range of contour analysis functions such as findContours to extract contours from an image and drawContours to visualize the contours. By integrating additional techniques such as image filtering, thresholding, and machine learning, contour analysis can significantly improve the accuracy and resilience of object detection and recognition systems.

Contour Detection in OpenCV

Edge Detection Methods

In computer vision, edge detection is a significant task. The task of identifying borders across regions with various properties, such as brightness or texture, can be characterized as it.

The method of locating edges in an image is known as edge detection. An edge is often a sharp transition from one color's pixel value to another, such as black to white.

Binary images are utilized to improve accuracy. Thus apply threshold or canny edge detection before looking for contours.

Canny Edge Detection Method

Syntax of canny edge detection method

The canny edge detector is a multi-stage method that detects image edges. It is one of the most used edge detection techniques, not only because it is simple, but also because it produces high-quality results.

Using the Canny algorithm, the function discovers edges in the input image and marks them in the output map edges. For edge linking, the least value between thresholds 1 and 2 is chosen. The highest value is used to locate the first segments of strong edges.

The Canny edge detection algorithm's approach can be divided into five steps:

  1. Smooth the image with a Gaussian filter to remove the noise.
  2. Determine the image's intensity gradients.
  3. To eliminate false responses to edge detection, use gradient magnitude thresholding or lower bound cut-off suppression.
  4. Use a twofold(double) threshold to identify probable edges.
  5. Hysteresis track edge: Finish edge detection by suppressing all other weak edges that are not connected to strong edges.

Implementation of Canny Edge Detection algorithm:

original image

blurred image

canny edge detection image

Contour Detection Algorithms

To perform contour detection OpenCV has a function called FindContours that searches for contours in an image. Of course, some treatment needs to be given to the image to detect shapes accurately. To release contours, we first use the MorphologyEx function using the CV_MORPH_OPENand CV_MORPH_CLOSE methods. Finally, despite working on a grayscale version of the image, we use the FindContours function to detect contours and print them on the color image

cv2.MORPH OPEN: Using an opening operation, we can remove small blobs from an image: first, an erosion is used to eliminate the small blobs, then a dilation is used to regenerate the original object's size. The actual opening method is carried out by calling the cv2.morphologyEx function.

cv2.MORPH CLOSE: We'll call cv2.morphologyEx again to do the closure operation, but this time we'll specify the cv2.MORPH CLOSE flag to indicate that our morphological operation is closing.

output-1

output-2

contour detection in original image

Contour detection after the morphological operation(opening/closing)

contour detection

contour detection

Thresholding Techniques

To perform thresholding, we will utilize the function cv2.threshold(). The function accepts a grayscale image as input, does fixed-level thresholding, and produces a binary image. In this scenario, all pixel values less than 100 are set to 0 (black), while those greater than 100 are set to 255. (white). Because the image has already been inverted, cv2.THRESH BINARY is used; however, cv2.THRESH BINARY INV should be used if the image has not yet been inverted. Also, the fixed level must be capable of accurately segmenting the target object as foreground (white).

Contour Analysis Techniques

The fundamentals of contour detection and how to draw the contour drawing have been covered so far in our Contour Detection part. In this part of the article, we will learn about contour analysis. This is critical because contour analysis allows you to recognize the object being detected and distinguish one contour from another. We'll also look at how to detect distinct aspects of contours to get important information.

Let's explore in depth how to identify the many contour characteristics, including area, perimeter, centroid, bounding box, and many other features and functions.

Contour Features/Properties

Image Moments

Image moments resemble the image's weighted average of pixel intensities. They assist in calculating properties such as an object's area and center of mass, among others. The cv.moments() function returns a dictionary containing all calculated moment values.

Syntax:

M = cv.moments(array)

Parameter:

array – Single-channel, 8-bit, or floating-point 2D array

Return:

M – A Python dictionary containing different moments properties

OUTPUT

The values that are returned indicate many types of image movements, such as central moments, scale- and rotation-invariant moments, and so forth.

Finding Centre of a Contour

Let's begin by using the image moments of the contour to get the centroid of the object in the image. The central image moments' Cx=M10/M00Cx=M10/M00 and Cy=M01/M00Cy=M01/M00 relations provide the X and Y coordinates of the centroid.

OUTPUT

To identify the centroids on the image, let's now use cv2.circle() to draw a circle.

OUTPUT

input image

Centroids on the images

output image

Finding Contour Area

OUTPUT

The area can also be located by utilizing the contour's m00 instant, which contains the area of the contour.

OUTPUT

Finding Contour Perimeter

It is also known as the arc length. The cv.arcLength() method can be used to determine it. When passed True, the second argument specifies whether the shape is a closed contour or just a curve.

OUTPUT

Contour Approximation

Depending on the level of precision we choose, it approximates a contour form to another shape with fewer vertices. It is a Ramer-Douglas-Peucker (RDP) algorithm implementation. By lowering the number of vertices on a polyline while keeping the majority of its shape, this technique seeks to simplify the polyline given a threshold value.

Simplifying a piecewise linear curve with the Douglas–Peucker algorithm

douglas peucker algorithm

Implementing Contour Approximation with OpenCV

Syntax

epsilon = 0.1*cv.arcLength(cnt,True)

approx = cv.approxPolyDP(cnt,epsilon,True)

Epsilon, the second argument, is the greatest distance from the contour to the approximation contour. It is a measurement of accuracy. To get the right output, epsilon must be chosen carefully.

OUTPUT

Original image
original image

Convexity Defects

The function cv.isContourConvex can be used to determine whether a curve is convex or not (). It just returns True or False. Nothing major.

convexity = cv.isContourConvex(cnt)

Convexity defects

convesity defects

Convex Hull will approximate to contour approximation, but it is not (Both may provide the same results in some cases). In this case, a curve is examined for convexity flaws and corrected using the cv.convexHull() method. Convex curves are often those that are always bulged out, or at the very least, flat. Moreover, it is referred to as a convexity defect if the interior is bulged. Check out the hand in the illustration below as an example. The convex hull of the hand is depicted in the red line. The convexity flaws, which are the regional maximum hull shape deviations, are indicated by the double-sided arrow marks.

Syntax

hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]])

If you want to find convexity defects, you need to pass returnPoints = False Convexity defects of a contour can be found using the OpenCV function cv2.convexityDefects(). The output of this algorithm is an array comprising the convexity flaws. Its inputs are the contour and its matching hull indices.

Syntax

convexityDefects = cv2.convexityDefects(contour, convexhull)

The output contains an array where each row contains 4 values [start point, endpoint, farthest point, approximate distance to the farthest point]. Note that the first three values returned are indices of contour.

Applications for Contour Visualization

In image processing, contour analysis is a fundamental method that can be applied to biometrics, object tracking, form analysis, and object identification and recognition. Following are some examples of how contour analysis is used in certain fields specifically:

1. Object detection and recognition: Contour analysis is frequently used to identify and locate objects inside a picture. A computer vision system can distinguish between items and backgrounds in an image and classify them based on their size, shape, and location by examining the contours' shapes and edges. Applications like facial detection, traffic sign recognition, and industrial automation can all benefit from this.

2.Object tracking: Contour analysis is a technique for tracing the movement of objects over time. A computer vision system can identify an object's trajectory and speed by comparing the contours of the object in several frames of a movie. Applications like surveillance, robotics, and sports analysis can all benefit from this.

3.Shape analysis: Contour analysis can be used to determine the shapes of items in an image. A computer vision system may categorize items based on their shape characteristics, such as curvature, aspect ratio, and area, by comparing the contours of various objects to determine if they are similar or distinct. Applications for this include material inspection, medical diagnosis, and quality control.

4.Biometrics: Contour analysis can be used to examine how human features like the hand, ear, or face are shaped. A computer vision system can produce a special template for biometric identification or verification by extracting the contours of these features. Applications like access control, forensic analysis, and monitoring can all benefit from this.

Techniques for Contour Visualization

Drawing Contours on Images

The cv.drawContours function is used to draw the contours. Any shape can be drawn using it as long as you can access its border points. The source image is the first input, followed by the contours, which should be supplied as a Python list, and the index of the contours (helpful when drawing individual contours). Pass -1; the remaining arguments include color, thickness, etc., to draw all outlines. To show an image's entire contour range:

Syntax:

cv.drawContours(img, contours, -1, (0,255,0), 3)

To draw a certain contour, such as the fourth contour:

Syntax:

cv.drawContours(img, contours, 3, (0,255,0), 3)

(or)

cnt = contours[4]

cv.drawContours(img, [cnt], 0, (0,255,0), 3)

Example:

input image

threshold image

image contours

Drawing Bounding Boxes and Circles Around Contours

cv::boundingRect(InputArray points)

The function determines the smallest up-right bounding rectangle for the given point set and returns it.

cv::minEnclosingCircle(InputArray points,Point2f & center,float & radius )

Using an iterative technique, the function determines the smallest enclosing circle for a 2D point set.

Example:

bounding boxes and circles around contours

Displaying Contour Hierarchies

Typically, the cv.findContours() function is used to find objects in an image. At times, things might be found in multiple places. But occasionally, certain shapes are contained by other shapes. similar to nested figures. Thus, we refer to the outer one as the parent and the inner one as the child. In this manner, there is some link between contours in an image. Moreover, we can describe the relationship between each contour, such as whether it is a parent or child of another contour. The Hierarchy is a representation of this relationship.

RETR_LIST: It merely obtains all of the contours.

RETR_CCOMP: This flag locates every contour and sets it up in a two-level hierarchy.

RETR_TREE: It retrieves all the contours and creates a full family hierarchy list

RETR_EXTERNAL: Just the outermost flags are returned if you use this flag. The contours of every child are gone.

Examples of Contour Analysis in Image Processing

Let's see how Contour Analysis is used in processing medical imaging.

OUTPUT

medical image

Counting Objects in an Image

There are various methods for counting objects in an image by visualizing their contours. The following are some often employed methods:

1.Contour drawing: drawing lines around objects for better visualization and counting.

2.Color labeling: assigning unique colors to segmented objects for easy differentiation and counting.

3.Blob analysis: analyzing object blobs based on size, shape, and intensity for counting.

4. Hough transform: detecting lines and circles in the image for counting objects with distinct edges or circular shapes.

5.Convex hull: finding the smallest convex polygon that encloses objects for counting irregularly shaped or holey objects.

Detecting and Recognizing Faces

The method of contour visualization can be used to find and identify faces. To identify a face, it is necessary to draw contour lines around the features, such as the eyes, nose, and mouth, and then consider their positions and shapes. When conventional facial recognition methods, including employing algorithms to compare facial traits, are ineffective, this technique may be helpful. It can need manual intervention and may not be as accurate as other approaches.

Tracking Objects in a Video Stream

Detecting and tracking things in a video stream using contour visualization entails drawing contour lines around the borders of objects in each frame of the video. Here's a high-level overview of the code for this procedure:

  1. Frame by frame, read the video stream
  2. Make each frame grayscale
  3. Apply a threshold to the grayscale image to create a binary image with highlighted object edges.
  4. Using a contour detection technique, locate the contours in the binary image.
  5. To choose only the objects of interest, filter the contours based on size, shape, and position.
  6. Draw a curve around each thing you've chosen.
  7. Save each object's position and size in a list or database.
  8. Overlay the object contours on the video stream.
  9. To follow the objects over time, repeat steps 1-8 for each frame of the video stream.
  10. Please keep in mind that this is only a general overview, and precise implementation details may differ based on the programming language and libraries used.

Analyzing the Shape and Texture of an Object

Contour visualization can be used to examine an object's shape and texture. The shape of an object can be established by drawing contour lines around it and examining the curvature and smoothness of the lines. The texture of the object can also be determined by evaluating the spacing and arrangement of the lines. This approach has a wide range of applications, including quality control to detect product faults and medical imaging to assess the form and texture of tissues.

Advantages and Limitations of Contour Analysis

Advantages: The ability to extract shape information and determine object boundaries makes contour analysis valuable for applications such as object recognition and tracking. It can also be used with a wide range of image types and is reasonably simple to set up.

Disadvantages: Contour analysis's limitations include its sensitivity to noise and reliance on edge detection algorithms. It may also have difficulty with items with uneven shapes or imprecise edges. Also, in some cases, it may not be as accurate as other image analysis techniques.

Conclusion

  • Contour analysis is a powerful OpenCV tool for picture analysis and processing. Contour detection techniques in OpenCV can identify and extract visual characteristics such as edges, lines, and forms. This approach has numerous applications, including object detection, recognition, tracking, and categorization.

  • Contour analysis, on the other hand, has significant limitations, such as being susceptible to noise and variations in lighting and background. It may also necessitate manual intervention to fine-tune the parameters and, in some cases, may be less accurate than other methods.

  • Overall, contour analysis is a valuable tool for image processing and computer vision applications, and it is likely to become increasingly more effective and widely utilized in the future as machine learning and computer vision technology progress.