1

I need to convert the distance between object and camera from pixels to meter/cm frame by frame and then calculate the speed of moving object. In the first frame, the distance of object from camera is 4 meter, and Focal length FL 8mm, width of the object is 0.0373 meters. What I did first to calculate the projection of object:

focal_length_px = 8000/4.8  #Focal length(px): 8mm / (4.8 µm / px) = 1667px =: f

Object :

    object_reall_width = 0.0373 # Width: 0.0373m
    distance = 4       # Distance: 4m
    obj_width_px = (object_reall_width/distance) * focal_length_px  # Projection of object(px): (0.0373 m / 4 m) * focal_length_px = 15.5 px

After object detection, I want to calculate the distance. I used the distance_finder() code in this link.

def distance_finder(focal_length, real_obj_width, obj_width_in_frame):  
    distance = (real_obj_width * focal_length) / obj_width_in_frame  
    return distance

and the output is:

obj_width_in_frame = 17
obj_dst = distance_finder(focal_length_px, obj_width_px, obj_width_in_frame)  # obj_dst = 1523.6928104575165

My question is: how to convert the object distance in the frame from the camera (obj_dst = 1523.69) from pixels to meters/cm?

and then calculate the speed of object in the frame.

S.EB
  • 1,966
  • 4
  • 29
  • 54
  • how can an object have a distance in pixels away from a camera? pixels are never ever used for such figures. – Christoph Rackwitz Feb 06 '23 at 08:28
  • please explain `distance_finder`. the result is [m] * [px] / [px] so the result is meters already, or is supposed to, unless any involved values are the wrong order of magnitude. – Christoph Rackwitz Feb 06 '23 at 08:33
  • Have you calibrated your camera? I see a lot of people are posting various solutions, but if OP hasn't calibrated their camera properly, there is really no accurate way to calculate the distance between the camera and the object. Try this tutorial: https://learnopencv.com/camera-calibration-using-opencv/ – Dimitar Feb 06 '23 at 10:18
  • calibration is just a more accurate procedure to estimate focal length (and lens distortion!). given a sufficiently well-built camera, **nominal** focal length __can__ be calculated from nominal design parameters. calibration doesn't change the math (which is very fundamental math here), it just gives you better values to plug into the equations. – Christoph Rackwitz Feb 06 '23 at 10:23

2 Answers2

2
obj_dst = 1523.69 
# [cm] = [px]* 2.54 / DPI 
distance = obj_dst*2.54/96
# 96 is DPI, 2.54 is inch to cm
print(distance,"cm")
kimhyunju
  • 309
  • 1
  • 7
  • One more question. The `obj_width_in_frame` is object width. what about it (x,y) position? the coordinates are not considered for distance computation? – S.EB Feb 06 '23 at 06:44
  • I don't know the details, but I know that the coordinates are not considered in calculating the shooting distance. – kimhyunju Feb 06 '23 at 07:01
  • simply applying "96 dpi" is (1) arbitrary and (2) and has no relation at all to the pinhole camera model, which is the model that applies here. please explain how this answer can possibly make sense. – Christoph Rackwitz Feb 06 '23 at 08:30
2

We discussed the math before. Now you just need to rearrange some equations.

Pinhole camera model.

  • Focal length (mm): 8 mm
  • Sensor pixel pitch: 4.8 µm/px
  • Focal length (px): 8 mm / (4.8 µm/px) = 1667 px =: f

Object:

  • Physical width: 0.0373 m
  • Apparent width: 17 pixels

Solve for physical distance, let's call it d.

  • (0.0373 m / d) * f = 17 px, which we already had (replace previous 4 meters by d, insert 17 px as known)
  • d = (0.0373 m) / (17 px / f) = 0.0373 m * f / 17 px

=> d ~= 3.66 meters

This equation is already implemented in your function distance_finder(). It is the same expression.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36