怎么将物体与相机之间的距离从像素转换为米?

huangapple go评论57阅读模式
英文:

How to convert the distance between object and camera from pixel for meter?

问题

我需要将物体与摄像机之间的距离从像素转换为米/厘米,逐帧计算移动物体的速度。在第一帧中,物体离摄像机的距离是4米,焦距FL为8毫米,物体的宽度为0.0373米。我首先做的是计算物体的投影:

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

Object :

    object_reall_width = 0.0373 # 宽度: 0.0373m
    distance = 4       # 距离: 4m
    obj_width_px = (object_reall_width/distance) * focal_length_px  # 物体投影(px): (0.0373 m / 4 m) * focal_length_px = 15.5 px

物体检测后,我想计算距离。我使用了此链接中的distance_finder()代码

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

输出是:

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

我的问题是:如何将来自摄像机的帧中的物体距离(obj_dst = 1523.69)从像素转换为米/厘米?

然后计算帧中物体的速度。

英文:

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.

答案1

得分: 2

obj_dst = 1523.69 
# [厘米] = [像素]* 2.54 / DPI 
distance = obj_dst*2.54/96
# 96 是 DPI,2.54 是英寸到厘米的转换
print(distance, "厘米")
英文:
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")

答案2

得分: 2

我们之前讨论过数学。现在你只需要重新排列一些方程。

针孔相机模型。

  • 焦距(毫米):8毫米
  • 传感器像素间距:4.8微米/像素
  • 焦距(像素):8毫米 /(4.8微米/像素)= 1667像素 =:f

物体:

  • 实际宽度:0.0373米
  • 表观宽度:17像素

解算物理距离,我们称之为 d

  • (0.0373米 / d) * f = 17像素,我们已经有了(用d替换先前的4米,插入已知的17像素)
  • d =(0.0373米)/(17像素 / f)= 0.0373米 * f / 17像素

=> d约为3.66米

这个方程已经在你的函数 distance_finder() 中实现。它是相同的表达式。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 11:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定