user1857375
user1857375

Reputation: 31

how can i measure distance of an detected object from camera in video using opencv?

All i know is that the height and width of an object in video. can someone guide me to calculate distance of an detected object from camera in video using c or c++? is there any algorithm or formula to do that? thanks in advance

Upvotes: 1

Views: 4611

Answers (2)

plan9assembler
plan9assembler

Reputation: 2984

i don´t think it is easy if have to use camera only,

consider about to use 3rd device/sensor like kinect/stereo camera,

then you will get the depth(z) from the data.

https://en.wikipedia.org/wiki/OpenNI

Upvotes: 0

Hammer
Hammer

Reputation: 10329

Martin Ch was correct in saying that you need to calibrate your camera, but as vasile pointed out, it is not a linear change. Calibrating your camera means finding this matrix

camera_matrix = [fx,0 ,cx,
                 0,fy,cy,
                 0,0, 1];

This matrix operates on a 3 dimensional coordinate (x,y,z) and converts it into a 2 dimensional homogeneous coordinate. To convert to your regular euclidean (x,y) coordinate just divide the first and second component by the third. So now what are those variables doing?

cx/cy: They exist to let you change coordinate systems if you like. For instance you might want the origin in camera space to be in the top left of the image and the origin in world space to be in the center. In that case

cx = -width/2;
cy = -height/2;

If you are not changing coordinate systems just leave these as 0.

fx/fy: These specify your focal length in units of x pixels and y pixels, these are very often close to the same value so you may be able to just give them the same value f. These parameters essentially define how strong perspective effects are. The mapping from a world coordinate to a screen coordinate (as you can work out for yourself from the above matrix) assuming no cx and cy is

xsc = fx*xworld/zworld;
ysc = fy*yworld/zworld;

As you can see the important quantity that makes things bigger closer up and smaller farther away is the ratio f/z. It is not linear, but by using homogenous coordinates we can still use linear transforms.

In short. With a calibrated camera, and a known object size in world coordinates you can calculate its distance from the camera. If you are missing either one of those it is impossible. Without knowing the object size in world coordinates the best you can do is map its screen position to a ray in world coordinates by determining the ration xworld/zworld (knowing fx).

Upvotes: 1

Related Questions