I am trying to write a program using opencv to calculate the distance from a webcam to a one inch white sphere. I feel like this should be pretty easy, but for whatever reason I'm drawing a blank. Thanks for the help ahead of time.
The formula: distance = size_obj * focal_length / size_obj_on_sensor. The whole method depends on figuring out the size of the object as it appears on the sensor given the focal length and the measured object size. Otherwise you have two unknowns.
Google's augmented reality app “Measure” turns ARCore-compatible Android smartphones into digital measuring tapes, as reported by Ars Technica. Using the app appears to be rather simple. Simply launch Measure, point the phone's camera to an object, then pick two points to measure the distance in between.
The relationship between object size and distance is an inverse linear relationship, i.e. size is 1 / distance. This makes sense when you think about it as if you double the distance the size halves.
Steps for Distance Estimation: Capture Reference Image: Measure the distance from the object(face) to the camera, capture a Reference image and note down the measured distance. Measure the object (face) width, make sure that measurement units are kept for reference image and object(face) width. Mine Reference Image.
You can use triangle similarity to calibrate the camera angle and find the distance.
You know your ball's size: D
units (e.g. cm). Place it at a known distance Z
, say 1 meter = 100cm, in front of the camera and measure its apparent width in pixels. Call this width d
.
The focal length of the camera f
(which is slightly different from camera to camera) is then f=d*Z/D
.
When you see this ball again with this camera, and its apparent width is d'
pixels, then by triangle similarity, you know that f/d'=Z'/D
and thus: Z'=D*f/d'
where Z'
is the ball's current distance from the camera.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With