本文介绍了如何使用opencv测量视频中摄像机中检测到的物体的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所知道的是视频中对象的高度和宽度.有人可以指导我使用c或c ++来计算视频中摄像机所检测到的物体的距离吗?有没有算法或公式可以做到这一点?预先感谢

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

推荐答案

Martin Ch说您需要校准相机是正确的,但是正如vasile指出的那样,这不是线性变化.校准相机意味着找到这个矩阵

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];

此矩阵在3维坐标(x,y,z)上进行运算,并将其转换为2维均匀坐标.要转换为常规的欧几里得(x,y)坐标,只需将第一个和第二个分量除以第三个分量即可.那么,这些变量现在在做什么呢?

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::它们的存在是让您可以根据需要更改坐标系.例如,您可能希望相机空间中的原点位于图像的左上方,而世界空间中的原点位于中心.在这种情况下

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;

如果不更改坐标系,则将其保留为0.

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

fx/fy::这些参数以x像素和y像素为单位指定焦距,它们通常接近相同的值,因此您可以为它们赋予相同的值f .这些参数本质上定义了透视效果有多强.假设没有cx和cy是世界坐标到屏幕坐标的映射(您可以从上面的矩阵中自己计算出来)

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;

如您所见,使事物越近越远,越小越远的重要量就是比率 f/z.它不是线性的,但是通过使用同质坐标,我们仍然可以使用线性变换.

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.

简而言之.使用经过校准的相机,并以世界坐标中已知的对象大小,您可以计算出它与相机的距离.如果您缺少其中之一,那是不可能的.不知道世界坐标中物体的大小,您能做的最好的事情就是通过确定xworld/zworld的比率(知道fx)将其屏幕位置映射到世界坐标中的射线.

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).

这篇关于如何使用opencv测量视频中摄像机中检测到的物体的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:07