使用opencv和python计算相机到物体的距离 [英] camera to object distance calculation using opencv and python

查看:165
本文介绍了使用opencv和python计算相机到物体的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在四轴飞行器上使用Microsoft Lifecam HD 3000进行自动降落.我希望计算四轴飞行器上的机载摄像头和上面印有矩形的着陆垫之间的距离.然后可以将该距离作为错误反馈给PID控制器.我正在使用python对此进行编码.谁能建议一种简单的方法来找到距离.

I am using Microsoft Lifecam HD 3000 on my quadcopter for autonomous landing . I wish to calculate the distance between the onboard camera on the quadcopter and a landing pad which has a rectangle printed on it. The distance can then be fed as error to the PID controller. i am using python to code this. Could anyone suggest a simple way to find the distance.

PS:我不需要距离的确切值

PS: I don't need the exact value of distance

推荐答案

如果着陆矩形的大小已知/恒定,则可以做到

If the landing rectangle size is known/constant then it is doable

  1. 一些要准备的数据

在已知距离处拍摄着陆矩形的图像.将 Microsoft Lifecam HD 3000 与直升机上的设置相同(或单独使用),并以像素为单位计算/测量矩形大小并将其存储.所以你有:

Take image of the landing rectangle at known distance. Use your Microsoft Lifecam HD 3000 with the same setup as on the copter (or take it by itself) and compute/measure the rectangle size in pixels and store it. So you have:

  • xs0,ys0 -矩形大小[px]
  • d0 -距相机图像的距离[m]
  • xs0,ys0 - rectangle size [px]
  • d0 - distance from camera image was taken [m]

拍摄图像

在矩形上找到并尝试将其投影为垂直于相机视图轴的矩形.然后以像素 xs,ys

Find the rectangle on it and try to project it as it was taken perpendicular to camera view axis. Then compute its size in pixels xs,ys

计算距离

使用三角形相似度

此图像是为此 Q/A 完成的:根据视觉尺寸指定速度可以帮上忙.所以

this image was done for this Q/A: Specifiyng speed from visual size which can be of help. So

  • h 是每个轴上的矩形大小(以像素为单位)
  • z 是距相机焦点的距离
  • h is the rectangle size in pixels in each axis
  • z is the distance from camera focus point

所以改写成你上面的名字

so rewritten to names above you get

  • dx = d0 * xs/xs0 [m]
  • dy = d0 * ys/ys0 [m]
  • dx=d0*xs/xs0 [m]
  • dy=d0*ys/ys0 [m]

dx dy 应该相同,但是如果您的图像不是垂直的或有其他失真,则它们会有所不同.因此,距离是平均值还是最小值或最大值取决于安全原因.例如

dx and dy should be the same but if your image is not take perpendicular or have other distortions then they will be a bit different. So distance is average of it or min or max depends on safety reasons. For example

  • d = 0.5 *(dx + dy) [m]

[注释]

距离是从相机的投影点开始测量的,因此,如果您不知道其位置,请从距已知点的2个已知距离中获取矩形图像,然后计算焦点位置...

The distances are measured from projection point of camera so if yo do not know its position take rectangle image from 2 known distances from known point and compute the focus position ...

这篇关于使用opencv和python计算相机到物体的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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