Open In App

Measure Size of an Object Using Python OpenCV

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
# Load the image
img = cv2.imread('/content/sample1.jpeg')
  
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
#to separate the object from the background
ret, thresh = cv2.threshold(gray, 127, 255, 0)
  
# Find the contours of the object 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
# Draw the contours on the original image
cv2.drawContours(img, contours, -1, (0,255,0), 3)
  
# Get the area of the object in pixels
area = cv2.contourArea(contours[0])
  
# Convert the area from pixels to a real-world unit of measurement (e.g. cm^2)
scale_factor = 0.1 # 1 pixel = 0.1 cm
size = area * scale_factor ** 2
  
# Print the size of the object
print('Size:', size)
  
# Display the image with the contours drawn
cv2.imwrite('Object.jpeg', img)
cv2.waitKey(0)
  
# Save the image with the contours drawn to a file
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
  
# Load the image
img = cv2.imread('path/to/image.jpg')
  
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
# Apply a threshold to the image to
# separate the objects from the background
ret, thresh = cv2.threshold(
    gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
  
# Find the contours of the objects in the image
contours, hierarchy = cv2.findContours(
    thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
# Loop through the contours and calculate the area of each object
for cnt in contours:
    area = cv2.contourArea(cnt)
  
    # Draw a bounding box around each
    # object and display the area on the image
    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)
  
# Show the final image with the bounding boxes
# and areas of the objects overlaid on top
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  
# Code By SR.Dhanush


Output:

Size of multiple objects



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads