# Measuring size of objects in an image with OpenCV

Measuring the size of an object (or objects) in an image has been a heavily requested tutorial on the PyImageSearch blog for some time now — and it feels great to get this post online and share it with you.

Today’s post is the second in a three part series on measuring the size of objects in an image and computing the distances between them.

Last week, we learned an important technique: how reliably order a set of rotated bounding box coordinates in a top-left, top-right, bottom-right, and bottom-left arrangement.

Today we are going to utilize this technique to aid us in computing the size of objects in an image. Be sure to read the entire post to see how it’s done!

Looking for the source code to this post?

## Measuring the size of objects in an image with OpenCV

Measuring the size of objects in an image is similar to computing the distance from our camera to an object — in both cases, we need to define a ratio that measures the number of pixels per a given metric.

I call this the “pixels per metric” ratio, which I have more formally defined in the following section.

### The “pixels per metric” ratio

In order to determine the size of an object in an image, we first need to perform a “calibration” (not to be confused with intrinsic/extrinsic calibration) using a reference object. Our reference object should have two important properties:

• Property #1: We should know the dimensions of this object (in terms of width or height) in a measurable unit (such as millimeters, inches, etc.).
• Property #2: We should be able to easily find this reference object in an image, either based on the placement of the object (such as the reference object always being placed in the top-left corner of an image) or via appearances (like being a distinctive color or shape, unique and different from all other objects in the image). In either case, our reference should should be uniquely identifiable in some manner.

In this example, we’ll be using the United States quarter as our reference object and throughout all examples, ensure it is always the left-most object in our image:

Figure 1: We’ll use a United States quarter as our reference object and ensure it is always placed as the left-most object in the image, making it easy for us to extract it by sorting contours based on their location.

By guaranteeing the quarter is the left-most object, we can sort our object contours from left-to-right, grab the quarter (which will always be the first contour in the sorted list), and use it to define our pixels_per_metric, which we define as:

pixels_per_metric = object_width / know_width

A US quarter has a known_width of 0.955 inches. Now, suppose that our object_width (measured in pixels) is computed be 150 pixels wide (based on its associated bounding box).

The pixels_per_metric is therefore:

pixels_per_metric = 150px / 0.955in = 157px

Thus implying there are approximately 157 pixels per every 0.955 inches in our image. Using this ratio, we can compute the size of objects in an image.

### Measuring the size of objects with computer vision

Now that we understand the “pixels per metric” ratio, we can implement the Python driver script used to measure the size of objects in an image.

Open up a new file, name it object_size.py , and insert the following code:

Lines 2-8 import our required Python packages. We’ll be making heavy use of the imutils package in this example, so if you don’t have it installed, make sure you install it before proceeding:

Otherwise, if you do have imutils  installed, ensure you have the latest version, which is 0.3.6  at the time of this writing:

Lines 10 and 11 defines a helper method called midpoint , which as the name suggests, is used to compute the midpoint between two sets of (x, y)-coordinates.

We then parse our command line arguments on Lines 14-19. We require two arguments, --image , which is the path to our input image containing the objects we want to measure, and --width , which is the width (in inches) of our reference object, presumed to be the left-most object in our --image .

We can now load our image and preprocess it:

Lines 22-24 load our image from disk, convert it to grayscale, and then smooth it using a Gaussian filter. We then perform edge detection along with a dilation + erosion to close any gaps in between edges in the edge map (Lines 28-30).

Lines 33-35 find contours (i.e., the outlines) that correspond to the objects in our edge map.

These contours are then sorted from left-to-right (allowing us to extract our reference object) on Line 39. We also initialize our pixelsPerMetric  value on Line 40.

The next step is to examine each of the contours:

On Line 43 we start looping over each of the individual contours. If the contour is not sufficiently large, we discard the region, presuming it to be noise left over from the edge detection process (Lines 45 and 46).

Provided that the contour region is large enough, we compute the rotated bounding box of the image on Lines 50-52, taking special care to use the cv2.cv.BoxPoints  function for OpenCV 2.4 and the cv2.boxPoints  method for OpenCV 3.

We then arrange our rotated bounding box  coordinates in top-left, top-right, bottom-right, and bottom-left order, as discussed in last week’s blog post (Line 58).

Lastly, Lines 59-63 draw the outline of the object in green, followed by drawing the vertices of the bounding box rectangle in as small, red circles.

Now that we have our bounding box ordered, we can compute a series of midpoints:

Lines 68-70 unpacks our ordered bounding box, then computes the midpoint between the top-left and top-right points, followed by the midpoint between the bottom-right points.

We’ll also compute the midpoints between the top-left + bottom-left and top-right + bottom-right, respectively (Lines 74 and 75).

Lines 78-81 draw the blue midpoints on our image , followed by connecting the midpoints with purple lines.

Next, we need to initialize our pixelsPerMetric  variable by investigating our reference object:

