OpenCV: How-to calculate distance between camera and object using image?

后端 未结 3 1233
挽巷
挽巷 2020-11-28 23:21

I am a newbie in OpenCV. I am working with the following formula to calculate distance:

distance to object (mm) = focal length (mm) * real height of the obje         


        
相关标签:
3条回答
  • 2020-11-28 23:54

    How to calculate distance given an object of known size

    You need to know one of 2 things up front

    • Focal-length (in mm and pixels per mm)
    • Physical size of the image sensor (to calculate pixels per mm)

    I'm going to use focal-length since I don't want to google for the sensor datasheet.

    Calibrate the camera

    Use the OpenCV calibrate.py tool and the Chessboard pattern PNG provided in the source code to generate a calibration matrix. I took about 2 dozen photos of the chessboard from as many angles as I could and exported the files to my Mac. For more detail check OpenCV's camera calibration docs.

    Camera Calibration Matrix (iPhone 5S Rear Camera)

    RMS: 1.13707201375
    camera matrix:
    [[  2.80360356e+03   0.00000000e+00   1.63679133e+03]
     [  0.00000000e+00   2.80521893e+03   1.27078235e+03]
     [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
    distortion coefficients:  [ 0.03716712  0.29130959  0.00289784 -0.00262589 -1.73944359]
    
    • f_x = 2803
    • f_y = 2805
    • c_x = 1637
    • c_y = 1271

    Checking the details of the series of chessboard photos you took, you will find the native resolution (3264x2448) of the photos and in their JPEG EXIF headers, visible in iPhoto, you can find the Focal Length value (4.15mm). These items should vary depending on camera.

    Pixels per millimeter

    We need to know the pixels per millimeter (px/mm) on the image sensor. From the page on camera resectioning we know that f_x and f_y are focal-length times a scaling factor.

    f_x = f * m_x
    f_y = f * m_y
    

    Since we have two of the variables for each formula we can solve for m_x and m_y. I just averaged 2803 and 2805 to get 2804.

    m = 2804px / 4.15mm = 676px/mm 
    

    Object size in pixels

    I used OpenCV (C++) to grab out the Rotated Rect of the points and determined the size of the object to be 41px. Notice I have already retrieved the corners of the object and I ask the bounding rectangle for its size.

    cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
    

    Small wrinkle

    The object is 41px in a video shot on the camera @ 640x480.

    Convert px/mm in the lower resolution

    3264/676 = 640/x
    x = 133 px/mm
    

    So given 41px/133px/mm we see that the size of the object on the image sensor is .308mm .

    Distance formula

    distance_mm = object_real_world_mm * focal-length_mm / object_image_sensor_mm
    distance_mm = 70mm * 4.15mm / .308mm
    distance_mm = 943mm
    

    This happens to be pretty good. I measured 910mm and with some refinements I can probably reduce the error.

    Feedback is appreciated.

    Similar triangles approach

    Adrian at pyimagesearch.com demonstrated a different technique using similar triangles. We discussed this topic beforehand and he took the similar triangles approach and I did camera intrinsics.

    0 讨论(0)
  • 2020-11-29 00:04

    there is no such function available in opencv to calculate the distance between object and the camera. see this : Finding distance from camera to object of known size

    You should know that the parameters depend on the camera and will change if the camera is changed.

    0 讨论(0)
  • 2020-11-29 00:08
    1. To get a mapping between the real world and camera without any prior information of the camera you need to calibrate the camera...here you can find some theory
    2. For calculating the depth i.e. distance between camera and object you need at least two images of the same object taken by two different cameras...which is popularly called the stereo vision technique..
    0 讨论(0)
提交回复
热议问题