在OpenCV中查找长宽比已知的矩形的距离 [英] Finding distance of rectangle with known aspect ratio in OpenCV

查看:394
本文介绍了在OpenCV中查找长宽比已知的矩形的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究OpenCV程序,以查找从照相机到具有已知纵横比的矩形的距离.找到从前向视图中看到的矩形的距离就可以了:

I'm working on an OpenCV program to find the distance from the camera to a rectangle with a known aspect ratio. Finding the distance to a rectangle as seen from a forward-facing view works just fine:

实际距离非常接近于此计算得出的距离:

The actual distance is very close to the distance calculated by this:

            wtarget · pimage
d = c ——————————————————————————
       2 · ptarget · tan(θfov / 2)

其中 w target 是目标的实际宽度(以英寸为单位), p image 是目标整个图像的像素宽度, p target 是检测到的四边形的最大宽度的长度(以像素为单位),θ fov 是网络摄像头的视野.然后将其乘以某个常数 c .

Where wtarget is the actual width (in inches) of the target, pimage is the pixel width of the overall image, ptarget is the length of the largest width (in pixels) of the detected quadrilateral, and θfov is the FOV of our webcam. This is then multiplied by some constant c.

从非正视的角度查看目标矩形时会发生此问题:

The issue occurs when the target rectangle is viewed from a perspective that isn't forward-facing:

这两个方向之间的实际距离差异很小,但检测到的距离相差近2英尺.

The difference in actual distance between these two orientations is small, but the detected distance differs by almost 2 feet.

我想知道的是如何始终如一地计算距离,并考虑到不同的视角.我已经尝试过getPerspectiveTransform,但这需要我知道目标的最终比例-我只知道纵横比.

What I'd like to know is how to calculate the distance consistently, accounting for different perspective angles. I've experimented with getPerspectiveTransform, but that requires me to know the resulting scale of the target - I only know the aspect ratio.

推荐答案

这是您所知道的:

  1. 左上角和右上角之间的距离,单位为英寸(w_target)
  2. 在2D平面上的像素这些角之间的距离(p_target)
  1. The distance between the top left and top right corners in inches (w_target)
  2. The distance between those corners in pixels on a 2D plane (p_target)

所以麻烦的是,当矩形成一定角度时,您没有考虑p_target的收缩距离.例如,当矩形旋转45度时,您会丢失p_target中大约一半的像素,但是您的公式假定w_target是恒定的,因此您高估了距离.

So the trouble is that you're not accounting for the shrinking distance of p_target when the rectangle is at an angle. For example, when the rectangle is turned 45 degrees, you'll lose about half of your pixels in p_target, but your formula assumes w_target is constant, so you overestimate distance.

要解决这个问题,您应该估计盒子被旋转的角度.我不确定从getPerspectiveTransform中提取该信息的简便方法,但是有可能.您可以设置约束优化,决策变量为距离和偏斜角,并在框上的左右两点之间强制采用公制距离.

To account for this, you should estimate the angle the box is turned. I'm not sure of an easy way to extract that information out of getPerspectiveTransform, but it may be possible. You could set up a constrained optimization where the decision variables are distance and skew angle and enforce a metric distance between the left and right points on the box.

最后,无论您做什么,都应确保相机已校准.根据您的应用程序,您也许可以使用 AprilTags 来解决你的问题.

Finally, no matter what you are doing, you should make sure your camera is calibrated. Depending on your application, you might be able to use AprilTags to just solve your problem.

这篇关于在OpenCV中查找长宽比已知的矩形的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