
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If an Image Contour is Convex in OpenCV Python
The function cv2.isContourConvex() is used to check whether a curve (contour) is convex or not. A contour of an object in the image is a curve joining all the continuous points along the boundary, having the same color or intensity. Contours are used for shape analysis and object detection and recognition, etc.
Syntax
The syntax for cv2.isContourConvex() is ?
cv2.isContourConvex(cnt)
Where, "cnt" is a numpy array of the contour points of an object in the image. It returns True if the contour cnt is convex, else False.
Steps
You can use the following steps to check if a contour in an image is convex or not ?
Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.
import cv2
Read the input image using cv2.imread() and convert it to grayscale.
img = cv2.imread('pentagon.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Apply thresholding on the grayscale image to create a binary image.
ret,thresh = cv2.threshold(gray,150,255,0)
Find the contours in the image using cv2.findContours() function.
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Compute convexity using cv2.isContourConvex(cnt). It returns True if the contour is convex, else false.
cnt= contours[0] k = cv2.isContourConvex(cnt)
Draw the contours on the input image.
cv2.drawContours(img, [cnt], -1, (0,255,255), 3)
Print convexity of the contour of an object in the image.
print("Convexity:", k)
Let's have a couple of examples for a better understanding.
Example 1
In the Python example below, we check that a contour of a rectangle is convex or not.
import cv2 import numpy as np img1 = np.zeros((350,630,3), dtype=np.uint8) img = img1[100:250, 200:380, :]=255 img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img,150,255,0) contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) print("Number of contours:", len(contours)) cnt= contours[0] k = cv2.isContourConvex(cnt) print("Convexity:", k) x, y = cnt[0][0] cv2.drawContours(img1, [cnt], -1, (0,255,255), 3) cv2.putText(img1, f'Convexity:{k}', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) cv2.imshow("Polylines", img1) cv2.waitKey(0) cv2.destroyAllWindows()
Output
On execution, it will produce the following output on the console ?
Number of contours: 1 Convexity: True
And we get this window, showing the output ?
Example 2
In this Python program, we check the convexity of a contour of star shape objects in the image.
import cv2 import numpy as np img1 = cv2.imread('star.png') img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img,0,255,0) contours,hierarchy = cv2.findContours(thresh, 1, 2) print("Number of contours:", len(contours)) cnt= contours[0] k = cv2.isContourConvex(cnt) print("Convexity:", k) x, y = cnt[0][0] cv2.drawContours(img1, [cnt], -1, (0,255,255), 3) cv2.putText(img1, f'Convexity:{k}', (x, y+30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) cv2.imshow("Image", img1) cv2.waitKey(0) cv2.destroyAllWindows()
We will use the following image at the Input File in this program.
Output
On execution, it will produce the following output on the console ?
Number of contours: 1 Convexity: False
And we get this window, showing the output ?