First, we compute the Euclidean distance between the our sets of midpoints (Lines 90 and 91). The dA  variable will contain the height distance (in pixels) while dB  will hold our width distance.

We then make a check on Line 96 to see if our pixelsPerMetric  variable has been initialized, and if it hasn’t, we divide dB  by our supplied --width , thus giving us our (approximate) pixels per inch.

Now that our pixelsPerMetric  variable has been defined, we can measure the size of objects in an image:

Lines 100 and 101 compute the dimensions of the object (in inches) by dividing the respective Euclidean distances by the pixelsPerMetric  value (see the “Pixels Per Metric” section above for more information on why this ratio works).

Lines 104-109 draw the dimensions of the object on our image , while Lines 112 and 113 display the output results.

### Object size measuring results

To test our object_size.py  script, just issue the following command:

Your output should look something like the following:

Figure 2: Measuring the size of objects in an image using OpenCV, Python, and computer vision + image processing techniques.

As you can see, we have successfully computed the size of each object in an our image — our business card is correctly reported as 3.5in x 2in. Similarly, our nickel is accurately described as 0.8in x 0.8in.

However, not all our results are perfect.

The Game Boy cartridges are reported as having slightly different dimensions (even though they are the same size). The height of both quarters are also off by 0.1in.

So why is this? How come the object measurements are not 100% accurate?

The reason is two-fold:

1. First, I hastily took this photo with my iPhone. The angle is most certainly not a perfect 90-degree angle “looking down” (like a birds-eye-view) at the objects. Without a perfect 90-degree view (or as close to it as possible), the dimensions of the objects can appear distorted.
2. Second, I did not calibrate my iPhone using the intrinsic and extrinsic parameters of the camera. Without determining these parameters, photos can be prone to radial and tangential lens distortion. Performing an extra calibration step to find these parameters can “un-distort” our image and lead to a better object size approximation (but I’ll leave the discussion of distortion correction as a topic of a future blog post).

In the meantime, strive to obtain as close to a 90-degree viewing angle as possible when taking photos of your objects — this will help increase the accuracy of your object size estimation.

That said, let’s look at a second example of measuring object size, this time measuring the dimensions of pills:

Figure 3: Measuring the size of pills in an image with OpenCV.

Nearly 50% of all 20,000+ prescription pills in the United States are round and/or white, thus if we can filter pills based on their measurements, we stand a better chance at accurately identification the medication.

Finally, we have a final example, this time using a 3.5in x 2in business card to measure the size of two vinyl EPs and an envelope:

Figure 4: A final example of measuring the size of objects in an image with Python + OpenCV.

Again, the results aren’t quite perfect, but this is due to (1) the viewing angle and (2) lens distortion, as mentioned above.

## Summary

In this blog post, we learned how to measure the size of objects in an image using Python and OpenCV.

Just like in our tutorial on measuring the distance from a camera to an object, we need to determine our “pixels per metric” ratio, which describes the number of pixels that can “fit” into a given number of inches, millimeters, meters, etc.

To compute this ratio, we need a reference object with two important properties:

• Property #1: The reference object should have known dimensions (such as width or height) in terms of a measurable unit (inches, millimeters, etc.).
• Property #2: The reference object should be easy to find, either in terms of location of the object or in its appearance.

Provided that both of these properties can be met, you can utilize your reference object to calibrate your pixels_per_metric variable, and from there, compute the size of other objects in an image.

In our next blog post, we’ll take this example a step further and learn how to compute the distance between objects in an image.

Ne sure to signup for the PyImageSearch Newsletter using the form below to be notified when the next blog post goes live — you won’t want to miss it!

### 125 Responses to Measuring size of objects in an image with OpenCV

1. Gadget Steve March 28, 2016 at 11:39 am #

Nice write-up as always Adrian but you have missed mentioning directly the prerequisite that all of the objects to be measured be co-planar with the reference object even though you did mention that the camera must be at as near as possible 90 degrees to that plane.

To do size and or distance calculations without a reference object you need a) the lens and focus information and b) two or more images of the same scene from different angles so as to use parallax calculations.

• Adrian Rosebrock March 28, 2016 at 1:28 pm #

Exactly. In a future blog post, I’ll detailing how to calibrate your cameras to obtain these parameters and thus undistort the image.

• vigneshwer dhinakaran March 28, 2016 at 11:55 pm #

Steve, Calibration can be done by taking a top view image and a image of your original camera position, after which you can find a homography matrix by which you wrap perspective the side view image to top view perspective.To find the homography matrix you will need to find similar points in both the top view reference and input image of different plane, which can be done by finding the sift point followed by a matcher.

You can look up to Adrian’s blog on making panoramic images for reference.

Only problem I see is to find the pixels per metric ratio value by giving an width value of the ref image, compensating for the camera distance from the image. Is there any way to solve the camera distance problem in an automated fashion?

• Ivan March 29, 2016 at 9:15 am #

Maybe 4 square marks in the corners of image cold be useful. The camera is well calibrated if aspect ratio of all marks is the same.

