一种似乎提供足够好的估计的方法 - 在确保输入值的精度的情况下可以证明非常准确,源自这篇文章,它解释了已知传感器的规格(传感器的宽度 - 高度,输出分辨率),和您的焦距,在不考虑缩放因子的情况下,您可以相应地以米或像素为单位计算对象的大小。这是通过计算对象在传感器上占据的像素,然后根据您的需要在实际大小和像素大小之间进行线性转换来实现的。此外,已知物体在现实世界(米)和框架内(像素)的大小,您还可以计算与它的距离。
下面的代码经过测试,它提供了一个近似值 - 与测量值略有偏差 - imo 非常令人满意 - 正如文章所述 -输入的精度至关重要。
#Requires 1)Real object size, 2)Distance from object and returns ---> Object_size in pixels
def given_real_get_pixels(Object_width_meters,Object_height_meters,distance):
Object_height_on_sensor_mms=(Focal_length*Object_height_meters)/distance
#Object_height_on_sensor_mms2
Object_height_pixels=(Sensor_height_pixels*Object_height_on_sensor_mms)/Sensor_height_mms
Object_width_on_sensor_mms=(Focal_length*Object_width_meters)/distance
Object_width_pixels=(Sensor_width_pixels*Object_width_on_sensor_mms)/Sensor_width_mms
print(f'Object_actual_size={Object_width_meters} x {Object_height_meters}(meters) at {distance}(meters) distance coresponds to Object_pixel_size={Object_width_pixels} x {Object_height_pixels}(pixels)')
return Object_height_pixels,Object_width_pixels
#Requires 1)distance from object, 2)Object height in pixels and returns ---> Object_size in meters
def given_pixels_get_real(distance,Object_height_pixels,Object_width_pixels):
Object_height_on_sensor_mms=(Sensor_height_mms*Object_height_pixels)/Sensor_height_pixels
Object_height_meters=((distance*Object_height_on_sensor_mms)/Focal_length)
Object_width_on_sensor_mms=(Sensor_width_mms*Object_width_pixels)/Sensor_width_pixels
Object_width_meters=((distance*Object_width_on_sensor_mms)/Focal_length)
print(f'Object_pixel_size={Object_width_pixels} x {Object_height_pixels}(pixels) at {distance}(meters) distance corresponds to Object_actual_size={Object_width_meters} x {Object_height_meters}(meters)')
return Object_height_meters,Object_width_meters
def given_real_and_pixels_get_distance(Object_width_meters,Object_height_meters,Object_width_pixels,Object_height_pixels):
Object_height_on_sensor_mms=(Sensor_height_mms*Object_height_pixels)/Sensor_height_pixels
distance_height=(Focal_length*Object_height_meters)/Object_height_on_sensor_mms
Object_width_on_sensor_mms=(Sensor_width_mms*Object_width_pixels)/Sensor_width_pixels
distance_width=(Focal_length*Object_width_meters)/Object_width_on_sensor_mms
print(f'For obtaining an Object_pixel_size={Object_width_pixels} x {Object_height_pixels}(pixels) of an object that has Object_actual_size={Object_width_meters} x {Object_height_meters}(meters), distance must be distance_width = {distance_width}(meters) and distance_height= {distance_height}(meters) and mean_distance(suggested)={(distance_width+distance_height)/2}(meters) ')
return distance_width,distance_height
#################################################### Define Specifications ######################################################
#Sensor
Sensor_width_mms=7.410 #mm
Sensor_height_mms=4.980 #mm
Sensor_width_pixels=3088 #pixels
Sensor_height_pixels=2076 #pixels
#Focal length
Focal_length=9.6 #mm
######################################################### Define Parameters ######################################################
distance=125 #0.8 # meters
################################################# GET OBJECT SIZE IN PIXELS ######################################################
Object_width_meters=Object_height_meters=0.001 #meters
Object_height_pixels,Object_width_pixels=given_real_get_pixels(Object_width_meters,Object_height_meters,distance)
################################################# GET OBJECT SIZE IN METERS ######################################################
Object_width_pixels=Object_height_pixels=20
Object_height_meters,Object_width_meters=given_pixels_get_real(distance,Object_height_pixels,Object_width_pixels)
########################################### GET DISTANCE TO OBJECT IN METERS ######################################################
Object_width_meters=0.0005
Object_height_meters=0.001 #meters
Object_width_pixels=10
Object_height_pixels=20
distance_width,distance_height=given_real_and_pixels_get_distance(Object_width_meters,Object_height_meters,Object_width_pixels,Object_height_pixels)
顺便提一下 - 变量的命名可能很麻烦,但并非没有充分的理由.. 稍微解释一下,所有方法都考虑宽度和高度,但每个方法的计算都是独立实现的..