OpenCV Python ŌĆō How to find and draw extreme points of an object on an image?
OpenCV Python is a powerful library for image processing and computer vision tasks. In this article, we will look at the process of finding and drawing extreme points of an object on an image using OpenCV Python.
What are Extreme Points?
Extreme points refer to the points on the contour of an object where the slope changes sign. They represent the corners or endpoints of the object and can be useful in detecting and recognizing objects.
In image processing, finding and drawing extreme points of an object on an image can help us identify the shape and location of the object.
Finding Extreme Points using OpenCV Python
The first step in finding extreme points in an image is to identify the object of interest and extract its contour. We can do this using various techniques such as edge detection, thresholding, and segmentation.
For the purpose of this tutorial, we will assume that we have already extracted the contour of the object using one of these techniques. We will then use the cv2.convexHull()
function to find the extreme points of the object.
Here is an example code that demonstrates how to find and draw extreme points of an object on an image:
import cv2
# Load image
img = cv2.imread('object.jpg')
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply binary thresholding
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contour of object
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find convex hull of contour
hull = cv2.convexHull(contours[0])
# Draw contour and hull on image
cv2.drawContours(img, [contours[0]], 0, (0, 255, 0), 2)
cv2.drawContours(img, [hull], 0, (0, 0, 255), 2)
# Display image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Let’s break down the code step by step:
- We start by loading the image and converting it to grayscale.
- We apply binary thresholding to the grayscale image. This helps to separate the object from the background and create a binary image.
- We use the
cv2.findContours()
function to find the contour of the object in the binary image. This function returns a list of contours and a corresponding hierarchy. - We use the
cv2.convexHull()
function to find the convex hull of the contour. This function returns the extreme points of the object. - We draw the contour and convex hull on the original image using the
cv2.drawContours()
function. - Finally, we display the image using
cv2.imshow()
and wait for a key press before closing the window.
Conclusion
In this article, we have looked at how to find and draw extreme points of an object on an image using OpenCV Python. Extreme points are useful in detecting and recognizing objects in an image, and can be easily obtained using the cv2.convexHull()
function. With this knowledge, you can start building more advanced computer vision applications that require the detection and recognition of objects.