• Flip Liebenberg July 2, 2017 at 5:24 pm #

2. rahulagnihotri25@hotmail.com March 28, 2016 at 11:50 am #

Hi,

It is a really helpful article..Thanks for it.

I am currently working on identifying human body and then get the length, width and circumference of the body parts from top to bottom.

I have been able to identify the human body but getting is the measurement is an issue.

Regards,

• Adrian Rosebrock March 28, 2016 at 1:27 pm #

So if I understand your question correctly, your goal is to (1) detect the body, (2) extract each body part, and then (3) measure each body part? If so, you should do some research on skeletal models for the body, and perhaps even utilizing a stereo camera to help in the segmentation of each body part.

3. Ivan March 28, 2016 at 12:29 pm #

Wonderful, thank you very much. I think, I can apply this method to measure cells on microscope images. I already have the pixel ratio in EXIF from microscope, so I’ll declare it as a constant. Maybe you’ll suggest something more optimal for round and ellipsoid objects like cells? I think I should find the smallest and the largest diameter. After this I can find the excentricity.

• Adrian Rosebrock March 28, 2016 at 1:26 pm #

In that case, you might want to compute the minimum enclosing circle or the minimum enclosing ellipse, then compute your ratios based on that. Also, you might want to look into ImageJ which is heavily used for microscopic images — I even did some work for the National Cancer Institute developing plugins for ImageJ.

• Ivan March 29, 2016 at 1:39 am #

Thank you. I use it with CellProfiler, but I often need to develop some new functions. So I use the scipy most of the time for image analysis. Now I want to try the opencv. Thanks again for your blog. Useful tutorials.

• Adrian Rosebrock March 29, 2016 at 7:01 am #

SciPy is definitely quite useful, but I think you’ll find that OpenCV makes it even easier, once you get the hang of it 🙂

4. Ankit March 28, 2016 at 5:53 pm #

• Adrian Rosebrock March 28, 2016 at 6:20 pm #

Thanks Ankit!

5. muhammad iqbal March 28, 2016 at 8:52 pm #

hi its really helpful and very cleared explained

6. Clement March 29, 2016 at 9:44 am #

Thank you for this post. I’m working on this problem and i found a solution for measuring objects using the focale distance. Obviously this can only work in smartphone api so i’ve been learning java to perform it and i think i’m not far from a good result. The real problem is that we need a camera with a motorised focale.

7. Kevin Hainsworth March 29, 2016 at 1:46 pm #

This may be considered to be a bit “off the wall” but I have 3 table tennis balls (known size) positioned at the interface between the ceiling and the wall of a room. If I have an image that shows all the balls am I correct in thinking that I can compute the distance between the balls and also the distance from the camera to each of the balls? I appreciate that lens distortion may have some impact but I’m not interested in that here as I only want to obtain an approx. room size. Assuming that the ceiling is level I would like to expand this idea to include adjacent walls and floors i.e. to generate a 3D CAD model of the room.

• Adrian Rosebrock March 29, 2016 at 3:36 pm #

Yes, you are correct in your thinking. If you can segment the 3 balls from your image and you know their size, you can absolutely use this technique to compute the distance between them (I cover how to do this in next weeks blog post). You’ll also have to perform a calibration for the distance from ball to camera, but again, that’s easily doable.

8. Kyle W. April 1, 2016 at 10:16 pm #

Nice! Thank you for this post, it is always a pleasure to read through your tutorials and learn something new. Alternatively to this method, if you are interested in calculating the area of an object instead of retrieving its dimensions, there is a handy contourArea function in openCV! So once the contours are defined, you can easily calculate the area of each contour enclosure in pixels (1 line of code) and convert to appropriate dimensions via a reference object area. This is especially useful for the blob-like objects I am working with which have poorly defined dimensions.

• Adrian Rosebrock April 3, 2016 at 10:31 am #

Nice tip Kyle, thanks for sharing!

9. Marcus W. April 28, 2016 at 3:36 am #

Hi. Thats a really nice post. I recently started programming, but i wanted to adapt this programm a bit. How can i change it that way, that my referenceobject is detectet by color?
I hope you can help me 😀

Please excuse my english, i am no native.

10. Alberto April 28, 2016 at 7:46 am #

I cannot import imutils and scipy modules.

I think this happens because the comand “sudo pip install imutils” installs it at the route “/usr/local/lib/python2.7/dist-packages” instead of “site-packages” because i can see it exploring the directories but i dont really know if this is correct or no.

I tried to install the modules outside the virtual environment and inside it, previously entering the commands “source ~./profiles” and “workon cv”.

Iam working in a raspbian jessie fresh install on a raspberry pi 3 and i followed your new openCV 3 installation tutorial without any error.

Please let me know how could i change the imutils/scipy installation directory or what other error i am committing.

• Adrian Rosebrock April 28, 2016 at 2:25 pm #

