Search code examples
opencvdistanceobject-detectionrosasus-xtion

how to find object distance from asus xtion pro camera opencv, ROS


Hi i am using an asus xtion pro live camera for my object detection, i am also new to opencv. Im trying to get distance of object from the camera. The Object detected is in 2d image. Im not sure on what should i use to get the information then following up with the calculations to get distance between camera and object detected. Could someone advise me please?


Solution

  • In short: You can't.

    You're losing the depth information and any visible pixel in your camera image essentially transforms into a ray originating from your camera.

    So once you've got an object at pixel X, all you know is that the object somewhere intersects the vector cast based on this pixel and the camera's intrinsic/extrinsic parameters.

    You'll essentially need more information. One of the following should suffice:

    • Know at least one coordinate of the 3D point (e.g. everything detected is on the ground or in some known plane).
    • Know the relation between two projected points:
      • Either the same point from different positions (known camera movement/offset)
      • or two points with significant distance between them (like the two ends of some staff or bar).

    Once you've got either, you're able to use simple trigonometry (rule of three) to calculate the missing values.


    Since I initially missed this being a camera with an OpenNI compatible depth sensor, it's possible to build OpenCV with support for that by definining the preprocessor define WITH_OPENNI when building the library.