camera to object distance calculation using opencv and python

為{幸葍}努か 提交于 2021-01-28 18:27:37

问题


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: I don't need the exact value of distance


回答1:


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

  1. some data to prepare

    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 - rectangle size [px]
    • d0 - distance from camera image was taken [m]
  2. take the image

    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

  3. compute the distance

    Use the triangle similarity

    FOV example

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

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

[Notes]

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



来源:https://stackoverflow.com/questions/29249546/camera-to-object-distance-calculation-using-opencv-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!