The reason you are getting this error is because you’re using `sudo` and `pip` to install imutils and SciPy into the system install of Python. Instead, you should be installing them into the virtual environment:

• Alberto April 29, 2016 at 4:16 am #

Thanks for the reply, it seens that the command pip install imutils/SciPy in the cv environment now works after a reboot.

11. ravi May 25, 2016 at 5:15 am #

Thank you so much for your great effort..

I have a question, Is it possible to measure the size of an object without the reference object in place.

Is it possible ..?

• Adrian Rosebrock May 25, 2016 at 3:21 pm #

You need to perform some sort of camera calibration. That can be done offline before the script is deployed (provided you’re using the same camera sensors) or you can include the reference object in the image. In either case, the system must be calibrated first.

12. Ebad Syed May 26, 2016 at 1:44 am #

I want to extract the largest contour.

I am actually calculating size of a box, however there is another perfect contour inside the box.

So it detects both the squares. And I only want the outside one.

What should I do ?

• Adrian Rosebrock May 26, 2016 at 6:20 am #

You simply need to find the contour with the largest area:

`c = max(cnts, key=cv2.contourArea)`

• EBAD URRAHMAN SYED May 28, 2016 at 11:08 am #

thank you so muchhhh !

13. Juan May 29, 2016 at 8:37 am #

Hi. Thanks for this really nice post.
I recently started programming with python, but i wanted to adapt this programm a bit to my situation.
I have a set of images that represent vehicles and I want to classify them in groups based on vehicle height.
How can I do this knowing that I don’t have this reference object in place?

• Adrian Rosebrock May 29, 2016 at 1:51 pm #

At some point, you need to calibrate your camera. You can do this “offline” before you take photos of your vehicles (such as computing the reference object focal length and then removing the reference object). But at some point the camera has to be calibrated.

14. Peng Fei June 2, 2016 at 9:29 am #

Thank you so much for the post! It’s really interesting. Any advices for measuring the size of an object in real time?

• Adrian Rosebrock June 3, 2016 at 3:07 pm #

You can use this same approach to measure the size of objects in real-time. Just wrap the around a method to access a video stream, such as this one.

15. Themba June 3, 2016 at 8:05 am #

Hi Adrian, thanks for the advice and this tutorial. I managed to use this tutorial to calibrate my object tracking project.

Actually, the computer vision bundle helped me to improve my programming skills. And I want to take pyimagesearch gurus course but my problem is that I do not have a good programming background, so it might take me some time.

• Adrian Rosebrock June 3, 2016 at 2:57 pm #

No problem at all Themba, I’m happy that myself/the PyImageSearch community was able to help on your journey 🙂

• Themba June 10, 2016 at 8:52 am #

Hi Adrian, I have been experimenting with different images and different objects within the image. I noticed that sometimes it does not detect some object, so to make it detect the object I varied the GaussainBlur() values from (7,7) to (11,11). However, still when I do that it does not detect all the object in the image.

So my question is how can I ensure that I detect all of the objects with the image ?

• Adrian Rosebrock June 12, 2016 at 9:39 am #

As you noted, detecting all objects in your image is highly dependent on (1) the image, (2) the contents of the image, and (3) your lighting conditions/environment. It’s hard to say which (or even all 3) is contributing to the issue here. All I can say is that you should continue to play with the parameters — but also keep in mind that for certain objects, you won’t be able to detect them using basic image processing techniques. Some objects require machine learning and/or object detection.

16. Piet Visser June 20, 2016 at 2:30 am #

First of all my sincere respect for what you’re doing.
I’m totally new at this and just started to work on measuring 3D-objects very recently.
My ultimate goal is to be able to get X, Y and Z dimension from a picture taken by a RPi V2-type camera, but the first issue to overcome is how to calibrate the camera.
I think the correct procedure would be to take several pictures from an object with known dimensions on different distances and count pixels to find the ratio formula, correct?
Any other suggestions?

• Adrian Rosebrock June 20, 2016 at 5:27 pm #

3D is an entirely different world (no pun intended). I honestly don’t have much experience in 3D, my main area of expertise focuses on image search engines and image classification (and even lately, deep learning). Essentially, you would need to create a 3D model of your object from 2D images. And at that point, you’ll be doing much more complex camera calibration anyway, which would lead to better accuracy.

17. Bilal Khan June 29, 2016 at 6:37 am #

Which IDE is Best to try this code

• Adrian Rosebrock June 29, 2016 at 1:59 pm #

I personally like either Sublime Text 2 or the PyCharm.

18. neo September 2, 2016 at 7:11 am #

wonderful post ! Explained well
Infact I’m working on a project which detects the dimensions of our dress using camera .
This helps a lot
thanks

19. Ramanan September 13, 2016 at 10:23 am #

I really find your post interesting!
I am doing a project where there is a conveyor on which boxes( cuboid shape) moves There is a 3d camera mounted on top to recognize these boxes and further we need to determine the dimensions and pose of the boxes.

