我在python中编写一个代码,将像素中检测到的对象大小与不同距离的实际物理尺寸进行比较,我知道相机的焦距,距离和数字传感器 .

我正在寻找一种解决方案,如果我在捕获的图像中以像素为单位给出物体尺寸,然后知道相机的距离和焦距,我就会给出物理尺寸 . 然后我想比较计算物理尺寸与实际真实物体尺寸 . 例如,我知道物体的实际宽度是20厘米,实际高度是40厘米,距离是1.1米,焦距是8毫米 . 我的对象检测OpenCV代码给我的对象高度是285像素,宽度是132像素 .

我假设物体与相机平行,因此它被视为2D物体 .

这是python代码

actual_H =0.4 #meter
    actual_W =0.2#meter

    F =0.008 #meter_focal_lenght
    Dist = 1.1 #meter distance  
    height=285 #pixel
    width=132  #pixel

    #do calculation
    pix_H =(Dist*float(height))/F
    pix_W =(Dist*float(width))/F
    print feature +"******************"
    print "Pixel Dimension :"+str(float(height))+":"+str(float(width))
    print "Physical Dimension:"+str(pix_H)+":"+str(pix_W)


   #comparision actual and pix
    if (  (pix_H <= actual_H) and (pix_W <= actual_W)):
        return True
    else:
        return False

这个计算是错误的 . 但不知道问题出在哪里 . 我正在使用这个公式

object size in image = focal length * object size / object distance

有帮助吗?