Measuring size of objects in an image with OpenCV

size_of_objects_example_02Measuring 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?
Jump right to the downloads section.

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.

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.

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:

Measuring the size of pills in an image with OpenCV.

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.

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.

Be 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!

Downloads:

If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , ,

306 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 #

        we can all do with a lesson on how to calibrate your camera. Please Adrian!

      • Alex V March 2, 2018 at 11:46 am #

        Hi, I’m working with cameras that have wide angle lenses, I’m trying to find a way to calibrate the image to a chessboard grid in order to transform the image to flat front. that way, I can manipulate the image from there without the lens distortion messing up further analysis. I’d appreciate some advice, or even a blog post on the topic.

        • Adrian Rosebrock March 7, 2018 at 9:49 am #

          I will try to do some more advanced calibration tutorials in the future but I’m not sure when/if that will be. Thank you for the suggestion though 🙂

      • Ashish shahi March 12, 2018 at 1:51 pm #

        Is it possible to find the volume of any object by finding the length and width from top and from side to find height from 2 different pictures?

      • Harry May 29, 2018 at 5:46 pm #

        Hi Adrian,

        Did you ever end up creating this tutorial? I am very interested.

        • Adrian Rosebrock May 31, 2018 at 5:17 am #

          I have not. I hope to do so in the future but I’m not sure if/when that may be.

    • Arvind Sai July 19, 2018 at 2:54 pm #

      @Gadget Steve @Adrian Rosebrock .. I want to measure the height of a person from a mobile image. Will it work if i find the homography matrix between few points in real world and corresponding points in image. Then apply that matrix to calculate image points representing humans. Or is there a better way.

  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.

    Can you please help.

    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 #

    Great Article Adrian!!

    • 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 #

    Hi there Adrian, awesome site.

    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.

    Thanks in advance.

    • 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 !

      • osama January 28, 2018 at 6:04 pm #

        how to specify area where we need to have contour?

  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?

    Thanks in advance.

    • 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 #

    Hi Adrian,

    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 #

    Hello Adrian,

    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 #

    Hi Adrian this post is very helpful

    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 #

    Hey Adrian,

    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 #

    Hi Adrian,

    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 #

        Thanks Adrian for the reply, it was a nice read.

        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 #

    Hi Adrian,
    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 #

        Thank you Adrian.
        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.

        • Laura May 28, 2018 at 3:40 pm #

          Hi

          I’m working on a similar project. Did you make any progress on finding the volume?

  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.

    Thanks in advance.

    • 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 #

    Hi Adrian!

    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
    i’m waiting for your help

    • 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 #

    Hello Adrian;
    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 #

    My problem is not counting this point is please help me!
    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

  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:

    ap.add_argument(“-i”, “–image”,

    • 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 #

    Hi Adrian,

    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 #

    Hello Adrian;
    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?

        Thanks in advance for reply

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

    Hi adrean;
    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

    Thanks in advance fo reply
    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 #

        Thank you for reply dear dr. Adrian

  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 #

    Hi Adrian Rosebrock !!!
    I’m really impressed with this article !!!
    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
    Thanks in advance

    • 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 #

    Hi Adrian Rosebrock
    Thank u so Much for this Article
    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 #

    Hi Adrian,

    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.

  55. Nana September 4, 2017 at 4:41 am #

    Hallo Adrian,

    I am working on identifying human body and then measure the arm span and Length. I use the camera of car. I dont have a refrence in Image. Can i do that with instrinsic and extrinsic calibration. Is it maybe possible with “Multi view Geometry”. please help me with that.

    Thanks

    • Adrian Rosebrock September 5, 2017 at 9:23 am #

      Hi Nana — you would need to use camera calibration and ideally multiple cameras to do this project. I am not an expert on 3D/stereo vision so I would suggest using this tutorial in the OpenCV docs to help you get started.

  56. Steve September 9, 2017 at 10:57 pm #

    Hi

    Im not sure my result right or not, so how can I find the material that you used in your blog ?

    Another confused point, why I cant pip install pyimagesearch appeared in your another blog, are you write this moduel ? How can I install it, I want to redo what you’ve done of your blog.

    thanks for you attention.

    • Adrian Rosebrock September 11, 2017 at 9:14 am #

      Please use the “Downloads” section of this tutorial to download a .zip of the code. Inside you’ll find the “pyimagesearch” module.

  57. yassin September 17, 2017 at 5:46 am #

    Hello Adrian,
    Awesome post – everything that I’ve read on your blog has been very clear and useful! Super impressed, how can I run it on windows or ubuntu? because I tried all the ways, please help me

    • Adrian Rosebrock September 18, 2017 at 2:10 pm #

      Hi Yassin — this code will work on Ubuntu and Windows. Use the “Downloads” section, download the code, and then execute. If you are running into an error feel free to share.

  58. Robin David September 20, 2017 at 9:30 am #

    Hello Adrian,

    First of all, I would like to thank for your posts. Recently I have jumped into the computer vision domain.

    Please let me how to implement the below points

    1) I need to set a unique ID automatically for each object in an image.

    say example – Figure 1 has 6 objects (Including reference object). ID should automatically generate for all six objects (Example:ob1,ob2…ob6)

    2) get all the object’s height and width with object ID into a text file

    Example:
    ob1 0.9 1.0
    ob2 0.5 0.5
    .
    .
    .

    also, Correct me if my question is wrong or it has any logic mistake.

    Thanks in advance.

    • Adrian Rosebrock September 23, 2017 at 10:20 am #

      I would suggest applying an object tracking algorithm. The simplest would be “centroid tracking”. Compute the centroid of all objects in consecutive frames. Then compute their Euclidean distances. Centroids with the smallest Euclidean distance belong to the same object. Your logic is correct, but you need to implement centroid tracking.

  59. luis perez galvan September 20, 2017 at 1:50 pm #

    Hello, is there a way using OpenCV to obtain measurements for different human body areas like: neck, person height, belly, etc… We are looking into this for a wellness and nutrition follow up. Thx

    • Adrian Rosebrock September 23, 2017 at 10:19 am #

      It can, but not easily. You could also get wildly inaccurate results depending on how good (or poor) your human body segmentation algorithm is. Typically the best results would come from stereo vision/depth cameras to aid in the segmentation. However, this is not something I have ever tried.

  60. Kelvin September 28, 2017 at 2:59 am #

    Hi Adrian, is it possible to use this tutorial to calculate the dimensions of the bounding box for the “Real-time object detection with deep learning and OpenCV” tutorial you’ve done recently? I am thinking of using the dimensions of the bounding box to determine the distance a person is away from the camera, since the further you stand away from the camera, the smaller you are and hence the smaller the bounding box. Thanks!

    • Adrian Rosebrock September 28, 2017 at 9:01 am #

      Provided you perform the triangle similarity to obtain a rough calibration for the camera, yes, you can combine this method with the deep learning-based object detector to obtain an approximation to the distance.

      • Kelvin October 1, 2017 at 11:06 pm #

        Would i need a stereo camera for that? I was just thinking of a way to measure the dimensions of the bounding boxes. With the dimensions, i would then use the values to do a simple command where the robot stops moving when the size of the bounding box reaches > a certain value. Thanks!

        • Adrian Rosebrock October 2, 2017 at 9:27 am #

          Stereo and 3D cameras would undoubtedly given you better segmentations and approximations to depth, thereby giving you more accurate measurements. You can still do an approximation using a simple 2D camera though, provided that your detections are accurate enough.

  61. pawan October 1, 2017 at 9:10 am #

    hi

    I just downloaded the code and trying in my pc the problem I faced is that the object is placed only one image not traversing all the object in image

    • Adrian Rosebrock October 2, 2017 at 9:43 am #

      Hi Pawan — can you elaborate on your comment? I’m not sure what your exact question is.

  62. pawan October 2, 2017 at 1:27 pm #

    in the tutorial as I see the measurement of objects in rotating through each object in the image but when i tried with the downloaded code with the same image the hight width box is still on one object image it is not measuring the other object size. ( )

  63. Jairo October 5, 2017 at 7:09 pm #

    Hi Adrian, first of all a pleasure to be in your page, well I have a doubt I hope you can help me clarify I do not understand where you say “Now, suppose that our object_width (measured in pixels) is computed be 150 pixels wide (based on its associated bounding box) ”

    Why 150 Pixels? 😀

    • Adrian Rosebrock October 6, 2017 at 4:58 pm #

      We need to know (1) the size of the reference object in pixels and (2) the size of the reference object in actual, measurable dimensions. Once we have both these values we can compute the triangle similarity and thereby compute the size of other objects in our image.

      • Jairo October 8, 2017 at 5:03 pm #

        OK thanks for answering, but what happens if the object is a square oh rectangle would be lxl and bxh to know the area in pixels?

        and sorry but I do not understand why you got 150Pixels the size of the object of the coin?
        : D

        • Adrian Rosebrock October 9, 2017 at 12:19 pm #

          150px was an example. It was used to demonstrate how the triangle similarity equation can be used for basic calibration.

  64. Ankit October 9, 2017 at 3:33 am #

    Hi Adrian,

    Is it possible to get this output like dimension of objects which we measured into csv files from disk.

    Thanks!

    • Adrian Rosebrock October 9, 2017 at 12:16 pm #

      A CSV file is just a comma separated list of strings. Open a file pointer and write the coordinates obtained via Lines 68-75 to disk.

  65. Patrick November 7, 2017 at 12:01 am #

    hi Adrian, I have the same thing as pawan mentioned. The measuring only worked on the first object on the left, and not measuring any other object in the pic.

    • Adrian Rosebrock November 9, 2017 at 7:08 am #

      My guess here is that the other objects in the image were not properly detected. Please take a look at the edged image to ensure the contours of the objects are properly revealed. You might need to update the parameters to the Canny edge detector and erode/dilation operations.

      • patrick November 9, 2017 at 11:00 pm #

        I fixed it, it’s just the structure of the code is not right.

  66. ekin November 9, 2017 at 8:26 am #

    if we make a picture over the background, you need help with this issue

  67. aditya gupte November 16, 2017 at 3:41 am #

    what can i do if i have to measure more complex shapes and then make the CAD model of that shape ?

  68. Ahmed Sadman November 25, 2017 at 12:59 am #

    Hello Adrian,
    Thank you for the nice explanation, but I have a problem. I still can’t figure out how the “contourArea” function works, because when I use it, some bigger areas are not detected (but they should be). And when I remove the checking area, the bigger area that was not shown before shows up, including those small noisy areas.

    I tried to print the contourArea and I saw that in many cases, area that looks cleary large in the image has a very small contourArea value, where on the other hand small areas or noisy areas have got bigger contourArea values, this is the thing that is causing problem. Do you think the contourArea function has any fault at measuring the contour areas?

    • Adrian Rosebrock November 25, 2017 at 12:14 pm #

      Hi Ahmed — that is indeed some very strange behavior. To be totally honest I’m not exactly sure what the issue would be. I would have to see the contours that were being correctly marked and then incorrectly marked first.

  69. can December 29, 2017 at 3:38 pm #

    Hey Adrian,

    Thank you for this code, but when I compile, it only show the coin at the right corner. I tried the 3 images but still the same. How can I let it show the sizes of the all of the objects?

    • Adrian Rosebrock December 31, 2017 at 9:46 am #

      That is certainly odd. Try investigating the edge map via cv2.imshow. If there are discontinuities along the outline then cv2.findContours will not be able to extract the coin.

  70. joe January 9, 2018 at 11:43 am #

    Hi Adrian,

    First of all congratulations for your posts they are quite helpful.

    Regarding “the pixel per metric” ratio, assuming that the camera distance is the same, is it applying to the whole image? I mean the objects close to the edges on the camera frame will have the same pixel ratio than the ones are in the middle?

    Thanks.

    • Adrian Rosebrock January 10, 2018 at 12:54 pm #

      Correct, and that is a limitation of this method. We assume a “straight on” view of the object. If the viewing angle changes the distance will change and this method cannot account for that.

      • joe January 12, 2018 at 6:38 am #

        Hi Adrian,
        Thanks for the reply, in order to clarify, would it work fine (the object measurement) if for example in the FIgure 3 we move some of the tablets closer to the frame edges?

        • Adrian Rosebrock January 15, 2018 at 9:33 am #

          Technically yes, but the size measurement would start to get more distorted. This is a simple method for object size measurement. For a more accurate method we would need to calibrate our camera and account for “barrel distortion” caused by the camera lens. The Embedded Vision Alliance has a nice writeup on this.

          • joe January 24, 2018 at 4:16 am #

            Thanks Adrian for the answer,
            I’ve got one more question,
            If we change the camera-objects distance obviously we have to re-calibrate to get the new pixel/mm ratio but, would that ratio be lineal if we change the camera distance? or should we calibrate the ratio for different distances?

          • Adrian Rosebrock January 24, 2018 at 4:58 pm #

            Correct, the relationship would be linear. You can move either the object or the camera but not both. If you move both you would need to recalibrate.

  71. Vasilis February 6, 2018 at 8:33 am #

    Hello Adrian
    Thank you very much for the post.
    I am trying to measure defect sizes of a component from an image taken. I know that the width of the component in mm is the width of the whole image. So will the pixelPermetric will work in the same way as having a reference object?
    Thank you in advance

    • Adrian Rosebrock February 6, 2018 at 10:00 am #

      Yes, provided you know the exact width you can still use the triangle similarity method covered in thsi post.

  72. Gayaa February 10, 2018 at 10:14 pm #

    Hi Adrian,

    As a requirement for a project, I need to find the height(pixel value) of a human object (head to feet). For this I want to find and mark top of the head and bottom of the feet. Then the distance between two points in pixels. Up to now I have done following steps (pre-processing).

    Load the image (human body with T-pose)

    Converted into gray-scale and blur

    Performed edge detection

    Performed dilation+erosion

    Next what should I do for the next step??

  73. Swappart February 19, 2018 at 6:39 pm #

    Is there a way to do this in java?

    • Adrian Rosebrock February 22, 2018 at 9:24 am #

      Yes, but you would need to port the code to Java + OpenCV.

  74. Syed Umaid February 20, 2018 at 11:56 am #

    error: (-215) scn == 3 || scn == 4 in function cvtColor

    This is the error I’ve got !

    • Adrian Rosebrock February 22, 2018 at 9:13 am #

      Double-check the file paths to your input images. The “cv2.imread” function is likely returning “None” implying that the input image could not be found.

  75. Mahmoud Ibrahim February 28, 2018 at 9:32 am #

    Hey Adrian,
    Many thanks for you effort and time building this wonderful website and projects. These samples helped me very much finishing very big part of my project.
    May I ask you about contours and minAreaRect , do they produce perfect rectangle with perfectly equal sides or it’s not a perfect rectangle ? .
    Also if so, what if I need to measure the dimensions of all sides of rectangle to compare the dimension of each side to each other using tr, tl, br, bl points. How can I define these points and then use the dist.euclidean function after ? or there is a better way to do it ?
    sorry I am not professional in coding !
    Many thanks in advance!

    • Adrian Rosebrock March 2, 2018 at 11:00 am #

      It’s not a perfect square. It’s the minimum enclosing bounding box which could be rectangle with non-equal sides.

      • Mahmoud Ibrahim March 4, 2018 at 11:18 am #

        Thanks Adrian Very much ,, then can you please tell me how to measure the length of each side of the bounding box?

        • Adrian Rosebrock March 7, 2018 at 9:34 am #

          Compute the Euclidean distance between each vertex of the rotated rectangle. Apply triangle similarity to obtain the final dimensions.

  76. ADITHYA March 12, 2018 at 1:04 am #

    args = vars(ap.parse_args())
    Hi adrian
    while executing the above line it is not executing and giving error” args is not defined” and we have sucessfully completed the above lines and giving width of image as 0.955 and imported the image sucessfully

    • Adrian Rosebrock March 14, 2018 at 1:11 pm #

      Did you download the source code to this post using the “Downloads” section? Or copy and paste? Make sure you download the code to ensure you didn’t accidentally introduce errors during the copy and paste.

      • ADITHYA March 21, 2018 at 5:54 am #

        hi adrian
        we have successfully downloaded the source code and imported the modules and only that single line is not executing that is
        args = vars(ap.parse_args())
        and the next lines are not executing due this error so please help us in solving this particular problem

        • Adrian Rosebrock March 22, 2018 at 10:02 am #

          You should read up on command line arguments before continuing — that post will resolve your issue. I hope that helps.

  77. Greeshma Satheesh March 12, 2018 at 9:15 am #

    Hi Adrian,
    Is there any way to calculate the area of United States Quarter

    • Adrian Rosebrock March 14, 2018 at 1:03 pm #

      The area of a circle is A = pi * r^2. The radius would be half of the width (or height) provided you are measuring a circle.

  78. Clem Kuns March 12, 2018 at 12:23 pm #

    Hello,

    I have read with interest your blog post regarding “Measuring size of objects in an image”. I am in the initial phases of determining the feasibility of evaluating metrics of an object in a photo. In the perfect world, I want to be able to take 1 or more pictures with a standard phone-camera, upload those photos to a central processing or cloud based server, and then evaluate dimensions, colors, etc. I have some background in aerial imagery, fiducial marks, ground based control, etc. However, doing something on the scale of an object approximately the size of a letter-size sheet of paper is more difficult. The objects i have in mind, I believe it would be feasible to overlay a “calibration card” into the photo. The calibration card could help give real dimension and color to the photo for scale and color rectification? I am looking to bounce some ideas of off an expert in this photogrammetry field if you would be so kind to entertain my thoughts. I understand camera lenses, etc present issues as well.
    thank you!

    • Adrian Rosebrock March 14, 2018 at 1:01 pm #

      If you have a calibration card of some sort that would be ideal. I would also suggest computing the intrinsic/extrinsic parameters of the camera to provide more accurate measurements.

  79. Greeshma Satheesh March 12, 2018 at 10:17 pm #

    Hi Adrian,
    Is there any way to calculate the area of these objects?

    • Adrian Rosebrock March 14, 2018 at 12:53 pm #

      If you know the width and height you can compute the area of the bounding box region.

  80. Kabuki March 14, 2018 at 10:09 am #

    Great work Adrian. is there a way of calculating the weight of an object in a picture. If yes plz how can I get it done.
    thank u.

    • Adrian Rosebrock March 14, 2018 at 12:25 pm #

      This tutorial demonstrates how to compute the width and height so I’m not sure what you are asking.

  81. Gaya March 14, 2018 at 10:39 pm #

    Hi,
    The article is very interesting and useful one. I run the code in PyCharm IDE and following error was occurred.

    usage: object_size.py [-h] -i IMAGE -w WIDTH
    object_size.py: error: the following arguments are required: -i/–image, -w/–width

    Can you please tell me how to solve this?

    • Adrian Rosebrock March 19, 2018 at 5:57 pm #

      Please see this post on command line arguments.

      • Mili July 24, 2018 at 12:24 pm #

        i am suffering from same problem.. command line arg.. plz help me to sort out..

        • Adrian Rosebrock July 25, 2018 at 7:59 am #

          Read the post linked to above, Mili. It will sort you out and help you understand commend line arguments 🙂

          • mili October 31, 2018 at 7:48 am #

            thank you adrian.. 🙂

            need to save this data, height and width to excel file. please help me.

          • Adrian Rosebrock November 2, 2018 at 7:37 am #

            PyImageSearch mainly discusses computer vision and deep learning. Writing simple text data to disk is a basic file I/O operation. I would highly suggest you learn the fundamentals of programming before continuing as computer vision is significantly more advanced. Take the time to invest in yourself now and you’ll be able to build wonderful applications in the future 🙂

  82. Fernando March 27, 2018 at 8:23 pm #

    Hi Adrian,

    I tried to install the imutils after typing:

    soruce ~/.profile
    workon cv
    pip install imutils

    …..but was intsalled in /usr/local/lib/pyhton3.4/dist-packages

    I expected to be installed in python2.7 in order to work with this version.

    Any suggestion ?

    • Adrian Rosebrock March 30, 2018 at 7:26 am #

      Can you share your full terminal commands + output in a GitHub Gist? I’m not sure why it would be installing to the system Python if you are inside the Python virtual environment. Guess is that either the “source” or “workon” command failed.

  83. Aamir March 28, 2018 at 6:12 am #

    Can you list out all the applications of the project in real life.

    • Adrian Rosebrock March 30, 2018 at 7:20 am #

      Use your imagination, give it some thought, and do a bit of research. I am confident you can create your list 🙂

      • Mirtunjay April 2, 2018 at 2:50 am #

        sir , I also have the same doubt, what are the various application of this project

        • zhihzu August 6, 2018 at 10:13 pm #

          it would be use in Site construction acceptance.it can reduce large cost

  84. AliK April 9, 2018 at 9:26 pm #

    Hi Adrian… great tutorial, really informative and helpful… one typo which you might want to correct: in the “pixels per metric” ratio section I think this sentence

    “Thus implying there are approximately 157 pixels per every 0.955 inches in our image…”

    was meant to be

    “Thus…157 pixels per inch in our image”..if there are 150px in 0.955inch, then there are 157px in 1 inch…it is not a big deal though 🙂

  85. TS May 5, 2018 at 11:54 am #

    Hi Adrian, it is a really a great tutorial. I am doing hand measurement for my project. I want to measure every single fingers’ measurement, palm breadth and palm length(from the middle bottom of index finger to the end of the palm) based on image. But, I don’t know how to locate the exact points, for example, on top of the every fingers and their bottom point at the middle of the line of the intersection between palm and fingers. Can you give me some guidelines and reference how to locate the points?

    • Adrian Rosebrock May 9, 2018 at 10:22 am #

      You’ll want to take a look at contour properties, including convexity defects and the convex hull. I have a lesson inside the PyImageSearch Gurus course that demonstrates how to locate each of these regions along the hand. I hope that helps point you in the right direction or at the very least gives you additional terms to Google and research.

  86. Artemii May 25, 2018 at 1:12 am #

    Hi, Adrian, thank you for sharing more and more of such a useful ideas!

    After handling with an implementation of your model, but in real time, I have been thinking about making it more robustness as an object detector.
    Some ideas popped out, I’m sharing with you and asking for your opinion, thank you in advance for your time!

    1. As you said a reference object has to be ‘easy-to-see’, what do you think about implementing ArUco Markers as so, maybe in this case we could get rid of the ‘top-left order’ function?
    2. While moving objects, numbers (obviously) get changed. Can we someways keep track of it? Extended Kalman filtering? Tracking function?
    3. While working with real-time recognition, boundingRect() causes flickering, is there any way to control it, please, any tips?

    • Adrian Rosebrock May 25, 2018 at 5:45 am #

      Thanks Artemii, I’m glad you enjoyed the tutorial. And congrats on porting it to a real-time implementation. To address your questions:

      1. Yes, you could use ArUco markers here. You could also train your own custom object detector if you wished. As long as you know the measurements of those markers beforehand.

      2. Take a look at correlation filters. They are very useful for tracking.

      3. I don’t think the bounding rectangle function itself is causing the flickering, it’s more likely that your method used to detect the marker is not detecting the marker in certain frames. You may want to double-check that function.

  87. Tiago de Oliveira May 30, 2018 at 12:59 pm #

    Hello Adrian, your tutorials is really great, thank you for sharing.

    I have a question, i’m brazilian, and there is almost no references about opencv.
    I have some knowledge in english but i’m not fluent, so it’s little hard.
    I’m doing my final paper, and i want to do, with raspberry pi3 and picamera, monitor the growth of a plant in size, so,
    I’m thinking of taking a picture every day of the plant and analyzing its size and making a graph that represents its growth.
    Could you direct me to something that would help me? I’m a beginner in python and raspberry.

    Thank you, and sorry for bad english.

    • Adrian Rosebrock May 31, 2018 at 5:03 am #

      There are a few ways to approach this but the two key components are:

      1. Create a script/cronjob that snaps a photo every N hours (or days)
      2. Create a script that can analyze the size of the plant

      Since your camera will be fixed and non-moving ideally some basic background subtraction between the subsequent frames will give the differences between the two plants. From there you could measure it. If you’re new to Python and OpenCV I would recommend reading through my introductory book, Practical Python and OpenCV. This book will help teach you the fundamentals of OpenCV and enable you to better approach this project. From there you can apply the background subtraction technique from this blog post.

      • Timothy Malche May 31, 2018 at 12:10 pm #

        do you have a tutorial to analyze the size of the plant or any object in image?

        • Adrian Rosebrock June 5, 2018 at 8:36 am #

          This blog post discusses how to measure the size of an object in an image. If you want to measure the size of a plant you’ll want to determine how you are going to detect it, either via:

          1. Basic image processing operations
          2. Machine learning
          3. Segmentation
          4. Object detection

  88. Timothy Malche May 31, 2018 at 8:11 am #

    Hi Adrian, thanks for great tutorial. Is there some changes needed to get accurate result for images taken from 180 degree.

    • Adrian Rosebrock June 5, 2018 at 8:38 am #

      Do you have an example image of what you are working with?

  89. Liu Nado June 1, 2018 at 2:58 pm #

    Hello Adrian

    I am new in this computer vision (and Programming in general!). I came from Agricultural Engineering, and the only experience I have in programming is Visual Basic that I studied as part of the curriculum for mere three months.

    I came here to finish my undergraduate thesis regarding measuring weight of cattle using smartphone camera (by analyzing their chest size, thus I need to know how to measure the dimension).

    Before I can get into that project, my teacher decided to challenge me by telling me to create a program to determine an object’s area that captured using camera. He told me that I can use any kind of programming language. Currently, it’s just a simple box, which mean it’s just length x width. He told me “it should be simple, if you can’t do it then you can’t proceed to your thesis”. The challenge here is he want me to get the area regardless of camera distance to object. So, if I capture the image from 1 meter or 3 meter, the result will always the same.

    Okay enough with my background. I google on how to do it, and I found this tutorial. Thank God!
    But now I am stuck looking at this page for hours, without knowing what exactly I need to do. As others might say, I do not know what I do not know.

    Will you help me by answering some of these questions ?

    1. What should I do first? I know I should study hard, but starting from?
    For your info, I have Windows 10. I already install Python and Sublime Text 3. Should I do all of these Python programming in Linux instead? Assume I am a complete newbie in programming, all that I have is ability to learn, capability to understand English, computer, 24H internet, my brain and my willpower.

    2. Is there any additional references for me to study computer vision, python, etc?

    3. OOT, The end goal of my undergraduate thesis is to create an Android app that is capable to measure weight of cattle using camera. FYI, I have not learn Java and Android Studio. In your honest opinion, by implementing technique here plus studying Android and its stuffs, do you think an average guy can finish it in 4 months? I am highly motivated enough to do impossible stuff, but I want rationale opinion. None of my friends are expert in this matter.

    Right now I feel completely lost, but I must do this (you may wonder why agriculture degree decided to do programming? It is common nowadays that some of students here decided to learn programming, whether for fun, business, or academic purpose ; regardless of their department)

    I am sorry to write this wall of text. One thing for sure, I am 100% ready to learn, I just need guidance on what should I do first, especially with tons of materials and knowledge available all over the internet. No matter how difficult it is, I will do it.

    Thank you and sorry for my bad english

    P.S : I already subscribe to your email course

    • Adrian Rosebrock June 5, 2018 at 8:20 am #

      Congrats on starting your project. But if you are new to programming your first step would be to learn how to program. I would recommend learning how to write code in Python as you’ll be using Python quite a bit with computer vision. The RealPython course would be a good one for you to get up to speed.

      Secondly, you’ll want to work through my book, Practical Python and OpenCV. This book explains, line by line, what each line of code and what each function are doing. It’s by far the fastest way to get up to speed with the OpenCV library and learn the fundamentals of computer vision and image processing. In fact, many readers have used the book to learn both Python and OpenCV at the same time!

      Take your time learning Python and OpenCV. It won’t be easy if you are new to writing code but if you practice each day I’m confident you can do it.

  90. Brian Tremaine June 1, 2018 at 3:07 pm #

    Hello Adrian,

    This is a very useful tutorial for me as I am working on a course project that involves identifying objects (coins) and I am doing so using size. I have seen the same issue that measurements of the same coin type are not equal.

    First-off I need to get a stable hands-off setup with a tripod that looks at the center of my flat object surface at 90 deg. After that I am considering using a script for camera calibration. I have a question on this however, even if the calibration is perfect won’t a coin at the image center measure differently from a coin at the edge of the field-of-view just due to the perspective?
    The size difference between a dime and a penny are not that great 17.91 mm versus 19.05 mm diameter so the accuracy of measurement needs to be within +/-0.57 mm. Currently my camera is about 15 inches above the surface and the field-of-view is about 45 deg so center to image edge is about 6 in in my case.

    Thank you for all your great blogs,

    Brian

    • Adrian Rosebrock June 5, 2018 at 8:17 am #

      There are a few ways to approach this problem, but yes, a full blown intrinsic/extrinsic camera calibration will give you the best measurement in accuracy here. That said, you’ll also want to make sure you can segment the coins from the background. If you cannot segment the coins you will not be able to perform the measurement.

  91. Ramesh June 2, 2018 at 6:17 am #

    I see 2 error as below:

    1. pixels_per_metric = 150px / 0.955in = 157px
    Here the unit of the result is px/inch, not just px.

    2. 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.
    Here it is 157 pixels per every 1 inch not 0.955 inches.

  92. fawad June 13, 2018 at 2:33 am #

    is this tutorial works in matlab?

    • Adrian Rosebrock June 13, 2018 at 5:24 am #

      The code for this tutorial is written in Python. You would need to translate it to MATLAB.

  93. Nantha June 21, 2018 at 12:43 am #

    hi, can you help me i have filtering image from front-end to measure the human body can use this way to finish this measurement.

    • Adrian Rosebrock June 21, 2018 at 5:35 am #

      There are a few ways to approach this, but if you want a super accurate method, you might want to consider a segmentation network, like UNet or Mask R-CNN to first extract a person, and ideally with a calibrated camera. From there you could make your measurements.

  94. ahsan July 2, 2018 at 11:41 pm #

    hello sir, can u help me.?
    how many distance, image to camera in your sample.?

    thanks

    • Adrian Rosebrock July 3, 2018 at 7:17 am #

      I did not measure the distance from my camera to the objects. Perhaps you are are referring to this tutorial?

      • damar July 4, 2018 at 1:33 am #

        hello sir. thanks for the reply. my friend, ahsan. he want ask the distance between object n camera. is that any certain distance?

        • Adrian Rosebrock July 5, 2018 at 6:38 am #

          As I mentioned in my first reply, I do not know the distance between the object and the camera for this blog post.

  95. Rudrani Angira July 7, 2018 at 9:50 pm #

    Hi Adrian

    I have python 2.7.13. And when i run object_size.py and distance_between.py , the program just stops after bounding box for the quarter. I never see the output for other objects. Can you suggest what I might be doing wrong.

    • Adrian Rosebrock July 10, 2018 at 8:40 am #

      You need to click the window opened by OpenCV then press any key on your keyboard to advance the execution of the script.

  96. Ferdows Shahryar July 23, 2018 at 4:58 am #

    Hi Adrian thanks again for the nice tutorial, but I have a question:
    I want to do the same thing in a car detection, where I want to find the length of car or object from a real time cam, there I have no reference object, do you know how do handle it?
    I would appreciate your job and thank in advance.

    • Adrian Rosebrock July 25, 2018 at 8:17 am #

      You will need a reference object in some capacity, whether that’s using the method here or computing the intrinsic/extrinsic parameters of the camera, likely during a “checkerboard calibration”.

  97. chahrazad Essalim July 26, 2018 at 11:37 pm #

    Hello Adrian, thanks for the great tutorial. I would like to know your opinion about solving the object size prediction problem using deep learning, I am not able to find any research papers or projects solving a similar problem using DL.

    • Adrian Rosebrock July 31, 2018 at 12:07 pm #

      Just because you can solve a problem using DL doesn’t mean you have to, on in this case, should. The most accurate measurements are going to come from calibrated cameras and multi-view geometry.

  98. PAVLINB August 2, 2018 at 2:34 am #

    Great tutorial and precise work.

    Have you thought about a version for mobiles? iOS especially?

    Kind regards.

    • Adrian Rosebrock August 2, 2018 at 9:21 am #

      Thanks, I’m glad you enjoyed the tutorial! I don’t have any plans to create a mobile version of this guide but provided you understand the function calls in this post you can translate the code to OpenCV on whatever mobile platform you are using.

  99. JD August 6, 2018 at 12:12 pm #

    Thanks Adrian for the great Tutorial,

    this code is helping me a lot. I am new to opencv and python in general.

    Is there a way to have this code run through numerous pictures at once rather than do each picture individually?

    Kind regards

    • Adrian Rosebrock August 7, 2018 at 6:38 am #

      I’m not sure what you mean by “run through numerous pictures at once”. Could you clarify?

      Additionally, if you are new to OpenCV and Python I would recommending working through my introductory book, Practical Python and OpenCV. This book will teach you the fundamentals of OpenCV and better prepare you for the more advanced tutorials here on PyImageSearch. Be sure to take a look!

      • JD August 7, 2018 at 11:04 am #

        in your example_01, you have the code run through only the image ‘example_01.png’.

        I have a folder which has 93 images in it.
        Is there a way to get the code running all of them at once?

        For further info:
        I have changed the code slightly, so that it gives me the measured size in a text document. So it would be handy if I could have all images run through the code at once rather than do them one by one. This way I could have the text document have all the text data I need and I can then see in which picture there could be a measurement error or mishap

        • Adrian Rosebrock August 9, 2018 at 3:07 pm #

          Yes, you can use the “list_images” function in the imutils library to supply your directory of 93 images and loop over them.

      • JD August 8, 2018 at 9:48 am #

        I was wondering if instead of your example:

        python object_size.py –image images/example_01.png –width 0.955

        where you only go check the one image ‘example_01.png’, if you could for example check many pictures from a folder all at once.

        python object_size.py –image images/examplefolder/*.png –width 0.955

        • Adrian Rosebrock August 9, 2018 at 2:53 pm #

          I would suggest modifying the code to either:

          1. Accept wildcards
          2. Or simply use the list_images function in the imutils library. An example of which can be found here.

  100. Abdur Rehman August 12, 2018 at 4:16 am #

    Can we detect object length/width by placing object on a graph paper?I am trying to calculate leaf length and width in an image.Suggest some image processing techniques.Plz

    • Adrian Rosebrock August 15, 2018 at 9:01 am #

      Yes you can, but you would need to know:

      1. The size (in terms of inches or millimeters) of each box in the graph paper or the size of the graph paper itself
      2. Be able to detect each of the cells on the graph paper

      From there you can compute the ratio of the number of cells on the paper to the number of cells the object takes up.

  101. Angel August 12, 2018 at 3:24 pm #

    Hello Adrian.
    Thank you for this guide, it is very helpful. I am taking my first steps with openCV and this example of measuring items in a picture is actually what I need…
    Is there a similar example in Java for such a use of openCV ?
    I could not find anything similar to what you have published with Java, must examples I could find for openCV are written with Python.
    Thank you,
    Angel

    • Adrian Rosebrock August 15, 2018 at 8:58 am #

      Hey Angel — OpenCV bindings do exist for the Java programming language, but you would need to port the code from Python to Java.

  102. Babu Reddy August 22, 2018 at 9:19 pm #

    If I have a image of a web page taken from a 5mp phone camera, how do I get the coordinates, color, width, height and any other attributes of an HTML object in that image.

    • Adrian Rosebrock August 24, 2018 at 8:48 am #

      I’m curious why you would be using a phone camera? Why not just load the webpage and then inspect the computed element attributes using the HTML and CSS?

  103. Abdur Rehman August 23, 2018 at 9:48 am #

    Can we achieve the same with OpenCV only.I am working on android.

    • Adrian Rosebrock August 24, 2018 at 8:39 am #

      Yes. You will need to:

      1. Implement my “imutils” function in Java + OpenCV
      2. Implement the distance function in Java which is also easy

  104. Krb August 24, 2018 at 10:54 am #

    Hello ! Thank you very much for this !

    I have a question, please, how I can see all rectangles and sizes in one time without press ‘Enter’ for each object ?

    And so, save the global resulting image ?

    Thx 🙂

    • Adrian Rosebrock August 30, 2018 at 9:43 am #

      Move these lines to the bottom of the Python script:

      Additionally, you can use the cv2.imwrite function to write images to disk.

      If you’re new to working with OpenCV I would recommend you read through Practical Python and OpenCV so you can learn the basics first.

  105. Niranjan A August 31, 2018 at 6:28 am #

    Thanks for your wonderful article Adrian. The explanation is so clean and well explained.

    • Adrian Rosebrock September 5, 2018 at 9:23 am #

      Thank you Niranjan, I really appreciate your kind words 🙂

  106. Mahdyar September 5, 2018 at 4:27 pm #

    Hello
    Thanks for this great article it really helped me out.
    How can I crop each object so it has its own image? What I mean is I want every single object that is detected cropped so nothing else is visible. What code should I write?
    Thank you very much

    • Adrian Rosebrock September 11, 2018 at 8:54 am #

      Do you want to crop out each object and then save the object to disk? Or are you trying to have a single image with only the objects themselves visible?

  107. erol September 7, 2018 at 4:17 am #

    Hi Adrian
    I want to measure the millimetre radius of the object in real time, but I don’t know where to start. I ran your sample project, but I couldn’t calculate it as milimetric. And also how do I run the webcam on Windows? Thanks in advance

  108. Baptiste September 13, 2018 at 3:04 am #

    Hi,

    Thanks for your script.
    I run the script and once object is detected.
    How display the size of all objects?

    Thanks.

    • Adrian Rosebrock September 14, 2018 at 9:39 am #

      Click on the window opened by OpenCV and then press any key on your keyboard to advance the execution of the script.

  109. Craig Day September 17, 2018 at 12:24 am #

    Hi Adrian, I have posted a question on stackoverflow: https://stackoverflow.com/q/52360556/7322816
    I was wondering if you can take a look at let me know if you have any ideas?
    Thanks

  110. Prathik Anand September 18, 2018 at 2:12 pm #

    Hi…..soo i actually wanted to determine the actual size of the object from an image…using this :-
    object size in image = Object size * focal length / object distance from camera
    can we find out the real size of objects using this equation given that i know the object distance from the camera and focal length….
    can we get the object size in image in any units of length instead of pixels ?? if so…how can we do that ??

  111. Syed September 19, 2018 at 1:03 am #

    Hi Adrian, I see that you extracted the contour information on line 35, then sort it on line 39 to go left to right direction. The cv2.findContours also outputs the hierarchy information (if using cv2.RETR_TREE). If I would like to sort the contour information like you did on line 39 together with the hierarchy information, how do I do that?

  112. Sayantika Banik September 23, 2018 at 2:16 pm #

    Can I contribute to the above problem statement ?

    • Adrian Rosebrock October 8, 2018 at 12:57 pm #

      I’m not sure what you mean, Sayantika. Could you clarify?

  113. Snehal October 3, 2018 at 4:05 am #

    hi Andrian,
    great work. .
    Is there any way to identify the distance of camera from the image taken? is there any way to identify the focal length from the image only?

    • Adrian Rosebrock October 8, 2018 at 10:26 am #

      You mean something like this post?

      • Simon October 11, 2018 at 5:14 am #

        Hello Adrian

        Amazing walk though and description of the script – especially helpfull since im fairly new with python programming at this “level”.
        Atm. im working on a project that takes base in the operations preformed in this tutorial – however i got a question regarding to the bounding box that are used to find the bounds of the contours to get the lengths of the objects.

        For example if im interested in finding the “precise” area of the object on the image and the square is a 15×15 but somewhere along the object there are a “bump”, an indentation on the object or the lines on the object was not in a perfect 90 degree angle leading to some small variances in width from top left corner to bottom left corner.
        Is there anyway the box operation could be made to take more “points” into consideration when making the measurement or any other solution/function that would be better suited.

        I hope that you understand my question and thanks in advance.
        -Simon

        • Adrian Rosebrock October 12, 2018 at 9:07 am #

          It sounds like you may be interested in a far more accurate measurement than what this code will give you. I would suggest looking into more advanced forms of camera calibration, including computing the intrinsic and extrinsic parameters of the camera.

          • Simon October 15, 2018 at 3:41 am #

            I will look into the camera parameters. However i tried the code from the example here and i got more then decent results ( around 1 cm off the real size, sometimes even smaller) leading me to the though that it if i could take multiple midpoints and sum over the object instead that it would provide me with a better result. My hope is to estimate the size within 1mm of error. and so far i have gotten it down to 4mm with this code and smaller corrections and modifications in camera position etc.

            If it makes it easier – im more to happy to take by email ?

            – Simon

  114. George October 9, 2018 at 6:33 am #

    Hi Andrian,
    Thanks for this lucid and nice tutorial.
    Could you suggest a way to measure the height of a human from monocular images using a reference object such as a credit card, whose size is known? . I assume a similar approach used in this tutorial could be used for that purpose. No?

    • Adrian Rosebrock October 12, 2018 at 9:32 am #

      It is a very similar approach to this tutorial. You would want to apply an object detector to detect the bounding box of the person first, then, once you have the bounding box, you can use your reference object to compute the height. This tutorial will help you get started on the person detection component.

  115. George October 9, 2018 at 6:38 am #

    Dear Adrian,

    Sincere apologies for mis-spelling your name.

  116. Colton Neary October 10, 2018 at 1:23 pm #

    I have tried following this post several times to get particle sizing on an image for my company. The sample that I am taking an image of has small particles (millimeter sizing) What would you recommend to get this accurately grab individual beads instead of hundreds at a time? I can email pictures of the output if that would help clarify what is going one. Thanks.

    • Adrian Rosebrock October 12, 2018 at 9:12 am #

      Hey Colton, without seeing example images of what you’re working with it’s hard to provide an exact suggestion, but my guess is that you’ll need a more accurate camera calibration for this. Feel free to email in the image though and I’ll try to take a look.

  117. Saad Naik October 11, 2018 at 9:38 am #

    How are you computing object width in pixels directly? Please reply asap .TY

    • Adrian Rosebrock October 12, 2018 at 9:01 am #

      Hey Saad — this blog post actually discusses how I’m converting pixels to actual measurable units. Is there a particular aspect of the post that is confusing you?

  118. SEB BAGHERNEZHAD October 11, 2018 at 7:16 pm #

    Hi,tank you Adrian
    I have a very big size object (Area rug) in camera and can not scroll window. how can scroll or zoom ?

    • Adrian Rosebrock October 12, 2018 at 8:54 am #

      OpenCV’s GUI functionality is very basic and meant only for debugging. You cannot zoom or scroll using OpenCV’s GUI functions.

  119. Kukuh Sulistyo October 17, 2018 at 10:42 pm #

    Hi, Adrian. Thank you for writing this great article!

    I have question, how to measure width and length of nonsymmetric object (e.g. corn kernel)? Thanks.

    • Adrian Rosebrock October 20, 2018 at 7:50 am #

      That’s a much more challenging problem. I would start by asking how exactly do people measure the dimensions of a kernel of corn? What is the “standard”? Once you know the answer to that question, then you can start considering how to create a computer vision application to perform the measurement.

  120. Andrew October 21, 2018 at 9:20 pm #

    Hi Adrian,

    I’m looking to do something similar with a bit more precision in order to measure the length and diameter of screws. How would you approach this problem?
    Thanks

    • Adrian Rosebrock October 22, 2018 at 7:53 am #

      I would suggest looking into the intrinsic and extrinsic camera parameters which will allow you to obtain a much more accurate calibration.

  121. Malika Bhatt October 23, 2018 at 7:51 am #

    Dear Adrian,

    I am working on a project in which we need to input this height and width into another system which is already have been developed. How can i save this data I.e. height and width both into a file. Like text file or excel. How could I perform it? Plz let know..

    Thanks in advance.

    • Adrian Rosebrock October 29, 2018 at 2:11 pm #

      I would suggest looking into a CSV file which could naturally be read by Excel.

  122. Jerol October 29, 2018 at 8:11 am #

    Hi Adrian,

    Thank you for sharing this awesome tutorial.
    But I was wondering how to deal when the objects are placed closely and sometimes overlayed to one another.
    Any input on that?

    Look forward to hearing!

    • Adrian Rosebrock October 29, 2018 at 1:07 pm #

      That is significantly more challenging. If the objects partially overlap you’ll want to look into instance segmentation algorithms which will help you estimate the center of the object.

  123. Sandeep S October 29, 2018 at 12:25 pm #

    Hi Adrian,

    I have problem in which I have to measure the size of eye-pupil. Using haar cascade I was able to get the box co-ordinate of eye with which I found the centre position of eye, but how to exactly find the size of pupil? Could you help me out?

    • Adrian Rosebrock October 29, 2018 at 1:01 pm #

      You should use facial landmarks rather than Haar cascades to localize the actual eye regions. From there you can localize the pupils.

  124. Sandeep S October 30, 2018 at 4:36 am #

    Hi adrain,
    Thanks a lot for the suggestion, I tried using facial landmarks approach and was able to localise the iris region. If I want to go further towards pupil region what can I do, could you help me with some suggestions?

    • Adrian Rosebrock November 2, 2018 at 8:31 am #

      I personally haven’t tried this tutorial but I know other PyImageSearch readers have had good luck with it. That would be my primary suggestion.

  125. Suresh Doraiswamy October 30, 2018 at 9:17 am #

    Superb Tutorial Adrian. Thanks very much. I continue to learn. It is almost becoming an Art.
    In addition, the comments and questions teach me a lot more. God bless y’all.

    • Adrian Rosebrock November 2, 2018 at 8:21 am #

      Thank you Suresh! I’m so happy you are developing your computer vision skills. Keep it up!

  126. Leonardo Godói November 5, 2018 at 12:57 pm #

    Hello! Very nice job!

    I suppose it does not work for perspective 3D (find dimensions of a box, for example). Am I right? If yes, could you mentiony alternative for that?

    Thank you!

    • Adrian Rosebrock November 6, 2018 at 1:13 pm #

      Correct, this method will not work for 3D and finding the dimensions of a box. I don’t have any tutorials for that specific use case but I will consider it for the future.

  127. Bashi November 14, 2018 at 4:54 am #

    Hi, is this to possible to find out the dimension of tyre from a vehicle’s image using this script ? ie. i doesn’t want to find the entire object’s size but some part of object .

    Thank you,

    • Adrian Rosebrock November 15, 2018 at 12:07 pm #

      Yes, you could use this method but you would want to train an actual tire detector first. You won’t be able to use simple image processing to reliably find a tire in an image.

  128. Bashi November 20, 2018 at 11:20 pm #

    Thank you Adrian.

    • Adrian Rosebrock November 21, 2018 at 9:25 am #

      You are welcome, Bashi!

  129. ali November 21, 2018 at 3:58 am #

    Hi Adrian. I take pictures of one person and put a reference object around it. But it does not calculate correctly. what’s the reason?

    • Adrian Rosebrock November 21, 2018 at 9:21 am #

      Can you be a bit more specific regarding “but it does not calculate properly”? What is incorrect regarding the calculation?

      • ali November 24, 2018 at 1:09 am #

        excuse me,
        I want to get human height and width from image. There is no reference object in the original image. Can I manually put a reference object in the image? While doing this, but do not calculate the height and width of the person properly.
        thanks a lot

        • Adrian Rosebrock November 25, 2018 at 9:05 am #

          You will need to perform some sort of calibration, whether be a reference object or explicitly computing the intrinsic and extrinsic parameters of the camera. Some form of calibration is required.

  130. laurence December 11, 2018 at 12:29 pm #

    Great stuff!
    Reading carefully I might have found a typo.
    “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. ”

    should be “there are approximately 157 pixels per every inch” 🙂

Leave a Reply