I have been studying and researching a lot on these topics and what algorithms to choose and how to proceed.To be brief i am unable to proceed next.

My task has the following constraints:

1) There should be no database of models created from which one can perform feature matching to detect objects.

The algorithm used for object detection should directly detect the object and provide its pose.

2) The shape of the object is always a cuboid/box shaped. The edges of the box might not be necessarily sharp. It can have rounded edges also. But the object to detected is always box shaped

3) Also my camera would be mounted in an inclined angle( not parallel to the object)

Can i use Opencv for this task. If yes, how do i proceed??

• Adrian Rosebrock September 13, 2016 at 12:44 pm #

Hey Ramanan, this sounds like a pretty neat project, although I don’t think it’s a great idea to constrain yourself from feature-based approaches. All that said, I personally don’t do much work in 3D and stereo vision, so I’m unfortunately not the right person to ask regarding this project. Best of luck!

20. Eduardo Antonio September 15, 2016 at 7:10 pm #

I want to put an “if” to display a “yes”, if the object has the size I want

what should I do?

have a nice day 😉

• Adrian Rosebrock September 16, 2016 at 8:23 am #

After Line 11 I would do something like:

Where the “…” signifies the remaining parameters to the function.

21. Dj September 16, 2016 at 11:29 am #

I def need help on combining several of these projects from pyimagesearch.com. Like skin finder, object detector, measuring, ect. Can you or anyone help me to get a project I’m trying to work on started and working starting ASAP.. Please…. Anyone.

22. Antonio September 24, 2016 at 11:55 pm #

i was wondering if Can i use this code to work together with my webcam?

-Have a nice day!!

23. Raymart Sison September 28, 2016 at 7:38 am #

Can someone please tell me how can i extract those measured value and put it in a txt file?

• Adrian Rosebrock September 28, 2016 at 10:33 am #

You can use simple file operations to write the measurements to file. I would suggest using using this tutorial to help you understand the basics of file operations.

24. Amar October 17, 2016 at 2:05 am #

Thank you so much for this great post!
I was having a problem while installing scipy on Rpi 3. I am trying to install it into the virtual environment, however, that last thing that shows on the window “Runing setup.py bdist_wheel for scipy … /” and hangs there forever.

What can I do to install scipy… I would greatly appreciate your help!

Thanks!

• Adrian Rosebrock October 17, 2016 at 4:03 pm #

It’s not “hanging” — it’s actually installing. It takes awhile to install SciPy on the Raspberry Pi 3. If I recall correctly, it took approximately 45-60 minutes to install the last time I did it.

• Amar October 18, 2016 at 6:47 am #

Thanks for the reply, it was eventually installed after waiting for a while!

25. mahmoud October 27, 2016 at 5:55 am #

i want to measure object by millimeters !!

• Adrian Rosebrock November 1, 2016 at 9:28 am #

That is totally doable with this code. Just change the command line argument to be your known width in millimeters then update the `cv2.putText` call. The exact same algorithm can still be used.

26. Larry November 10, 2016 at 7:14 pm #

Do you think is possible to get an approximate of the circumference/Shape of an object if we have 2 or 3 pictures from the object?

• Adrian Rosebrock November 14, 2016 at 12:18 pm #

Yes, once you know the number of pixels per measuring metric you can compute the minimum bounding circle via `cv2.minEnclosingCircle`. This will give you the radius of the object which you can then use to derive the circumference.

27. Joey November 27, 2016 at 1:53 am #

Hi Adrian! I’ve been a silent reader of your blogs! Personally, you have helped me a lot especially that I am working on a project now. I just want to know how to do this in real time? Like measuring objects in a video?

• Adrian Rosebrock November 28, 2016 at 10:28 am #

It is totally possible to measure the size of objects in a video stream, just wrap this code in a loop that continuously polls frames from a camera sensor. This blog post will help you get started. I also provide more examples of working with video streams inside Practical Python and OpenCV.

• margarita April 7, 2017 at 11:39 am #

Firstly, thank you very much for your guidance! You are amazing! Could you please guide how to make your code work in real time?

• Adrian Rosebrock April 8, 2017 at 12:46 pm #

I can’t provide 1-on-1 guidance or write the code for you, but I do highly recommend you read this post where I demonstrate how to access video streams. From there you can combine the two tutorials.

28. Max December 5, 2016 at 1:13 pm #

Hello,
Thanks for the nice guide. I have a problem. I Have a camera. I want to take a photo from the camera and mark out an area on it using mouse. Then i want to calculate the area within the marked area. Can you please guide me how do I do it?

Regards

• Adrian Rosebrock December 5, 2016 at 1:21 pm #

If you want to use mouse events, please refer to this blog post. This post will show you how to use mouse events to capture your click and marking.

• Max December 5, 2016 at 1:34 pm #

What I need to do is draw a freeform area, not any specific geometric shapes like squares/rectangles/circles, and finally calculate the area within.

