Open In App

Check if image contour is convex or not in OpenCV Python

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see different ways to check if an image contour is convex or not with help of python OpenCV.

Contour:

Contours are continuous curves that represent the boundaries of an object in an image. In the field of computer vision and image processing, contours are used to detect and analyze the shape and structure of objects in images. It is essential to know whether a contour is convex or not, as convex contours have certain desirable properties that make them easier to work with. In this article, we will discuss different ways to check if an image contour is convex or not in OpenCV Python.

Convexity:

 A contour is said to be convex if all the points on the contour are located on the same side of the straight lines connecting any two points on the contour. In other words, a contour is convex if it is “rounded” or “bulging” outward. Non-convex contours, on the other hand, have “indentations” or “dents” in them, which means that some points on the contour are located on the opposite side of the straight lines connecting any two points on the contour.

Convex Hull: 

The convex hull of a contour is the smallest convex polygon that encloses the contour. It can be computed using the cv2.convexHull() function in OpenCV. The convex hull is useful in many image processing and computer vision applications, as it preserves the essential shape of the object while eliminating the “dents” or “indentations” in the contour.

Methods to check if a contour is convex:

Method 1: Using the cv2.isContourConvex() function:

The cv2.isContourConvex() function is a built-in function in the OpenCV library that can be used to check if a contour is convex or not. This function takes a single argument, which is the contour that we want to check. It returns a Boolean value, which is True if the contour is convex and False if the contour is not convex.

Here’s an example of how to use this function:

Python3




import cv2
  
# Read the image
image = cv2.imread(r"star.png")
  
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Find the contours in the image
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
# Check if the contour is convex
convex = cv2.isContourConvex(contours[0])
  
if convex:
    print('The contour is convex')
else:
    print('The contour is not convex')


Input image:

star image

Output:

The contour is convex

Method 2: Using the cv2.convexHull() function:

The cv2.convexHull() function can also be used to check if a contour is convex or not. This function takes a single argument, which is the contour that we want to check, and returns the convex hull of the contour. If the input contour is already convex, the returned convex hull will be the same as the input contour.

Here’s an example of how to use this function:

Python3




import cv2
  
# Read the image
image = cv2.imread(r"star.png")
  
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Find the contours in the image
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
# Get the convex hull of the contour
hull = cv2.convexHull(contours[0])
  
# Check if the contour is convex
if len(hull) == len(contours[0]):
    print('The contour is convex')
else:
    print('The contour is not convex')


Input image:

star image

Output:

The contour is convex

Advantages of Convex Contours:

  1. Convex contours have certain desirable properties that make them easier to work with. Some of these advantages are:
  2. Convex contours are always simple, which means that they do not intersect themselves. This makes it easier to compute the perimeter and area of the contour.
  3. Convex contours are always closed, which means that they enclose a region. This is useful in applications where we need to fill the region enclosed by the contour.
  4. Convex contours have a single global minimum and a single global maximum, which makes it easy to find the centroid or center of mass of the contour.
  5. Convex contours have a well-defined boundary, which makes it easier to detect and track objects in images.

Disadvantages of Convex Contours:

  1. Convex contours may not always accurately represent the shape of an object, especially if the object has complex shapes with “dents” or “indentations”.
  2. Convex contours may not capture the fine details of an object, as they tend to smooth out the contour and eliminate small features.
  3. Convex contours may not be robust to noise and occlusions, as they may “collapse” or “break” under such conditions.


Like Article
Suggest improvement
Share your thoughts in the comments