Last updated on July 8, 2021.
A couple of days ago, Cameron, a PyImageSearch reader emailed in and asked about methods to find the distance from a camera to an object/marker in an image. He had spent some time researching, but hadn’t found an implementation.
I knew exactly how Cameron felt. Years ago I was working on a small project to analyze the movement of a baseball as it left the pitcher’s hand and headed for home plate.
Using motion analysis and trajectory-based tracking I was able to find/estimate the ball location in the frame of the video. And since a baseball has a known size, I was also able to estimate the distance to home plate.
It was an interesting project to work on, although the system was not as accurate as I wanted it to be — the “motion blur” of the ball moving so fast made it hard to obtain highly accurate estimates.
My project was definitely an “outlier” situation, but in general, determining the distance from a camera to a marker is actually a very well studied problem in the computer vision/image processing space. You can find techniques that are very straightforward and succinct like the triangle similarity. And you can find methods that are complex (albeit, more accurate) using the intrinsic parameters of the camera model.
In this blog post I’ll show you how Cameron and I came up with a solution to compute the distance from our camera to a known object or marker.
Definitely give this post a read — you won’t want to miss it!
- Update July 2021: Added three new sections. The first section covers improving distance measurement with camera calibration. The second section discusses stereo vision and depth cameras to measure distance. And the final section briefly mentions how LiDAR can be used with camera data to provide highly accurate distance measurements.
Looking for the source code to this post?
Jump Right To The Downloads SectionTriangle Similarity for Object/Marker to Camera Distance
In order to determine the distance from our camera to a known object or marker, we are going to utilize triangle similarity.
The triangle similarity goes something like this: Let’s say we have a marker or object with a known width W. We then place this marker some distance D from our camera. We take a picture of our object using our camera and then measure the apparent width in pixels P. This allows us to derive the perceived focal length F of our camera:
F = (P x D) / W
For example, let’s say I place a standard piece of 8.5 x 11in piece of paper (horizontally; W = 11) D = 24 inches in front of my camera and take a photo. When I measure the width of the piece of paper in the image, I notice that the perceived width of the paper is P = 248 pixels.
My focal length F is then:
F = (248px x 24in) / 11in = 543.45
As I continue to move my camera both closer and farther away from the object/marker, I can apply the triangle similarity to determine the distance of the object to the camera:
D’ = (W x F) / P
Again, to make this more concrete, let’s say I move my camera 3 ft (or 36 inches) away from my marker and take a photo of the same piece of paper. Through automatic image processing I am able to determine that the perceived width of the piece of paper is now 170 pixels. Plugging this into the equation we now get:
D’ = (11in x 543.45) / 170 = 35in
Or roughly 36 inches, which is 3 feet.
Note: When I captured the photos for this example my tape measure had a bit of slack in it and thus the results are off by roughly 1 inch. Furthermore, I also captured the photos hastily and not 100% on top of the feet markers on the tape measure, which added to the 1 inch error. That all said, the triangle similarity still holds and you can use this method to compute the distance from an object or marker to your camera quite easily.
Make sense now?
Awesome. Let’s move into some code to see how finding the distance from your camera to an object or marker is done using Python, OpenCV, and image processing and computer vision techniques.
Finding the distance from your camera to object/marker using Python and OpenCV
Let’s go ahead and get this project started. Open up a new file, name it distance_to_camera.py
, and we’ll get to work:
# import the necessary packages from imutils import paths import numpy as np import imutils import cv2 def find_marker(image): # convert the image to grayscale, blur it, and detect edges gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 35, 125) # find the contours in the edged image and keep the largest one; # we'll assume that this is our piece of paper in the image cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key = cv2.contourArea) # compute the bounding box of the of the paper region and return it return cv2.minAreaRect(c)
The first thing we’ll do is import our necessary packages (Lines 2-5). We’ll use paths
from imutils
to load the available images in a directory. We’ll use NumPy for numerical processing and cv2
for our OpenCV bindings.
From there we define our find_marker
function. This function accepts a single argument, image
, and is meant to be utilized to find the object we want to compute the distance to.
In this case we are using a standard piece of 8.5 x 11 inch piece of paper as our marker.
Our first task is to now find this piece of paper in the image.
To to do this, we’ll convert the image to grayscale, blur it slightly to remove high frequency noise, and apply edge detection on Lines 9-11.
After applying these steps our image should look something like this:
As you can see, the edges of our marker (the piece of paper) have clearly been reveled. Now all we need to do is find the contour (i.e. outline) that represents the piece of paper.
We find our marker on Lines 15 and 16 by using the cv2.findContours
function (taking care to handle different OpenCV versions) and then determining the contour with the largest area on Line 17.
We are making the assumption that the contour with the largest area is our piece of paper. This assumption works for this particular example, but in reality finding the marker in an image is highly application specific.
In our example, simple edge detection and finding the largest contour works well. We could also make this example more robust by applying contour approximation, discarding any contours that do not have 4 points (since a piece of paper is a rectangle and thus has 4 points), and then finding the largest 4-point contour.
Note: More on this methodology can be found in this post on building a kick-ass mobile document scanner.
Other alternatives to finding markers in images is to utilize color, such that the color of the marker is substantially different from the rest of the scene in the image. You could also apply methods like keypoint detection, local invariant descriptors, and keypoint matching to find markers; however, these approaches are outside the scope of this article and are again, highly application specific.
Anyway, now that we have the contour that corresponds to our marker, we return the bounding box which contains the (x, y)-coordinates and width and height of the box (in pixels) to the calling function on Line 20.
Let’s also quickly to define a function that computes the distance to an object using the triangle similarity detailed above:
def distance_to_camera(knownWidth, focalLength, perWidth): # compute and return the distance from the maker to the camera return (knownWidth * focalLength) / perWidth
This function takes a knownWidth
of the marker, a computed focalLength
, and perceived width of an object in an image (measured in pixels), and applies the triangle similarity detailed above to compute the actual distance to the object.
To see how we utilize these functions, continue reading:
# initialize the known distance from the camera to the object, which # in this case is 24 inches KNOWN_DISTANCE = 24.0 # initialize the known object width, which in this case, the piece of # paper is 12 inches wide KNOWN_WIDTH = 11.0 # load the furst image that contains an object that is KNOWN TO BE 2 feet # from our camera, then find the paper marker in the image, and initialize # the focal length image = cv2.imread("images/2ft.png") marker = find_marker(image) focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
The first step to finding the distance to an object or marker in an image is to calibrate and compute the focal length. To do this, we need to know:
- The distance of the camera from an object.
- The width (in units such as inches, meters, etc.) of this object. Note: The height could also be utilized, but this example simply uses the width.
Let’s also take a second and mention that what we are doing is not true camera calibration. True camera calibration involves the intrinsic parameters of the camera, which you can read more on here.
On Line 28 we initialize our known KNOWN_DISTANCE
from the camera to our object to be 24 inches. And on Line 32 we initialize the KNOWN_WIDTH
of the object to be 11 inches (i.e. a standard 8.5 x 11 inch piece of paper laid out horizontally).
The next step is important: it’s our simple calibration step.
We load the first image off disk on Line 37 — we’ll be using this image as our calibration image.
Once the image is loaded, we find the piece of paper in the image on Line 38, and then compute our focalLength
on Line 39 using the triangle similarity.
Now that we have “calibrated” our system and have the focalLength
, we can compute the distance from our camera to our marker in subsequent images quite easily.
Let’s see how this is done:
# loop over the images for imagePath in sorted(paths.list_images("images")): # load the image, find the marker in the image, then compute the # distance to the marker from the camera image = cv2.imread(imagePath) marker = find_marker(image) inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0]) # draw a bounding box around the image and display it box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker) box = np.int0(box) cv2.drawContours(image, [box], -1, (0, 255, 0), 2) cv2.putText(image, "%.2fft" % (inches / 12), (image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 3) cv2.imshow("image", image) cv2.waitKey(0)
We start looping over our image paths on Line 42.
Then, for each image in the list, we load the image off disk on Line 45, find the marker in the image on Line 46, and then compute the distance of the object to the camera on Line 47.
From there, we simply draw the bounding box around our marker and display the distance on Lines 50-57 (the boxPoints
are calculated on Line 50 taking care to handle different OpenCV versions).
Results
To see our script in action, open up a terminal, navigate to your code directory, and execute the following command:
$ python distance_to_camera.py
If all goes well you should first see the results of 2ft.png
, which is the image we use to “calibrate” our system and compute our initial focalLength
:
From the above image we can see that our focal length is properly determined and the distance to the piece of paper is 2 feet, per the KNOWN_DISTANCE
and KNOWN_WIDTH
variables in the code.
Now that we have our focal length, we can compute the distance to our marker in subsequent images:
In above example our camera is now approximate 3 feet from the marker.
Let’s try moving back another foot:
Again, it’s important to note that when I captured the photos for this example I did so hastily and left too much slack in the tape measure. Furthermore, I also did not ensure my camera was 100% lined up on the foot markers, so again, there is roughly 1 inch error in these examples.
That all said, the triangle similarity approach detailed in this article will still work and allow you to find the distance from an object or marker in an image to your camera.
Improving distance measurement with camera calibration
In order to perform distance measurement, we first need to calibrate our system. In this post, we used a simple “pixels per metric” technique.
However, better accuracy can be obtained by performing a proper camera calibration by computing the extrinsic and intrinsic parameters:
- Extrinsic parameters are rotation and translation matrices used to convert something from the world frame to the camera frame
- Intrinsic parameters are the internal camera parameters, such as the focal length, to convert that information into a pixel
The most common way is to perform a checkerboard camera calibration using OpenCV. Doing so will remove radial distortion and tangential distortion, both of which impact the output image, and therefore the output measurement of objects in the image.
Here are some resources to help you get started with camera calibration:
- Understanding Lens Distortion
- Camera Calibration using OpenCV
- Camera Calibration (official OpenCV documentation)
Stereo vision and depth cameras for distance measurement
As humans, we take having two eyes for granted. Even if we lost an eye in an accident we could still see and perceive the world around us.
However, with only one eye we lose important information — depth.
Depth perception gives us the perceived distance from where we stand to the object in front of us. In order to perceive depth, we need two eyes.
Most cameras on the other hand only have one eye, which is why it’s so challenging to obtain depth information using a standard 2D camera (although you can use deep learning models to attempt to “learn depth” from a 2D image).
You can create a stereo vision system capable of computing depth information using two cameras, such as USB webcams:
- Introduction to Epipolar Geometry and Stereo Vision
- Making A Low-Cost Stereo Camera Using OpenCV
- Depth perception using stereo camera (Python/C++)
Or, you could use specific hardware that has depth cameras built in, such as OpenCV’s AI Kit (OAK-D).
LiDAR for depth information
For more accurate depth information you should consider using LiDAR. LiDAR uses light sensors to measure the distance between the sensor and any object(s) in front of it.
LiDAR is especially popular in self-driving cars where a camera is simply not enough.
We cover how to use LiDAR for self-driving car applications inside PyImageSearch University.
What's next? We recommend PyImageSearch University.
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 84 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 84 Certificates of Completion
- ✓ 114+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 536+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this blog post we learned how to determine the distance from a known object in an image to our camera.
To accomplish this task we utilized the triangle similarity, which requires us to know two important parameters prior to applying our algorithm:
- The width (or height) in some distance measure, such as inches or meters, of the object we are using as a marker.
- The distance (in inches or meters) of the camera to the marker in step 1.
Computer vision and image processing algorithms can then be used to automatically determine the perceived width/height of the object in pixels and complete the triangle similarity and give us our focal length.
Then, in subsequent images we simply need to find our marker/object and utilize the computed focal length to determine the distance to the object from the camera.
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and 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!
Mike
Good info here:
http://photo.stackexchange.com/questions/12434/how-do-i-calculate-the-distance-of-an-object-in-a-photo
joe
How could you apply these techniques to sports events photos taken with a telephoto lens?
Adrian Rosebrock
You would have to estimate the intrinsic parameters of the camera which requires calibration with a “chessboard”.
ASAD
Hello Adrian I want one help . Actually I want to make a project in which I want take an image from any distance and want to know the center and dimensions of that circle
sc
we have done intrisnic params for fisheye camera, can u pls tell how to use that to estimate distance
André
HI Adrian,
Nice post, I will try to implement the idea to determine the size of an object after the calibration.
Hajar
Great article Adrian, it was really helpful! Thank you so much!
Adrian Rosebrock
I’m glad you enjoyed it Hajar! 🙂
JD
hey, Adrian
it’s a great piece of work you have done here and i used this technique working for my android device , and i want to take it to next level by measuring multiple object distances..but getting same results if two objects are on same position. any suggestions would be appreciated
thnx
Adrian Rosebrock
Hi JD, if you are looking to measure multiple objects, you just need to examine multiple contours from the
cv2.findContours
function. In this example, I’m simply taking the largest one, but in your case, you should loop over each of the contours individually and process them and see if they correspond to your marker.Aminah
I want to use this technique with android device too. 🙂
Sidharth
hey,could you help me, I’m working on this and trying to implement it in my android device too
Anton
on above example, we know the width of an object. What if we don’t know the width of the object ? for example in real life situation where a robot need to navigate the it meets each unknown object and need to find the distance even tough it has now knowledge about each objects actual width. Any other examples ?
Adrian Rosebrock
In general, you’ll need to know the dimension of the object in order to get an accurate reading on the width/height. However, in unconstrained environments you might be able to use a stereo camera so you can compute the “depth” of an image and do a better job avoiding obstacles.
Sowjanya Chunduri
Can we use a web-camera in an unconstrained environment for measuring the depth of an image?
Pradeesh
How an stereo camera can be used to find the distance of unknown obstacles?
Karen
For those who may still be looking for this information, once you have distances, it’s not hard to calculate widths (or heights). Just solve the original formula
D = (W * F) / P
for W, algebraically, which is
W = (D * P) / F
BTW, F is dependent on camera resolution, and unit of measurement (e.g. centimeters rather than inches).
Abu
Is it possible to do this using the back end of a car in realtime? Maybe a HOG classifier could detect it and than the program? Any insight would be helpful.
Thanks
Adrian Rosebrock
Take a look at this post on HOG + Linear SVM, I think it will really help you get started.
Dries
Hi there Andrian!
First of all i’m very thankful for this and other tutorials out there! this is by far the best series of tutorials online! Perfectly described step by step and explained why to preform every step 🙂 I can’t thank you enough!
I’m trying to do the same thing as described in the above tutorial (finding an object and determine the distance between obj and camera) but i wonder how i should do it when using constant streaming video instead of loading images?
thanks!
Adrian Rosebrock
Hi Dries, thanks for the great comment, it definitely put a smile on my face 🙂 As for when using a constant video stream versus loading images, there is no real difference, you just need to update the code to use a video stream. It’s actually a lot easier than it sounds and I cover it both in Practical Python and OpenCV and this post.
lady kenna
what if i wanted to display cm instead of ft?
Adrian Rosebrock
Your final metric is completely arbitrary — you can use feet, meters, centimeters, whatever you want. Take a look at Lines 25 and 29 and redefine them using the metric you want. And then you’ll need to modify Line 52 to output your metric instead of feet.
raj
Hi there,
when i try the same code using 2 feet and an image of 1 inch ,the focal length is around 1260 . Is this ok ? coz im getting unacceptable distances of around 3.4 feet for 6 feet… Are there any limits for this method . I find this method really interesting , i am thinking forward to do this in my project .
One more thing , will this work for webcam from a laptop .
Thanks in advance
Adrian Rosebrock
The main limitation of this method is that you need to have a straight-on view of the object you are detecting. As the viewpoint becomes angled, it distorts the calculation of the bounding box and thus the overall returned distance. And yes, this method will work with your laptop webcam, you just need to update the code to grab frames using the
cv2.VideoCapture
function. See this post for an example of grabbing frames from the webcam stream.David
Hi Adrian, Excelent tutorial i have a question for you, i hope you can help me.
i need the position (X,Y,Z) in mm, with your tutorial i could get the point z, my problem is when i calibrate the camera to get the points X,Y my Z is wrong.
do you know what happens?
Thanks.
Adrian Rosebrock
Are you doing work in 3D? If so, this method is not suited for 3D and you’ll need to use a different calibration method to examine the intrinsic parameters.
David
Hi Adrian, Thanks for answer me.
I’ll tell you what my project. I detect a small object in real time with a webcam, with the position of the object, will point a laser that will be about two servos , one for the X axis , and one for the Y axis.
I am ready the detection and tracking. but I’m having trouble getting the positions X , Y, Z . I research the transformation from 3D to 2D but there are certain points that do not understand.
do you can help me?
Thanks a lot
Matt
Hi David !
Currently, I’m working on a similar project, and I have a problem to do the relationship between coordinates in the 3D and servos. First, I would like to compute and track my 3D-coordinates object but it does not work.
Did you find an idea ?
Thanks
murugan
awesome program.but how to use it for a real streaming purpose.
Adrian Rosebrock
This code can be easily adapted to work in real-time video streams. I would start by looking up the
cv2.VideoCapture
function. I use it multiple times on the PyImageSearch blog — I think this post can help get you started.murugan
hi sir
i tried cv2.videocapture but ended with errors so i request you to modify the program
Adrian Rosebrock
If you are getting errors related to
cv2.VideoCapture
you should ensure that your installation of OpenCV has been compiled with video support.hyshan
Hi Adrian, how do you define the marker?
Adrian Rosebrock
In the case of this blog post, I defined a marker as a large rectangle. Rectangle-like regions have the benefit of being easy to find in an image. Markers can be made more robust by adding (1) color or (2) any type of special design on the marker themselves.
li
excuse me, I want to know how to realise it in real-time camera?
Adrian Rosebrock
In order to perform real-time distance detection, you’ll need to leverage the
cv2.VideoCapture
function to access the video stream of your camera. I have an example of accessing the video stream in this post. I also cover accessing the video stream more thoroughly inside Practical Python and OpenCV.Shatha Omar
Hi , this is a great helpful job
please i’m working on application that take an image then find the object to calculate its dimensins , so i need to know the distance or if there is another blog/ article/ refferance you can help me with
Adrian Rosebrock
In order to compute the distance to an object in an image, you need to be able to identify what the object is. For example, you could maintain a know database of objects and their dimensions, so when you find them, just pull out the dimensions and run the distance calculation.
khalil
Thanks for your nice post.. it is really a good job.. dear, is there any additional procedure for which i can use this procedure for the unknown object distance measurement from camera….
Adrian Rosebrock
You’ll need to know the size of some object in the image to perform camera calibration. In this example, we have a piece of paper. But you could have used a coin. A coffee cup. A book. But the point is that you need to know the size of object(s) you’ll be using to perform the camera calibration using triangle similarity.
Minjae
Hi, Thank you for this post.
I’m looking for like Johnny Chung Lee’s Wii headtracking in VR Juggler through VRPN projects.
Anyway,
when I tried this code, there are some isues.
My pi2 connected with noir-picam and ms-webcam.
1. X-window
(image:1297): GdkGLExt-WARNING **: Window system doesn’t support OpenGL.
2. X-wondow with cv virtualenv
Traceback (most recent call last):
File “distance_to_camera.py”, line 41, in
marker = find_marker(image)
File “distance_to_camera.py”, line 16, in find_marker
(cnts, _) = cv2.findCountours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack (expected 2)
3. console
(image:867): Gtk-WARNING **: cannot open display:
4. cv virtualenv
same with 2.
Please let me know the reason…
Adrian Rosebrock
Hey Minjae:
1. I’m not a Windows user, so I’m not sure a about this particular problem. Perhaps another PyImageSearcher reacher can help you out here.
2. The code associated with this post was built for OpenCV 2.4. You are using OpenCV 3. You need to update the
cv2.findContours
function call, here to work with OpenCV 3..3. Are you SSH’ing into your Pi? If so, make sure you pass in the
-X
flag for X11 forwarding.nami
Hi adrian,
Thanks for sharing.
I have tried your code and its work.
But is it possible to calculate the object distance if the object size in real world ( width / height ) unknown? Just using internal camera parameter and object size in pixels..
Adrian Rosebrock
Hey Nami, you might want to look into camera calibration and the intrinsic camera parameters.
nami
Hi adrian,
So after we do camera calibration and get focal length ( fx, fy), principal point, etc. How can i measure the object distance? with object size in real world undefine..
Thanks..
Adrian Rosebrock
You basically need to convert the “pixel points” to “real world” points, from there the distance between objects can be measured. This is something I’ll try to cover on PyImageSearch in the future, but in the meantime, this is a really good MATLAB tutorial that demonstrates the basics.
Juan Morales
Hello Adrian, is there a tutorial on this today? it would be very useful for me. Also Thank you for all your work, your books are great!
Adrian Rosebrock
Sorry, I haven’t had a chance to publish a tutorial on that topic yet.
mert
Hello Adrian. First of all , This is a great website. I have a question : Is this distance estimation can be done in real time processing applications. I mean I want to measure distance from an object(cicular and coorful) to my robot while the robot moving on a straight line. ı use a moderate camera with low resolution(usb cam).How should I do that
Adrian Rosebrock
Yes, this can absolutely be done in real-time processing, that’s not an issue. As long as you perform the calibration ahead of time, you can certainly perform the distance computation in your video pipeline.
mert
Thanks for the help and your fast reply man. Camera Calibration looks like complicated though. Cause I wiil look at an object from an angled position say 30 degree(initially) while I m moving on a straigt line the angle will increase. In each time. So I have to make sure that the object is almost middle in the frame to use above code?
Adrian Rosebrock
Camera calibration (at least the calibration discussed in this post) is actually pretty straightforward. This post assumes you have a 90-degree straight-on view of the object. For angled positioning this approach won’t work well unless you can apply a perspective transform. You should look into more advanced camera calibration methods.
mert
Got it. I’ll get right on it. Hope to meet you
JolyDroneSP
Hi Adrian. Thanks for the good work, it’s the most concise guide to this topic that I have found!
By the way, what camera did you use for this project?
I am trying to implement it through raspberry pi board camera and it doesn’t seem to work…
Adrian Rosebrock
I simply took photos using my iPhone for this post, but the code can work with either a built-in/USB webcam or the Raspberry Pi camera. If you’re having trouble getting your Raspberry Pi camera + code working, I suggest reading this post on accessing the Raspberry Pi camera.
Tejas
Hello Adrian!
First of all, thank you for a wonderful tutorial as always. I am working on a similar project and need some help.
1. Is there a literature review available regarding all methods of depth estimation `without` using a stereo camera i.e. using only a normal single lens camera? If not, can you point me to resources – papers and hopefully implementations – about the state-of-the-art on this problem? I should mention that I am mainly interested in understanding the physics of the scene and not reconstructing per se. I haven’t been able to find any decent open implementations of these and that’s kind of sad.
2. I am investigating the importance of head movements in animals(humans included) for depth perception and came across few decade old papers on the topic. Can you provide me references on how motion of camera affects detection of edges, depth estimation etc from a computer vision perspective? Aligning with point 1, I am looking for something on the lines of how one can estimate depth accurately by moving the single lens camera and detect edges and/or object boundaries by virtue of this movement. Methods like triangle similarity aren’t really helpful since they need an estimate of the original size of object/marker in question.
Please help me out with this and keep up the good work! 🙂
Adrian Rosebrock
For both questions, my suggestion would be start off with this paper and follow the references. This paper is heavily cited in the CV literature and links to previous works that should also help you out.
Tyrone Robinson
I may be coming in a little for this post but Im having trouble with the code.
When I run it I get the following error. Can you please assist me with this.
By the wayI think you are doing an awesome job.
Adrian Rosebrock
Please see my reply to Minjae above and read this post for more information.
Swan
Thanks for that clarification!
azizul
hello Adrian. it a very good work.
but can i ask question, how to implement stereo vision in this project?
Adrian Rosebrock
I honestly don’t do much work in stereo vision, although that’s something I would like to cover in 2016. Once I get a tutorial going for stereo version, I’ll be sure to let you know!
azizul
i will be really please to hear the news from u…thnk u very much 🙂
Jon
Hi Adrian!
First off, kudos on making such a complex system seem so intuitive! It makes the process look so much less intimidating to us newbies (whether I’m able to follow along once my picam gets here is another story!)
My question for ya is this: assuming I can manage to follow along and get distance readings for my marker, how difficult would it be to add the code required to trigger an event on a device that the marker is mounted to? In my case, I am looking to vibrate a small motor when the tracked object is more than 10 feet away. Ideally, it would increase in intensity based on how much further the object gets from the camera. I know this would require some investment in more hardware, but does it sound like a plausible idea?
Thanks for posting this, and being so generous with helping out in the comments!
Adrian Rosebrock
The short answer to your question is that it would be easy to implement this type of functionality. All you would need to do is wrap this code in a VideoStream rather than processing a single image. From there, you can make a check if the object is > 10 feet away. If it is, then you call your buzzer code.
Randy
In OpenCV 3, we must use cv2.boxPoints(marker) instead of cv2.cv.BoxPoints(marker).
Adrian Rosebrock
You’re absolutely right Randy!
Kenton
Hey,
I’m messing with this stuff now and it’s not working out too well for this part.
My error is cv2 has no attribute boxPoints.
Is there a way around this you can think of?
Thanks
Adrian Rosebrock
Hi Kenton — can you check which version of OpenCV that you are using? The
cv2.boxPoints
function is named differently depending on your OpenCV version.Kevin
Hey Adrian, great work! This was very informative and well done. I have a quick question regarding the limitations of such an approach when using larger distances. For example, 30 feet. At this distance, a relatively small object may be represented by very few pixels right? I’m assuming the better the camera (with better resolution) the farther the range in which this approach can still be accurate. My question is whether my assumption is indeed correct?
Furthermore, I find that when I utilize this approach, the distance calculation sometimes fluctuates as the perceived width in pixels fluctuates. Could this be due to noise? And if so, what are some good techniques to reduce said noise? I’ve looked into the blur function in OpenCV, but I haven’t had much luck with that.
Thanks again for the website, it’s super helpful. Look forward to hearing from you. Thank you!
Adrian Rosebrock
Indeed, that is correct. The farther away the object is and the smaller the resolution of the camera is, the less accurate the approximation will be. As for reducing noise, that is entirely dependent on the image data you are working with. Blurring is one way to reduce noise. You might also want to look into the segmentation process and ensure you are obtaining an accurate segmentation of the background from the foreground. This can be improved by tweaking Canny edge detection parameter, threshold values, etc.
mahdi
hello, please hellp me can i run this with opencv 3.1.0 ?
Adrian Rosebrock
Please see my response to “Tyrone” above — the only change needed for this code to run with OpenCV 3 is to modify the
cv2.findContours
.Abdul Javed
Hello Adrian… i just wanted to know that how can i use this distance recognition technique to make a 2D map of a vertical wall(whose photo can be taken easily) to precisely know the position of doors windows and other stuffs on the wall and the distances between each other and their dimensions with certain accuracy……???
Adrian Rosebrock
Hi Adbul — I’m not sure quite sure what you mean by a 2D map of a vertical wall, but if you want super precise measurements between doors, windows, etc., then I would suggest using a 3D/stereo camera instead of a 2D camera. This will give you much better results.
Matt
Hi Adrian,
Thanks for your tuto again. I have a question. What happened if you tilt the paper with an angle, with respect to the camera ? Does your algo consider it ?
Thanks.
Adrian Rosebrock
Yes, even with the paper titled, this algorithm will still find the paper, provided that the paper is the largest contour area in the image. For a more robust algorithm for finding rectangular regions (and verifying that they indeed have 4 vertices), please see this post.
Matt
Ok, but for example, if you tilted your paper with an angle of 90 degrees, you do not detect a rectangle, so you do not know the distance between the object and the camera no?
Adrian Rosebrock
Hey Matt, I’m not sure I understand your question — a rectangle has 4 vertices, no matter how you rotate it. An easy way to detect rectangles in an image is to simply use contour approximation, which I mentioned in my previous comment.
Matt
If you consider z the axis on which you compute the distance object-camera, x and y the additional axes, and if you rotate with an angle 90 degrees around x-axis or y-axis, your camera do not detect a rectangle but a straight line. So what happens in this case ?
Thanks.
Adrian Rosebrock
Oh, you were referring to the z-axis, that was my mistake. In that case, you would need utilize a more advanced algorithm to detect your object. This blog post is primarily geared towards easily detectable objects and computing the distance.
Frane
Hi Adrian, i need to find the distance and cordinates of the red marker. I made the filter to see red color only but i have problem considering distance. I lose the “seeing” the red color after 10cm (the markers are 3 red circle diameter 10cm each in triangle formation). Im using raspberry pi 2 b+ and pi camera
Adrian Rosebrock
How are you looking for the red color? Via color thresholding? If so, investigate the mask that is being generated from
cv2.inRange
and see if the red color region exists in the mask. If it does, then you’ll likely want to look at the contours being detected and see if red masked region is being returned.yousaf shah
how to work when difference size of same object?
Adrian Rosebrock
You normally would use a single reference object to calibrate your camera. Once you have the camera calibrated, you can detect the distances/object sizes of varying sizes. See this blog post for more information.
Farzaneh Golkhoo
Hi Adrian
Thanks for your great information, just I have a question.
if you take a picture of a special object for example in the ceiling and you are required to know the distance of that object from the camera but not the perpendicular distance, what should we do?
In fact I know the exact location of the camera (x,y,z) and also the location of the object in the ceiling in terms of (x,y) but I do not know the z of the object. I want to measure z.
As you said I can measure the perpendicular distance between the camera an the object by taking a picture of the object, but I have to know the direct distance (not perpendicular) between the camera and the object.
Thank you so much
Mr.E
Hi dear Adrian . tanks for all of your good and useful information.I had very good result of this algorithm on mobile photography or raspberry camera . but i just have big problem with the digital camera witch has a lenses (DSLR or compact ). Can you help me about this?? if i know the F of the camera for example 3.2 how should be put it on my calculating ???
what i calculate (F) is about 3600~3700 . and so it’s give me wrong answer .
Adrian Rosebrock
There is a difference between the focal length of the physical lens and the perceived focal length from the image. You’ll need to calibrate your DSLR camera in the same way that you performed the calibrations on the Pi and mobile phone.
Chandrama
Hello i am trying to use your code but not able to get output ,
getting error like..
Please help to resolve
Adrian Rosebrock
Please read the comments to this post before you post as I’ve already addressed this issue multiple times. Since you’re using OpenCV 3, you need to change the
cv2.findContours
call to:(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
You can read more about the change to
cv2.findContours
between OpenCV 2.4 and OpenCV 3 in this blog post.Talgat
Thank you!
Excellent article! But one little mistake that can confuse beginners, you wrote “perceived width of the paper is P = 249 pixels” but in calculations you used 248. Hope to see the tutorial on finding the distance to an randomly chosen object by using stereo-pair of cameras.
Adrian Rosebrock
Thanks for pointing out the typo! I have corrected it now.
Amigo
Please comment on how accurate can this method be, i.e mm cm or in inches etc. also if I want to find distance between two objects, how should I modify this code?
Adrian Rosebrock
The level of accuracy depends on the resolution of your camera. The smaller the resolution, the less accurate. The further the objects are away, the less accurate. This script does not perform radial distortion correct, which is something else to consider. As for finding the distance between two objects, please see this post.
Shiva
Hello Adrain,
Great Article to start with the distance estimation.
I have downloaded your code and trying to validate with images, but i am not getting the distance as expected. Could you please send me some reference images.
Adrian Rosebrock
Hey Shiva — the downloads to this blog post also include the example images I included in this post. You can use these images to validate your distances.
Thommy
Hai Adrian, when i try run the code, i got error :
Please, hope the resolve
Adrian Rosebrock
The code for this blog post was intended for OpenCV 2.4; however, you are using OpenCV 3. You can resolve the issue by changing the code to:
box = cv2.boxPoints(marker)
Su
Hi Adrian,
Can you please help me here?
Traceback (most recent call last):
File “distance_to_camera.py”, line 53, in
box = np.int0(cv2.BoxPoints(marker))
AttributeError: ‘module’ object has no attribute ‘BoxPoints’
__________________
>>> cv2.__version__
‘3.3.0’
Adrian Rosebrock
See my reply to “Thommy” above.
Ankit
Hello Adrian Rosebrock,
Your explanation helped me understand the concept very well. I am still perplexed by another problem. For example, I have a calibrated camera, i.e. I know the focal lengths and the optical offsets of the lens and sensor. Then, the relation between pixel and the actual height/width of an object is true only if the object is placed at the focal length. If I place an object of unknown dimensions at an unknown distance from the camera lens, then there is no way to estimate the distance between them. Am I thinking right or is something missing? Can you please help.
Thanks
Ondrej
Hi. I was thinking about make mobile app which will measure width and height some objects by using dual cameras like this:www.theverge.com/2016/4/6/11377202/huawei-p9-dual-camera-system-how-it-works . Do you think that it will be working? Thank you for your answer.
Adrian Rosebrock
Using two cameras you can measure the “depth” of an image. I don’t cover this on the PyImageSearch blog, but it is absolutely possible.
Erwin
Hi Adrien,
Thanks for the information. I have one question: Is it possible to incorporate the distance estimation with the ball tracking code you have? I am currently working on a project whereby I am trying to detect an object with the color green and find the distance between the camera and the object.
Adrian Rosebrock
Absolutely, but you need to calibrate your system first as I did in this blog post only this time using the green ball. From there you will be able to estimate distance.
Erwin
Sorry, i seems to have phrased my question wrongly. What I meant was is it possible to find the distance from the camera to the green ball in real time? So instead of making it detect edges, i modified it to detect green?
Adrian Rosebrock
Yes, it’s absolutely possible. As long as you perform the calibration step before you try to find the distances you can use the same technique to determine the distance to the ball in real-time.
Erwin
Your help is greatly appreciated. Thank you.
Josue Godinez
Hello Adrian, nice post.
Just a simple question. How you determine the “focalLength”?
Adrian Rosebrock
Take a look at the example in the “Triangle Similarity for Object/Marker to Camera Distance” section to see how focal length is computed. Otherwise,
Lines 38 and 39
compute thefocalLength
variable.John
Hello . I want to use this code to detect images real time using the PiCamera. I read your replies and honestly have no idea how to ” Use the cv2.VideoCapture function to access the stream of your camera ” . Playing with the code results in all sorts of errors. Could you help me by giving a detailed explanation ? Thanks
Adrian Rosebrock
If you want to use the Raspberry Pi camera module you should start by reading this blog post on accessing the Pi camera module before doing anything else. From there, you can take the code from this post and use it with your Raspberry Pi camera.
Mostafa Sabry
Hi Adrain,
I am a big fan of your posts, I was impressed with all of what I have seen. I have a problem in my thesis that I guess you might help me in related to localization. My thesis project is automating the process of lawn mower. The mower will be given the size of a rectangle to cover then it will move back and forth starting from one of the corners. It will track its distance using wheel encoders, however, due to error due slippage and drifting. Therefore, we need a reliable method through which we can know the absolute location to correct for the relative localization error of wheel encoders. We have tried several methods and all had some problems:
1- Tracking successive features in frames to estimate the rotational and transnational matrices however for sharp turns it looses track of everything.
2- Triangulation by placing three balls of different colors and identifying the angle of each through a camera on a motor however using colors outdoor is so unreliable due different lightening at different day times. When you decrease the HSV scale becomes more accurate but increases the probability of loses balls and increasing range catches noise from the environment.
3- Using homography instead of color balls for triangulation but it is computationally slow.
4- Using April Tags or Aruco tags but as mechanical engineers, were are finding it hard to develop our algorithm and still we didn’t find a starting point to continue on by finding a code and understanding it.
Hope U can help us and sorry for the long post
Adrian Rosebrock
To start, it’s always helpful to have actual real-world images of what you’re working with. This helps me and other readers visually see and appreciate what you are trying to accomplish. Myself, as I imagine many other readers, don’t know much about the intricacies of lawn mowers, wheel encoders, or slippage/drifting.
That said, based on your comment it seems that the homography is producing the correct results, correct? And if that’s the case why not focus your efforts on speeding up the homography estimation? Try to reduce the number of keypoints? Utilize binary rather than real-valued descriptors? Implement this part of the algorithm in C/C++ for an extra speed gain?
Eren
hi Adrian. Firstly,thank you for sharing.nowadays I am working similar projects.
so I have a question.there is an object with a known width w and I don’t know distance D from my camera.in fact I will do that I will put a rectangular object ( a box) with a known size under camera I will measure distance from object but I know only distance between camera and ground. how to do?
Adrian Rosebrock
If you know the distance from the ground to the camera and know the size of the object in both units (inches, millimeters, etc.) then you should be able to apply some trigonometry to workout the triangle property. I haven’t actually tried this, so I’m just thinking off the top of my head. It might not work, but it’s worth a shot.
Alex
Hi Adrian, first of all, excellent article!
I have a question, regarding this code. I’m currently carrying out research for my dissertation which requires using stereo vision to calculate distance from the camera to a chosen object/area. Will this code be applicable for stereo vision as well ?
Thanks!
Adrian Rosebrock
No, I would not use this code for stereo vision. The reason is because stereo cameras (by definition) can give you a much more accurate depth map. I don’t do much work in stereo vision, but this short tutorial or computing a depth map should help you out.
Alpha
i am doing a project in which i need to get the exact location of a human at a distance. Exact location refers to the EXACT location as used by a gun to aim at an enemy. Here, i dont have any marker object or any known distances. Any way out?
Adrian Rosebrock
You would need to compute the intrinsic properties of the camera first. I would suggest starting here.
Tanya
Hey Adrian,
This is super nice tutorial ever!!
I am working on the project about detecting the distance changing of array sphere. (changing like mm in unit)
I gotta put the camera at the same axis of the movement so its very hard to see the difference.( the sphere is very small like 2 mm)
Do you think is it possible to detect using your algorithm?
Adrian Rosebrock
It really depends on the quality of the images themselves. How high is the resolution of your image capture?
Tanya
around 640 × 480 pixels/each image
for area of 1 x 2 cm
Adrian Rosebrock
Hmm, that’s a pretty small resolution for that accurate of results. The first step would be to calibrate your camera and account for barrel distortion. If your images are really noisy and you can’t accurately segment the object from every image, then you’re in for some real trouble. But if you can get a nice segmentation I would give it a try and see what results you come up with. It’s best to experiment with projects like these and note what does and does not work.
Dasaradh S K
Hi Adrian. i would like to calculate the distance between a drone flying at a good height (with a cam & MCUs) and people at the ground. Please help me Adrian. 😀
Rahmat Hardian Putra
Hi Adrian.. your article is a really great tutorial 😀
btw i’m working on a project that measure distance of a fire, but fire tend to change it shape, whether it smaller or bigger, so the marker also get bigger or smaller, therefore I can’t define the width and height of the marker.
the question is, can I modify your algorithm so that I can measure distance with my condition ? or do you have another method that suitable to measure distance of a fire ?
Thanks in advance
Regards
Rahmat Hardian
Adrian Rosebrock
It sounds like you might need a more advanced calibration technique. I personally haven’t done/read any research related to fire direction techniques with computer vision, but I would suggest reading up on intrinsic camera properties for calibration.
kartik pandey
can you please describe the hardware part of this amazing project of yours as i need it for a small project of mine too like how did u integrate the phones camera or did u use raspberry pi with a camera module please let me know
Adrian Rosebrock
I used my iPhone to capture the example photos. The photos were moved to my laptop. And then I processed the photos using my laptop. The actual method used to capture the photos doesn’t matter as long as (1) its consistent and (2) you are calibrating your camera.
Musa
Hi Adrian,
I’m trying to copy this method for a USB camera and have used your previous posts to modify it to work with a camera using a while loop. The problem I’m having is the max function in find_marker is constantly coming out as None hence resulting in the distance_to_camera function throwing an error. Do you have any idea why this could be?
Horus
Hi,
I have an CT 2D image with two projection. in the image there are spherical objects, free or occluded. How can I get the depth of these spheres and their centre. An idea or any help would be great.
Israa
Hi adrian,
please help me
when I use the same code and the same images, I get these results .
inches:
24.0
201.873157429
17.7140389174
what is error?
youssef
Hey adrian, this website is the bestest reference for beginners, but may i ask if for example the camera is not looking straight to the object the distance in pixels would change so it wouldnt work right ?
if my logic was correct how to overcome this ?
thank you
Adrian Rosebrock
If there is variation of viewpoint then you would definitely want to calibrate your camera by computing the intrinsic properties of the camera. This will give you more reliable results.
Randy
Hi, excellent tutorial, i got the following error, maybe you or someone can help me:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor,
…
Adrian Rosebrock
Hi Randy — double check your image paths to
cv2.imread
. It looks like your image path was invalid, causingcv2.imread
to returnNone
. Thecv2.imread
function does not throw an error when an invalid image path is supplied.Sid
Hi Adrian,
Amazing tutorial to get me started with the marker detection. It was really easy to understand with all the explanation that you’ve given! Cheers to that 🙂
Lets say I wish to detect an object which is totally black with a size of (2 in x 2 in). How would I be able to do that with your code?
Thanks
Adrian Rosebrock
I would suggest using color thresholding (rather than contour properties) to find your black object.
Vaibhav Jha
Hey Adrian,
In order to calculate you used L = W*F/P
L= length of the object from cam
W=Width of object
F=Focal length
P= Pixel width of the object
But in the code for the pixel width you supplied the value marker[1][0].
my question:
1) What is the meaning of marker[1][0]?
2) Also, shouldn’t you just convert the value of knownwidth into pixel and hardwire into the code? Instead of marker[1][0]
Thanks !
P.S. I am applying you code(modifying it a bit) for faces. I am also very new to programming so sorry if my questions are too silly.
Jackson Curry
marker here returns the parameters of minAreaRect which are these 3 parameters:
(center (x,y), (width, height), angle of rotation). Note that the first and second parameter here actually have 2 parameters within them (x,y) or (width,height). Thus why he is doing marker[1][0] is to access the second parameter and then the first parameter which corresponds to width. Lets say you want to reference height then you would say marker[1][1].
Alex
Awesome! Could this work with height instead of width?
Adrian Rosebrock
Yes, absolutely. As long as either the width or height is known you can use the same algorithm.
Shehroz
Hi Adrian
Can i use Kinect sensor as my camera?if so,do i have to change something?
Adrian Rosebrock
It’s been a long time since I’ve used the Kinect camera, but I would likely recommend something like PyKinect.
nahid
Hi Adrian, exellent tutorial, i’m working on a project in which i measure how tall are the People. I have to use a cellphon as camera.
Can i mix this Projekt with machine learning. So that i train a System with bodysizes in cm und in Pixels as expriences.
Denn i use the bodysize as my refrence to calculate another properties of Body.
for example: leg and arm length. At the end can i Train another system to estimate the weight.
Please tell me your recommendations and expriences. That’s my masterthesis and i have not enough time.
Thanks
Adrian Rosebrock
If you need to measure the size of a person you don’t actually need any machine learning outside any you might want to apply to detect a person in a photo/video stream. Simply compute the intrinsic/extrinsic parameters of the camera and calibrate.
Computing weight is much more challenging. If you’re using a 2D camera I doubt you’ll be able to obtain a reliable weight measurement. Perhaps machine learning could be used here with enough training data but I would be very skeptical.
Rahul Tripathy
Hi Adrian,
I definitely liked the approach but i do have a few questions. It would be great if you answered it!
First, the image width will vary with the angle the picture is taken from. Then how to find the distance in such case?
Second, the depth here is for a known object. What if I want to get the depth of objects in an image? All blogs stop after getting the disparity map! I need to know the use of the disparity map like you do with all the other concepts in your blogs 🙂
It would be great if you could help! And to be mentioned…. Not just I but many love your blogs and it may be the only place where we got to know that computer vision is not hard to begin 🙂 So, from all of us…. Thank You.
Adrian Rosebrock
This is a basic form of distance measuring. For varying viewpoints and more advanced distance measuring you would definitely want to calibrate your camera by computing the intrinsic/extrinsic parameters. If you have multiple cameras or a stereo camera you can compute the depth map.
Ram
Im getting this error on run of this program:
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack
plzz solve my doubt
Adrian Rosebrock
Please read the other comments. I’ve already addressed this question multiple times. See my reply to “Tyrone Robinson”.
Dawn Rabor
Hi adrian! I would just like to ask if there is a possible way how to compute the distances between lines in an image? Thank you!
Adrian Rosebrock
Please see this post.
Carlos
Hey Adrian, I have learned a lot with your tutorials, thanks you! I
I am currently working on a very challenging project where I need to determine the X and Y coordinates of an object in relation to the room it is in. I know the real height of the object and how the camera perceives it’s height in pixels, my difficult is how I come up with the X and Y coordinates? The linear diantace from the camera to the object is possible to be calculated by using the triangle.
Best
Carlos
Adrian Rosebrock
Hi Carlos — so if I understand your question correctly you need to transform the (x, y)-coordinates of your object to the “real world” coordinates? If so, you would need to perform a more robust camera calibration by computing the intrinsic/extrinsic parameters.
Dulce
Hello, where can I find theory about the perceive focal length. I’ve looked into the triangle similarity and coundn´t find a relation between them
Manh Nguyen
Hi Adian, This post is very interesting, Everyweek I have to read your website for studying computer vision. I have a question. I mesure two time. the first time, Distance and width = const so I calculated focal length F. the second time, I measure distance by ultrasonic sensor and F =focal length in first time and I measure width. If I do this, the result is correct? Pls help me. Wish you have a nice day, thank you!
Adrian Rosebrock
Hi Manh — I haven’t used ultrasonic sensors for distance measurement so I would do a bit more research into this.
berk
Hello Adrian;
Vaibhav Jha asked the same question, but I think you missed it.why use the item at the marker as follows?
it will not cause a usage like this: marker[0][0]
Mary
Thank you. Can I make it work with a video instead of images? where I need to calculate the distance between the camera and a person face?
Adrian Rosebrock
Yes, I would suggest take a look at this blog post on accessing video streams as well as my book, Practical Python and OpenCV.
Amanda Joy Panell
Do you think this technique could be tweaked to find the distance of something REALLY far away? (3-5 miles) accurately?
Adrian Rosebrock
No, not using standard cameras. Measuring objects from that far away would require the intrinsic/extrinsic parameters of the camera, a high definition capture, and an object that could be easily detected/isolated.
YJ
Traceback (most recent call last):
…
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack
Hi Adrian, I have face a problem since i just try to run the program why will come out those error? can tell me why ? thank you.
Adrian Rosebrock
Please take a look at the comments as this question has already been addressed. In particular, I address this question in my replies to “Minjae”, “Chandrama”, and others. The quick and dirty solution is:
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
Since you are using OpenCV 3.
YJ
Hi Adrian, is it possible modify your code and use in real time ? like I’m going to use the laptop camera to detect the distance of an object from the camera .
Adrian Rosebrock
Yes. Please see my replies to “raj” and “Jon”.
TrAyZeN
Hello, in what unit the focal length is express ?
Adrian Rosebrock
In this tutorial we use “inches” as our unit of measurement. However, you can convert the code to use another unit.
Srinivasan
Hey Adrain,
This code is good for distance calibration. I have small that when it’s fixed frame of paper its calculated easily. When calculating distance of moving object how to calibrate the distance, where you don’t know the exact size of the object(Both physical size and pixel size of the object) which appears in the frame
joseph
hello Adrian, please how do I measure the width of the piece of paper in the image take i have captured with my phone
Adrian Rosebrock
Hi Joseph — I would suggest referring to this blog post on measuring the size of objects in an image.
Kevin Roy
Hi Adrian,
I’ve recently started openCV and have been following your tutorials. So far, its been the best series of tutorials I’ve ever found, online and otherwise. Currently, I am working on a little side-project which requires me to crop a square part of an image. The part needed to be cropped is random so I cannot directly mention the X, Y coordinates in the code. Therefore I tried to use the find_marker function in this post to find the square and then crop it. But as sson as I run the program, I get the following error:
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack
Can you please help me to sort it all out?
Adrian Rosebrock
Hi Kevin — I’ve actually discussed this error a few times in the comments. Please see my replies to “Tyrone”, “Chandrama”, “YJ”, etc. Thank you.
Thanos
Hi Adrian,
I am a MSc student at National Technical University of Athens, and my master thesis is Real Time Position Tracking of an underwater robotic fish inside a tank full of water. I use your color tracking algorithm, to extract (x,y) position of the fish. The robotic fish is moving in a plane xy that is perpendicular to my camera. At this moment the distance is at 135 cm from the camera. I want to add to my code, a block of code that calculates the distance of the moving plane from the camera, to update the distance in case of moving fish deeper in the water.
I want to combine your color tracking algorithm with that distance find algorithm, real time.
I have three questions:
1. According to your code (distance from camera), the focal length is at mm units? Your focal length result is 543.45. My focal length result is in range of 200 – 800 testing a variety of cameras avaliable. If focal length units are in mm, is there any logical explanation of too much high values? Because from what I know, focal length must be small (3 – 10 mm). I use simple webcameras.
2. The distance you calculate is the distance from camera to the perpendicular plane or the distance from camera to the object, where the object has coordinates (x,y), not to the center of the screen?
3. This algorith you are using, is calculated on the air. My robotic fish is inside the water. Will the result changes if the color tracking is happening inside the water? Because due to refractive effects Ι think I will face a big problem.
Benni
It is showing me a error in the 54 th line “cv2.drawContours(image, [box], -1, (0, 255, 0), 2)”.It shows that cv2.error. error: (-215) npoints > 0 in function cv::drawContours.
Please help me soon. I am stuck.
Benni
Actually I figured it up. Thank You anyways. it is because I should have typed the code as “cv2.drawContours(image, [box.astype(“int”)], -1, (0,255,0), 2)”
Saurabh Thawali
That’s awesome and exactly i was looking for to implement in my personal project 🙂
Lucky to find such a detail resource, Appreciate your great work and advice/comments Adrian Rosebrock…. You rock \m/ \m/
Now i am implementing this using laptop webcam the way you guided in your another post here https://pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/
but i am not sure how your Room Status (occupied/unoccupied) is changing based on calculations….may be i need to work on it more.
Thanks once again ….Much appreciated !!
Adrian Rosebrock
Thanks Saurabh 🙂 I’m glad you found the post useful! The room status is changed based on background subtraction. For a more advanced background subtraction algorithm, take a look at this blog post.
Nachiket
What is the difference between the function cv2.boundingRect() and cv2.minAreaRect()? When to each of them? Is it not that both of them returns x,y,w,h?
Adrian Rosebrock
The
cv2.minAreaRect
function returns a bounding box that can be rotated, hence the term the minimum area rectangle that the region will fit into. Thecv2.boundingRect
function will return a non-rotated bounding box.Nachiket
Ok Got it. Here in this example, we can use either of them, right? Can you tell me a particular example where we ‘should’ use minArea function and an example where boundingRect should be used?
Thank you
Adrian Rosebrock
As an example, you should use the minAreaRect when you need a rotated bounding box and then later need to apply a perspective transform, such as in the document scanner post. If you used boundingRect the four points of the rectangle would not match the vertices of the document we are “scanning”.
SR
My code is working without any errors but, the distance (value) is not displayed on the picture after the code runs. any help would be much appreciated.
Adrian Rosebrock
It sounds like either the reference object or the object you want to compute the distance to was not detected. Check the contours list and ensure they were detected.
Karan
Good evening sir, I want to know how can I detect the height at which object is placed from the ground when we are using a webcam as a feed…
thanks sir in advance..
please do reply
Adrian Rosebrock
Hey Karan — take a look at this blog post where I discuss how to measure the size of objects in images. I hope that helps!
SNR
Thank you very much for this informative tutorial. i am currently doing my final year project and i have to find distance to tennis balls. i have studied your real time ball tracking tutorial. i have tried to combine that and this tutorial for a real time situation. however i am not getting it right. any tips on how to combine the two codes, where to break the code etc. any help would be much much appreciated and will be a great support to complete my project. again thank you very much for the tutorials.
Adrian Rosebrock
Congrats on working on your final year project, that’s great! It’s hard to give generic tips so could you please elaborate on what specific issues/errors you are encountering when trying to combine the code from the two posts?
SNR
Thank you very much for replying, I tried to add the ‘finding distance’ code to the ‘ball tracking’ code. I’m having trouble finding the exact position where the ‘finding distance’ code should start in the original ‘ball tracking’ code.
Also I should not be using the ‘find marker’ statement at the beginning right? Since I’m already tracking the ball and contouring it in the ‘ball tracking’ code.
And I changed the term ‘image’ to camera, giving the camera = cv2.VideoCapture(0) command.
I also removed the IMAGE_PATHS = [“images/2ft.png”, “images/3ft.png”, “images/4ft.png”] and image = cv2.imread(IMAGE_PATHS[0]) command. I’m not sure whether its the correct thing to do.
The command, “for imagePath in IMAGE_PATHS:” and “marker = find_marker(image)” , I think I replaced it with the wrong terms; therefore I’m not getting anything.
I can see that the camera is active and that’s all. Sorry for asking so many questions, this my first time doing image processing. Any help would be much appreciated as I’m struggling with this big time. Thank you very much for your time!
Adrian Rosebrock
I would start by ensuring you have performed the calibration and computed the triangle similarity. If I understand your project correctly, there isn’t a point in trying to track an object if you haven’t computed the distance, correct? Thus, you need to calibrate first.
Once you have your focal measure you can move on to the actual tracking. Here you’ll be looping over frames from your video stream and looking for your object. If you’re using the ball tracking code, here you will be performing color thresholding. Once you find the ball in the mask you can pass this area into the
distance_to_camera
function which will give you your distance.It can be tricky putting together code from multiple posts, especially if you’re new to image processing and computer vision, but it is doable. I would recommend that you work through Practical Python and OpenCV. I designed this book to help beginners with zero experience in computer vision/image processing get up to speed quickly. It’s a quick read and if you pick up a copy, you’ll be done by the end of the weekend and be better prepared to tackle your ball tracking + distance measurement project.
I hope that helps!
SNR
thank you very much for all the tips. i have calibrated and found the focal length and also the color threshold. i don’t understand the part where you tell to, “pass this area into the distance_to_camera function”. any help would be so helpful.
Adrian Rosebrock
Once you have the focal length and the object detected you can call the “distance_to_camera” function (take a look at the parameters required for the function; that is what I meant by “pass these values”).
mithilesh
hi adrian you did an awesome job there…i have a question regarding finding the depth of an object in an single shot of camera..is this possible?
Adrian Rosebrock
This method will give you the distance to the object. Are you referring to computing a depth map for the entire image?
mithilesh
yes
Adrian Rosebrock
Computing the depth map is best done using a stereo/depth camera. Are you trying to build the depth map from a standard 2D camera?
amir
Hey Adrian,
How can I implement this if I want to measure the distance from a pi camera to an object which is detected in real time from its colour? Im struggling to adapt this code for using my raspberry pi camera for distance tracking…
Thanks,
Amir
Adrian Rosebrock
Hey Amir — this tutorial demonstrates how to detect and track an object based on color. You can combine the code for both of these tutorials.
ami
Hey adrian, really appreciate your response.
I couldn’t delete/modify my original reply apologies. When merging this code for detecting colour in a stream, i’m not sure if you still use your images for calibration and calculating focal length because I am thinking that you have to somehow leverage the colour (in my case its blue) to be used as a reference point to measure from..? Spent a few days on this now and I’m struggling on trying to figure out how to combine the two functions so that the distance to the blue colour from the picamera can be measured in live stream..
Thanks in advance again!
Adrian Rosebrock
You can use whatever object you would like for the initial collaboration provided you know:
1. The size of the object (in terms of a measurable units)
2. The initial distance to the object (again, in measurable units)
Once you perform the initial calibration you no longer need the reference object.
Try to nail down the code used to compute the focal length before you try incorporating the actual tracking of the object and measuring the distance.
It can be a tedious process, but this is how we learn. Keep it up, you’re doing great 🙂
Adrian Rosebrock
1. You just need to calibrate your system by computing the focal length once per run. Once the script is up and running and you have performed the calibration you can compute the distance to any object, provided you can localize the object of course.
2. For an object that is not parallel to the camera you will need a more advanced method. Take a look at computing the intrinsic and extrinsic parameters of your camera.
LOKESH AMARA
Sir, your work is awesome and very helpful.But i have 2 questions
1) sir, how did you know the image is only 270 pixels wide because if i take image with my android camera i am getting 3024 pixels width
2)and later you mentioned that by moving some distance back you get 180 pixels width of the object through image processing.how did u get that
sir, i am doing my final year project on this topic only.could you please help me out??
Thank you in advance
Adrian Rosebrock
I did not know the sizes n pixels. I knew (1) the distance from the camera to the object and (2) the width of the object. Once I detected the object in the image I could determine any pixel dimensions.
ADAM
is there any way to work this out on Android studio ? can we integrate this with tensorflow ?
Adrian Rosebrock
Java provides OpenCV bindings, I would suggest you start there.
SKR
As usual AR rocks with his technical yet easy to follow articles!
I would request to have some 1) suggestions, 2) pointers, and 3) insights regarding a problem I am working on since past a few days. Instead of having one image, how would you compute depth (Z-coordinate), if you have two images? Let us say for example, you have two time-aligned videos, first showing the front view and the second showing the left-side view. We can extract RGB frames from both videos so we have two images now. Without having any knowledge of camera intrinsic or width of object or focal length how you can compute the depth and thereby the real X,Y,Z coordinates using these two images? Please suggest all methods/techniques or provide pointers to resources, perhaps your own article on this problem, and if possible give some insights. The video contains a person sitting and changing his gaze so the problem relates to estimation of gaze.
Lastly, by any logic or technique, is it possible to measure depth from single video, if we have multiple frames extracted at different times without camera or object moving?
Adrian Rosebrock
This method is a very simplistic form of camera calibration. If you want to be working with depth you should compute the extrinsic/intrinsic parameters of the camera and perform a full-blown calibration. I wouldn’t suggest any approximations (they won’t work). I haven’t worked with depth from a single video/camera so I’m not sure about the answer to that. This video from Microsoft research might be similar to what you are trying to obtain.
SKR
Thanks for your reply AR. Actually what I wanted to know whether it is possible to measure depth to create a 3D point cloud WITHOUT computing the extrinsic/intrinsic parameters of the camera and full calibration. I have no knowledge of cameras as I do not possess them. What I have are two time-aligned random RGB videos, one a giving front pose and the other giving a side pose. I can extract multiple frames from each videos and can start working.
Source Wiki: It is possible to perform rectification without having the camera parameters. All that is required is a set of seven or more image to image correspondences to compute the fundamental matrices and epipoles. Ref: Richard Hartley and Andrew Zisserman (2003). Multiple view geometry in computer vision. Cambridge university press.
So if we can perform rectification using more than seven extracted frames, is it possible to arrive at depth somehow? Thanks so much for your pointers and insights.
Adrian Rosebrock
I believe the OpenCV docs have something similar to what you are referring to. I would suggest starting there. You need seven images (typically the “chessboard”) and keypoint correspondences to compute the fundamental matrix. The OpenCV docs cover this.
SKR
Hey Adrian, just a quick question, do you have any idea about what a volume blending means and how one can achieve it so that 3D reconstructed images look like a filled up object instead of a plain image? I was unable to find some good resources to read and understand about it.
Also, can multiple consecutive video frames act as image slices to have volume rendering during 3D reconstruction?
Thanks in anticipation.
Adrian Rosebrock
Sorry, I do not have any experience with volume blending.
lulu
hi Adrian, i want to used your distance formula in my thesis. could you give me reference where you get that formula so i can put it on my thesis ?
thank you
Adrian Rosebrock
Triangle similarity is a basic Geometry topic. Most Geometry textbooks will cover it. If you want to include a reference to this PyImageSearch blog post, please feel free to do so, but I don’t think there is a “singular source/reference” for using triangle similarity.
ayesha sh
Hy Adrian, Great Tutorial as always. I have implemented it and got awesome results. Next thing I want to do is to implement it to calculate distance of a moving object from the camera. Like you already have a tutorial on motion detection and tracking, I would like to know how can we merge these two so that we can find the distance of a moving object from camera continuously rather than putting images?
Thanks in advance.
Adrian Rosebrock
The first step would be to access your systems camera. I would suggest starting with this blog post and then merging the code together.
AliK
Hi Adrian..thanks for your as-always great tutorial…I have one question: imagine after edge detection (figure 1) you needed to choose the contour of your measuring tape (instead of the piece of paper).. do you have any idea how that can be done? (consider the image is busy so it cannot be defined simply as the contour on far right or far left hand side of the image)..for example, is there a way to use two parallel lines (which is the property of measuring tape) to detect its contour in a busy image? Thanks for any suggestions you might have!
Adrian Rosebrock
Yes, you can do this. Take a look at the “cv2.HoughLines” function in OpenCV (I do not have any tutorials on this method, unfortunately).
AliK
Thank you Adrian!
Sam
As always great tutorial sir..
From where you get bounding box rectangle width in pixels?
Which function/line return it’s value in above code?
Adrian Rosebrock
The “find_marker” function is responsible for finding the marker (in this case, the piece of paper) in the image. The “cv2.minAreaRect” returns the (rotated) bounding box.
dheeraj
which camera would be preffered for this project?
Adrian Rosebrock
I used my iPhone to gather the example images. If you would like a USB camera I really like the Logitech C920.
ahsan
hello sir,
what code displays the pixels in image.?
Adrian Rosebrock
I’m not sure I understand your question properly but the “cv2.imshow” function is used to display an image to your screen. I’m not sure what else you may be looking for.
ahsan
hello sir,
can you help me,? please
whether the code that displays the pixel value is “marker[1][0]”.?
but why the program I created pixel value there is its comma, not an integer
thanks
Tux
Hi Adrian and thank you for all your brillant work.
Can you explain why you work on 800×600 images instead of the originals please ?
Thanks very much !
Adrian Rosebrock
The less data there is to process, the faster your algorithms will run. Secondly, resizing images can be considered “noise reduction”. High resolution images may be visually appealing for us to look at but they can actually hurt computer vision algorithm performance.
Jamie McMahon
Hi Adrain, like a lot of people that follow you im a bit of a beginner and having a really stupid error but cant seem to find the solution for it. When I compile the code here I get the following error message:
“Traceback (most recent call last):
File ‘distance_to_camera.py’, line 37, in
image = cv2.imread(IMAGE_PATHS[0])
NameError: name ‘IMAGE_PATHS’ is not defined”
Any suggestions on how to fix it?
Adrian Rosebrock
Hey Jamie! Thank you for catching this error. I had recently updated the blog post to work with both OpenCV 2.4 and OpenCV 3 (and future versions) but had refactored part of the code. If you take a look at the updated blog post, in particular Line 37, you’ll see it now reads:
image = cv2.imread("images/2ft.png")
If you update the code you downloaded it will work just fine 🙂
Thanks again!
Jamie McMahon
Adrian,
Thanks very much for responding, like I said I knew it would be something stupid on my part. Really finding the book and website excellent for improving my knowledge on OpenCV and Python.
a010291
Hi Adrian,
Useful tutorial. I know this question has been raised already but still unable to get proper answers.
Is it possible to determine the distance of a person(face) from the camera? If possible can you kindly tell me how to proceed. I am almost stuck with this issue for weeks.
Adrian Rosebrock
You can swap in a face detector instead of the rectangle detector that we used here. From there the rest of the code is the same but keep in mind you may still want to use the piece of paper to calibrate the system first as face/head sizes will vary depending on the person.
John
Hi Adrian,
Great tutorial!
I was wondering if this could be done with multiple objects which then could be mapped out on a grid?
For example put stickers on walls around a room, go around with the cameras, and essentially plot a map of the room.
If not, do you have any suggestions on how to do this type of thing?
Thanks!
Adrian Rosebrock
It’s absolutely possible but I would instead suggest researching Simultaneous Localization and Mapping (SLAM) algorithms — they can be used to map areas.
Shrijan00
Hey Adrian
I’m still confused why u choose marker[1][0] only?
Gianluca
Hi Adrian,
Thank you very much for your tutorial! I have a question.
I have a monocular camera mounted on the end-effector of a robot and I want to estimate the pose of an object (similar to a pen) of which I know the heigth.
I think I can use this approach to estimate the distance of the object from the camera, but my question is: from different angle views, the object in the image has different dimensions. How can I get the distance in these (most frequent) cases?
Thank you very much!
Adrian Rosebrock
In that case you may want to see if it’s possible to compute the intrinsic/extrinsic parameters of the camera — it will give you a much more accurate calibration of your camera and be better for different viewing angles.
Gianluca
Hi Adrian,
I will calibrate the camera in the next days, but how can the calibration result parameters be useful for getting a better estimate from different viewing angles?
Thank you
Louie
HI SIR ADRIAN!…
im the one who followed your projects and its working perfectly.
but i have one question what if sir i want to set the range of the detection not to know,…i want to set the detection to object in short range because i have a project also in my class to detect an object but in just short range….like small objects such paper,plastic bottle……..
i hope for your response and thanks also for the projects you shared to us….
Dhiren Motwani
Thank you for such a great tutorial! I am working on a pothole detection project and I will be trying to use this in my project. Can the given code be updated to find the distance from a camera to the center pixel of the image? (i am new to opencv)
Sanjay
Hello Sir,
What if I do not know the initial distance of the object from camera? In this case how can I find the distance of the object from my camera at any point of the time? I need it to make my robot to follow the moving object (for example rolling ball on the surface) continuously.
Thank you
Adrian Rosebrock
You need to perform camera calibration at some point, either via the method in this tutorial or via an intrinsic/extrinsic camera calibration. There is no real way to get around that.
SAN
how can we define Perceived focal length F ?
Adrian Rosebrock
The code automatically defines the perceived focal length once the reference object is detected.
Ben
Hi Adrian,
Can cv2.selectROI be used to measure the apparent width in pixels of the ROI?
It outputs 4 values but I am not sure what they are… x,y, height, width??
Adrian Rosebrock
They are the bounding box coordinates (in pixels). The x and y coordinates are the starting bounding box coordinates. The width and height are the corresponding width and height of the rectangle.
Sivaram
Is focal length same at every time while we run the code for different objects?
Adrian Rosebrock
Are you referring to the initial calibration of the camera?
sonay
Hello Adrian,
I follow you for a long time, it is a great tutorial as always. I am trying to implement this on my rpi model B, but I am getting an error on line 5. can you please help?
Adrian Rosebrock
What specifically is the error? Without knowing the error I cannot provide any suggestions.
sonay
Hello again me 🙂 also I would like to implement this code into yolo v3. Can you help me about, should I call yolo inside your code? thank you so much!
Adrian Rosebrock
I would suggest starting with my YOLO object detection post. That tutorial will teach you how to use YOLO with OpenCV. From there you can replace the contour-based object detector with the YOLO object detector.
Kunal Choudhary
Is it possible to use this algorithm to estimate the distance of multiple objects from moving camera in real time?
Adrian Rosebrock
Yes, but you will need to modify the code to detect each of the individual objects.
KUMARESH A
Hello Adrian,
Great work. How to measure size (Height & Width) without the shade of the object taken from camera. If yes, can you help me in this regards.
Regards
Kumaresh A
Adrian Rosebrock
Refer to this tutorial.
vikram kumar
hello sir,how can i measure the distance between the objects in real time using the windows 10
Benjamin Brown
Would this still work if the camera was looking at the paper from an angle?
Adrian Rosebrock
No, it wouldn’t work as well if the camera was angled. In that case take a look at a proper camera calibration using intrinsic/extrinsic parameters. I might be covering that in a future tutorial but I’ll definitely be covering it in my upcoming Computer Vision + Raspberry Pi book.
scott Caldwell
Adrian, I have a problem. I have a picture taken of my back yard that was taken from inside my back yard fence but I would like to know the exact distance and placement the camera was at when picture was taken. I can measure anything in the picture to get reference dimensions. Can you lead me to someone who could figure this out please?
Adrian Rosebrock
I would place, at a bare minimum, of four different markers in your backyard. Compute the distance between each one of the markers. For any photo being taken you should be able to detect at least one of the markers. Based on that, you can compute the relative distance to all other makers and determine your location.
Zeeshan Ahmed
Hi! can you tell me how can i calculate distance to any object in formula { focal length = (pixel*distance) / width} from the moving camera traveling from Point A to Point B at n speed?
Shipra
Hi Adrian,
It is mentioned in the comments :
“#find the contours in the edged image and keep the largest one;
# we’ll assume that this is our piece of paper in the image”
The bounding box isn’t being accurately calculated when there is another edged background. For e.g if I have a whiteboard behind the paper, it returns the bounding box of the whiteboard instead of the paper. How can I fix this problem?
Thanks
ishani
Hi Adrian, Thank you very much for your clear explanations. Do you also have a tutorial on stereo vision?
Thank you !
Adrian Rosebrock
Sorry, I do not have any tutorials on stereo vision.
Ali Mehmood
Hello Adrian, I have a problem.
I have an image, and I want to find distance between camera and a particular object in that image. There may be some other objects too in that image but I have used ML algorithm to find that particular object and get a bounding box on it. I have following data:
Roughly fitted bounding box around the object in the image.
I know actual dimensions of that object in real world.
I also have meta data of image (like focal length of camera etc.)
I can also take a photograph of that object on a specific distance.
My question is:
Will this work if i change camera? Or would I have to make sure that camera remains same every time? Would I have to calibrate every time I use a different camera?
Godwin P
Hi, I like to track a target from the webcam by developing in javascript with opencv.js can you guide me?
Adrian Rosebrock
Sorry, I don’t have any experience with OpenCV.js.
Rizqi Gunawan
I am working on navigation system for multirotor that will fly below 10 meters, there will be a landing pad with red color 3mx3m then i need to move the drone exactly 15m away from that location. can i used this method to measure the altitude ?
Adrian Rosebrock
You should use a more accurate camera calibration by computing the intrinsic/extrinsic camera parameters.
kartik
As always,Great article Adrian. Is their any way to calculate the distance of an object from the camera which is placed at some angle??.
Rajii
Thanks adrian.
I have utilized the content.
now i can measure object – camera 90 degree incident distance .
is there any method to calculate object – camera non 90 degree incidents.
regards
suriyaa
hi sir,
iam from big fan of your blog which is very use usefull in my projects
can you able to post the blog of detecting distance from camera to object in real live stream
Brhayan Liberato Tafur
Hello Adrian, this is excellent blog congratulations, I have a question, you mention intrinsic and extrinsic parameters of camera. In python OpenCV and MATLAB have algorithms for get these parameters (camera matrix), how use these parameters in other code? what should I do for use this in other code? Could you give me an example of how to use them so that my camera is always calibrated and can take a video.
Thaks and Greetings from Colombia
Adrian Rosebrock
I’ll try to cover intrinsic/extrinsic camera calibration in a separate tutorial.
Sajjad Zaidi
Hello Sir,
Is it necessary to resize images to a lower resolution e.g. 600 x 800 in the resources provided. Is there any other work around?
hasan
hi Adrian,
i wanna estimate to object distance from source in live video for guidance & navigation it,
but your method is based on known width 🙁
what can i do to solve this problem?
thank,
Emre
Hi Adrian,
If we selected control points within a successive frame(video) and control points are selected from corner or edge that is easy for tracking, how can we find the distance by tracking these control points?
Ardhika Nofardiansa
can i use this code for real time video?
Kartik Malhotra
Amazing post as always Can you please share me your knowledge on how to calculate 3d object distance like what tesla do finding depth of field using 2 camera.
David B
Hi Adrian,
Thank you so much for this and other tutorials you create, this is by far the best series of tutorials online. You describe everything step by step and explain it very well!!.
Thanks again, and I would like to keep reading you!.
Adrian Rosebrock
Thanks David, I really appreciate that 🙂
Josef
Hey Adrian! Thanks alot for the post it was really helpful!
but can i apply this to live stream from webcam? and if so can you tell me how?
Thanks again!
Adrian Rosebrock
You should start by learning how to access your webcam. From there you can start integrating the code into a video stream.
Frank
Dear Adrian
I want to thank you for your excellent blog on opencv. The tutorials are the best you can get!
I am trying to combine Ball Tracking script with the camera to object distance so that a robot car can follow the ball
Any pointers on that?
Adrian Rosebrock
That exact project is covered inside Raspberry Pi for Computer Vision. I suggest you start there.