Regards
Max

• Adrian Rosebrock December 5, 2016 at 1:53 pm #

In that case, I would modify the script to maintain a list of (x, y)-coordinates that you click. You can then loop over them and draw them with `cv2.line`. There is also a polygon drawing function in OpenCV but I can’t remember the name of it off the top of my head. You can look it up in the OpenCV docs.

29. Suraj December 15, 2016 at 5:20 am #

after executing the code I’m getting error of image required.. even after mentioning the path and Is there a need to import argparse if yes then plz do tell how to install it.
Regards

• Adrian Rosebrock December 18, 2016 at 9:07 am #

It sounds like you are having problems with the command line arguments. I would suggest you read this tutorial before continuing.

30. Gerdy Hasni December 23, 2016 at 9:31 am #

sir, may I ask how to get the angle of things while your program about the measuring of objects? before that, personally I’m very grateful of your code and thanks to you

• Adrian Rosebrock December 23, 2016 at 10:47 am #

You can use the `cv2.minAreaRect` function to obtain the angle of a rotated rectangle that encapsulates the object.

31. GK December 28, 2016 at 4:03 pm #

Hope you had a wonderful Christmas!
I have been following your tutorials, and I have learned quite a lot of things from implementing these projects.
Now I have a question, would it be possible to determine the size of the object with the following conditions:
1.Without a reference object
2.We know the FOV
3.We know the distance – readings from a sensor.
Would you be able to give me some directions?

Thank you,
GK

• Adrian Rosebrock December 31, 2016 at 1:28 pm #

If you don’t have a reference object you would need to calibrate your camera and determine the intrinsic camera parameters first.

• GK January 3, 2017 at 1:02 am #

Would you be able to give me some specifics on the parameters?
Here’s what I thought I’d do:
I know the distance, real time measurements from a sensor.
I know the F of the camera.
I know the area occupied by the object in the image.
Based on this, following your tutorial on finding the distance between the object and the camera, would it not be possible to find the size of the object?

Note: the object is in the same plane as the entire image. There are no other objects in between or in the background.

• Adrian Rosebrock January 4, 2017 at 10:47 am #

It’s not exactly that simple. I don’t have any tutorials on this topic at the moment, but I’ll certainly add it to my queue and try to cover it in the future. I can’t think of any good existing Python tutorials off the top of my head, but I know this MATLAB tutorial would be a good start for you to understand the basic principles.

• GK January 5, 2017 at 12:53 pm #

Thank you Adrian, for the suggestion. I have one more question.
Would it be possible to measure the size of the object, if I have a reference object with known size and distance, but in separate images?
So, here’s what I’m looking at:
I know the size, and the distance to a known object – separate images (following your tutorial, 3 images).
I know the focal length and the actual distance from camera to the target object – from a sensor, I’m able to find the real-time distance.
This looks doable to me, I’m just kind of confused :-/ Would you be able to throw some light on this issue? Thank you 🙂

• Adrian Rosebrock January 7, 2017 at 9:37 am #

If the images were captured using the same (calibrated) camera sensor, then yes, you could still compute the object size.

32. Marcos January 9, 2017 at 12:23 pm #

Hi!

I have implemented your example and modified it a little to work with live video. Now I’m trying to get measurements on objects’ volume using only the webcam. Can you point me in some direction? Thanks!

P.S.: Adrian, your content rocks! Thanks for the effort and congrats on the approaches. Always easy to learn from you.

• Adrian Rosebrock January 10, 2017 at 1:10 pm #

What type of object are you working with? Normally computing the volume would require depth information, in which case I would recommend using a stereo/depth camera instead.

• Marcos January 11, 2017 at 11:31 am #

Hey, Adrian. Thanks for the attention.

I’m working mainly with boxes and packages. It’s for a logistics project. It’s a challenge and I’m restrained to webcams, though I can use other commonly available material. Of course I’m expecting not-so-refined measures.

Cheers!

• Adrian Rosebrock January 12, 2017 at 8:00 am #

Personally, I don’t think you’re going to get a decent measure of volume without using 2 cameras. If you can setup two webcams you could essentially create a stereo-vision environment by calibrating them and obtaining an approximate depth map.

33. Ashutosh January 18, 2017 at 12:21 am #

I have another thought on the approach. If I crop the image to keep the coin part, and then use the labelling method to find its size in pixels. Finally, using the pixels and the known dimensions I would find the pixels per metric. Would that be a right approach?

• Adrian Rosebrock January 18, 2017 at 7:09 am #

Yes, if you can manually label the ROI and use the ROI to compute the dimensions this is essentially equivalent to teh calibration step.

34. ruhi February 1, 2017 at 3:04 am #

I have one template image where i have fixed size rectangle which contain some information. Now, i want to search that exact fixed size rectangle from query image then perform cropping. Query image may be in different DPI/resolution format.How can i deal with this issue? Is this article help me? Please suggest some idea for solving it.

