Search code examples
imageopencvimage-processingcamerawebcam

Distance to Object Webcam C920HD or use OpenCV calibrate.py


I am trying to determine the distance of an object and the height of an object towards my camera. Is it possible or do I need to use OpenCV calibrate.py to gather more information? I am confused because the Logitech C920HD has 3 MP and scales to 15 MP via software.

I have following info:

  • Resolution (pixel): 1920x1080
  • Focal Length (mm): 3.67mm
  • Pixel Size (µm): 3.98
  • Sensor Size (inches): 1/2.88
  • Object real height (mm): 180
  • Object image height (px): 370

I checked this formula:Distance

distance (mm) = 3.67(mm) * 180(mm) * 1080(px) / 511 (px) * (1/2.88)(inches)*2.54 (mm/inches)

Which gives me 15.8 cm. Altough it should be about 60cm.

What am I doing wrong?

Thanks for help!


Solution

  • Your formula looks the correct one, however, for it to hold over the entire image plane, you should correct lens distortions first, e.g., following the answer

    Camera calibration, reverse projection of pixel to direction

    Along the way, OpenCV lens calibration module will estimate your true focal length.

    Filling the formula gives

    Distance = 3.67 mm * 180 mm * 1080/511 / sensor_height_mm = 1396 mm^2 / sensor_height_mm
    

    Leaving sensor_height_mm unknown. Given your camera is 16:9 format

    w^2 + h^2 = D^2
    (16x)^2+(9x)^2 = D^2
    <=>
    x = sqrt( D^2/337 )
    <=>
    h = 9x = 9*sqrt( D^2/337 )
    

    Remember the rule of 16:

    https://photo.stackexchange.com/questions/24952/why-is-a-1-sensor-actually-13-2-%C3%97-8-8mm/24954

    Most importantly, a 1/2.88" sensor has 16/2.88 mm image circle diameter instead of 25.4/2.88 mm. Funny enough, the true image circle diameter is metric. Thus the sensor diameter is

    D = 16 mm/ 2.88 = 5.556 mm
    

    and

    sensor_height_mm = h = 2.72 mm
    

    giving

    Distance = 513 mm
    

    Note, that this distance is measured with respect to the lenses first principal point and not the sensor position or the lens front element position.

    As you correct the barrel distortion, the reading should get more accurate. It's quite a lot for this camera. I have similar.

    Hope this helps.