Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure distance to object with a single camera in a static scene

let's say I am placing a small object on a flat floor inside a room.

  • First step: Take a picture of the room floor from a known, static position in the world coordinate system.
  • Second step: Detect the bottom edge of the object in the image and map the pixel coordinate to the object position in the world coordinate system.
  • Third step: By using a measuring tape measure the real distance to the object.

I could move the small object, repeat this three steps for every pixel coordinate and create a lookup table (key: pixel coordinate; value: distance). This procedure is accurate enough for my use case. I know that it is problematic if there are multiple objects (an object could cover an other object).

My question: Is there an easier way to create this lookup table? Accidentally changing the camera angle by a few degrees destroys the hard work. ;)

Maybe it is possible to execute the three steps for a few specific pixel coordinates or positions in the world coordinate system and perform some "calibration" to calculate the distances with the computed parameters?

like image 814
Marcello90 Avatar asked Dec 12 '16 13:12

Marcello90


People also ask

Can cameras tell distance?

The distance between the projection plane and the lens of the camera is image distance. Object distance can be obtained by applying image distance to lens formula. This method is proved to be effective and a single distance measurement can be performed within 3.21ms.

How do you Measure the height width and distance of an object using a camera?

So using the camera and just the camera, it is not possible. You need to know distance somehow, or need a reference. If you are using the application to measure height of items you know the location of, then using GPS, you can find distance, and rest is math.


2 Answers

Most vision libraries (including opencv) have built in functions that will take a couple points from a camera reference frame and the related points from a Cartesian plane and generate your warp matrix (affine transformation) for you. (some are fancy enough to include non-linearity mappings with enough input points, but that brings you back to your time to calibrate issue)

A final note: most vision libraries use some type of grid to calibrate off of ie a checkerboard patter. If you wrote your calibration to work off of such a sheet, then you would only need to measure distances to 1 target object as the transformations would be calculated by the sheet and the target would just provide the world offsets.

like image 40
Sneaky Polar Bear Avatar answered May 29 '23 08:05

Sneaky Polar Bear


If the floor is flat, its equation is that of a plane, let

a.x + b.y + c.z = 1

in the camera coordinates (the origin is the optical center of the camera, XY forms the focal plane and Z the viewing direction).

Then a ray from the camera center to a point on the image at pixel coordinates (u, v) is given by

(u, v, f).t

where f is the focal length.

The ray hits the plane when

(a.u + b.v + c.f) t = 1, 

i.e. at the point

(u, v, f) / (a.u + b.v + c.f)

Finally, the distance from the camera to the point is

p = √(u² + v² + f²) / (a.u + b.v + c.f)

This is the function that you need to tabulate. Assuming that f is known, you can determine the unknown coefficients a, b, c by taking three non-aligned points, measuring the image coordinates (u, v) and the distances, and solving a 3x3 system of linear equations.

From the last equation, you can then estimate the distance for any point of the image.

The focal distance can be measured (in pixels) by looking at a target of known size, at a known distance. By proportionality, the ratio of the distance over the size is f over the length in the image.

like image 156
Yves Daoust Avatar answered May 29 '23 08:05

Yves Daoust