• Adrian Rosebrock February 1, 2017 at 12:48 pm #

If your query image is a different resolution I would suggest always resizing the query to a fixed resolution and then applying the multi-scale template matching technique proposed in this blog post.

35. Jean February 4, 2017 at 11:20 am #

Hi Adrian, I am new to computer vision and as the other commenters said many times I am absolutely grateful with the help you’re providing the community.

I noticed your comment regarding the need to aim squarely at the target to obtain a precise measurement. In my case by design my target will be up to 2 degree off. Is there a way to compensate for this?

What I’m trying to do is aim a laser at a ruler placed a few feet out and capture the laser position (e.g. aiming at the 128mm mark). I’m not sure if the better approach is:

1) to write code to detect this laser line and use ocr to interpret where its aiming

or 2) use the technique presented in this post and find a way to offset the 1-2 degree distortion. I know the ruler total length, could I measure distance to the left and right sides of the ruler to derive its angle and then use this info to compensate when measuring the target object length (e.g. from the ruler zero mark to the laser line)?

I’m shooting for 1 or 2 mm accuracy. Thanks in advance for pointing me in the right direction

• Adrian Rosebrock February 7, 2017 at 9:29 am #

The method in this blog post requires only simple camera calibration based on the output image. For your level of accuracy I would suggest instead computing the intrinsic properties of the camera and doing a much more accurate calibration. I don’t have a tutorial on this at the moment, but I will try to do one in the future. In the meantime, take a look at this excellent resource on intrinsic camera properties.

36. Jackie March 1, 2017 at 5:12 pm #

Thank you so much for this post- it’s really great!

For some reason, this script doesn’t animate for me like yours does. I just see the coin’s dimensions and none of the dimensions of the other items. I’m running an Ubuntu VM on Mac.
What would you recommend I do?

• Adrian Rosebrock March 2, 2017 at 6:45 am #

I’m not sure what you mean by “animate” as I created a GIF file to demonstrate the animation. Click on the active window and press any key to advance the detections. The call to `cv2.waitKey` pauses execution.

• Jackie March 2, 2017 at 1:06 pm #

Oh! Thank you so much, Adrian! It all works now.

37. Ahmed March 19, 2017 at 9:10 pm #

Hi Adrian! Thank you so much for this useful post
when i run this program tell me this error

Traceback (most recent call last):

from scipy.spatial import distance as dist
ImportError: No module named scipy.spatial

also i installed scipy

• Adrian Rosebrock March 21, 2017 at 7:24 am #

It sounds like you might have forgotten to install NumPy and SciPy:

`\$ pip install numpy scipy`

38. Rafii Naufal March 29, 2017 at 6:55 pm #

hello adrian.. it’s work very well in mine…
but if i wanna change the source image to webcam, what steps should i do so it’s well work in my raspi 3? i try change the source but it’s always failed.. thanks for the knowledges

39. Prateek April 1, 2017 at 6:45 am #

I want to detect seperate bottles from an image of 5 bottles and process on it that is detect neck and shoulder of bottle and find the centre point of the two and check whether the liquid is filled upto that point. Can you help?

• Adrian Rosebrock April 3, 2017 at 2:11 pm #

Hey Prateek — I think it would be easier to offer help in this situation if you had an example image of what you’re working with.

40. E Hamza April 12, 2017 at 9:25 am #

hello Adrian, i want to save the measuring sizes and send it to arduino in order to compare it with a data base, is it possible and how ?
Can you help?

41. hamed May 18, 2017 at 1:59 am #

Thank you
If these points are not neatly together, and in what way we can resolve this problem.

42. Ivan May 24, 2017 at 6:25 am #

i am try your script but after run. like this ?

usage: object_size.py [-h] -i IMAGE -w WIDTH
object_size.py: error: argument -i/–image is required

• Adrian Rosebrock May 25, 2017 at 4:20 am #

You need to supply the command line arguments to the script. You can read more about command line arguments here.

43. shorav May 26, 2017 at 10:24 pm #

i am trying to run the code but its throwing the error.. i just want to know that what i have to write in place of -i and –image in order to run the entire code because its throwing an error

Code:

• Adrian Rosebrock May 28, 2017 at 1:03 am #

Please read up on command line arguments before continuing. Knowledge of command line arguments will fix your issue.

44. Henry June 2, 2017 at 5:16 am #

I have a problem py [-h] -i IMAGE -w WIDTH
meas.py: error: the following arguments are required: -i/–image, -w/–width

im using python 3 and opencv3

• Adrian Rosebrock June 2, 2017 at 1:58 pm #

Please read up on command line arguments. You are forgetting to supply command line arguments to the script.

45. Nashwan June 5, 2017 at 5:55 pm #

Thanks for amazing blog post, thanks in advance for this great toturial..
I want to save the result in one image and result show me measurement for all objects in one image
Plz help me
Regards

• Adrian Rosebrock June 6, 2017 at 11:58 am #

