VOOZH about

URL: https://www.geeksforgeeks.org/python/measure-size-of-an-object-using-python-opencv/

⇱ Measure Size of an Object Using Python OpenCV - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Measure Size of an Object Using Python OpenCV

Last Updated : 23 Jul, 2025

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

👁 Image
GeeksforGeeks logo

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
👁 Image
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
Image with multiple objects

Output:

👁 Image
Size of multiple objects
Comment