问题
I am programming a code in python that compare the detected object size in the image in pixels with its actual physical dimensions from different distances in a way that I know the focal length, distance and digital sensor of the camera.
I am looking for a solution which gives me the physical dimension if I give the object size in pixel in the captured image, then know the distance to the camera and focal length. Then I would like to compare that calculate physical dimension with the actual real object dimension. For example, I know that the real width of the object is 20cm, real height is 40 cm, the distance is 1.1 m, and the focal length is 8mm. My object detection OpenCV code gives me object height is 285 pixels, width is 132 pixels.
I assume the object is parallel to the camera so it is considered as a 2D object.
here is the python code
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
This calculation is wrong. But dont know where is the problem. Im using this Formula
object size in image = focal length * object size / object distance
Any help?
来源:https://stackoverflow.com/questions/43844117/calculate-the-physical-object-size-in-m-knowing-its-distance-focal-length-and-p