Measure Size of an Object Using Python OpenCV
Last Updated :
17 Apr, 2023
In this article, we will learn about how to measure the size of an object using OpenCV which is implemented in Python. It is implemented by finding the contours around it. This can be done by first loading an image of an object, converting it to grayscale, and applying a threshold to separate the object from the background. It then finds the object’s contours in the thresholded image and draws them on the original image. The code calculates the area of the object in pixels using the cv2.contourArea() function and converts the area to a real-world unit of measurement using a scale factor. Finally, it prints the size of the object in the chosen unit of measurement.
Measure Size of an Object using Python OpenCV
Let’s see a few examples to measure the size of an object using Python’s openCV module.
Example 1: Measure the size of a Single Object in Python
GeeksforGeeks logo
Python3
import cv2
img = cv2.imread( '/content/sample1.jpeg' )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127 , 255 , 0 )
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, - 1 , ( 0 , 255 , 0 ), 3 )
area = cv2.contourArea(contours[ 0 ])
scale_factor = 0.1
size = area * scale_factor * * 2
print ( 'Size:' , size)
cv2.imwrite( 'Object.jpeg' , img)
cv2.waitKey( 0 )
cv2.imwrite( 'object_with_contours.jpg' , img)
|
Output:
First we will use the cv2.imread() function to load the image into a numpy array then convert the image to grayscale using the cv2.cvtColor() function. Apply a threshold to the grayscale image to separate the object from the background. The threshold can be applied using the cv2.threshold() function.
To find and draw the contours of the object in the thresholded image, we will use the cv2.findContours() function and cv2.drawContours() function respectively. Use the cv2.contourArea() function to calculate the area of the object in pixels. And at last, display the image with the contours drawn using the cv2.imshow() function and wait for a key press using the cv2.waitKey() function. Optionally, save the image to a file using the cv2.imwrite() function.
Size: 85.81500000000001
Output image
Example 2: Measure the size of Multiple Objects in Python
New, let us see and example to measure the size multiple objects.
Image with multiple objects
Python3
import cv2
import numpy as np
img = cv2.imread( 'path/to/image.jpg' )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(
gray, 0 , 255 , cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x + w, y + h), ( 0 , 255 , 0 ), 2 )
cv2.putText(img, str (area), (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 1 , ( 0 , 0 , 255 ), 2 )
cv2.imshow( 'image' , img)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output:
Size of multiple objects
Share your thoughts in the comments
Please Login to comment...