You want to save the resulting image to disk? If so, just use `cv2.imwrite`.

• Nashwan June 6, 2017 at 7:46 pm #

No, I know this
But I want to draw all the lines for all objects and saved it in a single image..?? How i can do?

46. Jacki June 19, 2017 at 12:11 pm #

I have a question
Why we used dilate and erode algorithm??
What the work of these two algorithms?
If ypu can quide me to these two algorithm

Best regards

• Adrian Rosebrock June 20, 2017 at 10:56 am #

The dilation and erosion helps close any gaps between edges in the edge map.

• Jacki June 20, 2017 at 12:09 pm #

47. Rich July 5, 2017 at 3:47 am #

Hi, can this method (or similar) be used to measure the length of a curved object? Specifically an animal that is not perfectly straight? I need to find a way to measure the nose to tail length, but the animal may be bent in the middle.

• Adrian Rosebrock July 5, 2017 at 5:52 am #

I’m not familiar on the science/literature of measuring the length of animals, but I think it would have to be an approximation to the length, especially if the animal is free and moving. Otherwise, if you know the contour of the body, then you might be able to apply splines to determine the length, but that’s something I have not done.

48. Vasanth July 14, 2017 at 1:09 pm #

I just need to know how to change the code for giving the image and width directly in the code instead of giving it in command prompt .
Eventhough it is a simple question to you please guide me because i’m a beginner here

• Adrian Rosebrock July 18, 2017 at 10:16 am #

There are a number of ways to do this, but if you want the easiest, foolproof way to do, change Line 20 (after the command line arguments are parsed) to be:

`args["width"] = YOUR_WIDTH`

Otherwise, delete the command line argument parsing code and hardcode the values into separate variables.

49. V July 15, 2017 at 1:48 am #

Can u give me the code for automating it by taking photograph from webcam and process it instead of giving it in command line…

• Adrian Rosebrock July 18, 2017 at 10:10 am #

Sorry, I do not write code for you. I am happy to help point you in the right direction and provide these blog posts, but again, I cannot write the code for you. If you need help working with your webcam consider referring to one of my many tutorials here on PyImageSearch or working through Practical Python and OpenCV.

50. Ryan July 17, 2017 at 10:35 pm #

hello , i just want ask to question regarding the pixel measurement,, suppose i have an image with pixel dimension of 1366×768 .. the center of image consist of a red bounding rectangle, is it possible for opencv to get the pixel dimension of the rectangle ? in pixels also like 500px x 400px ?? thank you sir ..

• Adrian Rosebrock July 18, 2017 at 9:51 am #

Yes. Simply detect the red bounding rectangle compute its bounding box.

51. Charlz July 17, 2017 at 11:31 pm #

chi, how can i get pixel measurement of object in an image instead of inches?

• Adrian Rosebrock July 18, 2017 at 9:49 am #

The pixel measurement is used to derive the inches. You already have the pixel measurement. Specifically, Lines 90 and 91 compute the distance in pixels.

52. Pixels July 23, 2017 at 7:53 am #

Thanx for the nice work Andrian, I want to measure particle size distribution for particles on a fast moving conveyor belt. I have tested your method and it seems quite slow for the application, can you point me in the right direction?

• Adrian Rosebrock July 24, 2017 at 3:35 pm #

The issue here will be running your application in real-time. The algorithm this method uses is actually quite fast, but you might be running into issues with I/O latency. I would also make sure that the camera you are using can read frames at a frame rate high enough to ensure you can (accurately) measure your objects. To reduce I/O latency, take a look at this tutorial.

53. Jeff July 28, 2017 at 4:13 pm #

Awesome post – everything that I’ve read on your blob has been very clear and useful! Super impressed 🙂

I’m working on a simple application that attempts to measure the height and width of a perfect rectangle in the image with a reference object. However, the perspective distortion in the images isn’t constrained (meaning no perfect perpendicular picture like in this example). Additionally, the reference object is very small compared to the rest of the image – meaning I can’t warp the rectangle based on the perspective transform of the reference. Any thoughts on how to approach this problem?

Thanks!

• Adrian Rosebrock August 1, 2017 at 9:52 am #

This certainly makes the project more challenging. It sounds like you need a more advanced camera calibration, such as computing the intrinsic and extrinsic parameters.

54. Ali Ahmad August 14, 2017 at 1:31 pm #

Hello Sir,

Can you pitch and suggest some idea to measure the hand size from the hand pic. The reference of the hand size is given in the picture what i mean “https://drive.google.com/file/d/0B7u-f_qlWl0jTVpvMHJWWk5wRzQ/view?usp=sharing” Please help me with that

• Adrian Rosebrock August 17, 2017 at 9:28 am #

If you’re trying to measure the size of the hand you can use this tutorial. You find the points along the hand you want to measure and then use the triangle similarity we covered here. The Euclidean distance between the points will give you distance in pixels. Then apply the triangle similarity to determine the distance in real-world metrics.