Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

OpenCV: How-to calculate distance between camera and object using image?

I am a newbie in OpenCV. I am working with the following formula to calculate distance:

distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels)
                          ----------------------------------------------------------------
                                object height (pixels) * sensor height (mm)

Is there a function in OpenCV that can determine object distance? If not, any reference to sample code?

question from:https://stackoverflow.com/questions/14038002/opencv-how-to-calculate-distance-between-camera-and-object-using-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to calculate distance given an object of known size

You need to know one of 2 things up front

  • Focal-length (in mm and pixels per mm)
  • Physical size of the image sensor (to calculate pixels per mm)

I'm going to use focal-length since I don't want to google for the sensor datasheet.

Calibrate the camera

Use the OpenCV calibrate.py tool and the Chessboard pattern PNG provided in the source code to generate a calibration matrix. I took about 2 dozen photos of the chessboard from as many angles as I could and exported the files to my Mac. For more detail check OpenCV's camera calibration docs.

Camera Calibration Matrix (iPhone 5S Rear Camera)

RMS: 1.13707201375
camera matrix:
[[  2.80360356e+03   0.00000000e+00   1.63679133e+03]
 [  0.00000000e+00   2.80521893e+03   1.27078235e+03]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
distortion coefficients:  [ 0.03716712  0.29130959  0.00289784 -0.00262589 -1.73944359]
  • f_x = 2803
  • f_y = 2805
  • c_x = 1637
  • c_y = 1271

Checking the details of the series of chessboard photos you took, you will find the native resolution (3264x2448) of the photos and in their JPEG EXIF headers, visible in iPhoto, you can find the Focal Length value (4.15mm). These items should vary depending on camera.

Pixels per millimeter

We need to know the pixels per millimeter (px/mm) on the image sensor. From the page on camera resectioning we know that f_x and f_y are focal-length times a scaling factor.

f_x = f * m_x
f_y = f * m_y

Since we have two of the variables for each formula we can solve for m_x and m_y. I just averaged 2803 and 2805 to get 2804.

m = 2804px / 4.15mm = 676px/mm 

Object size in pixels

I used OpenCV (C++) to grab out the Rotated Rect of the points and determined the size of the object to be 41px. Notice I have already retrieved the corners of the object and I ask the bounding rectangle for its size.

cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));

Small wrinkle

The object is 41px in a video shot on the camera @ 640x480.

Convert px/mm in the lower resolution

3264/676 = 640/x
x = 133 px/mm

So given 41px/133px/mm we see that the size of the object on the image sensor is .308mm .

Distance formula

distance_mm = object_real_world_mm * focal-length_mm / object_image_sensor_mm
distance_mm = 70mm * 4.15mm / .308mm
distance_mm = 943mm

This happens to be pretty good. I measured 910mm and with some refinements I can probably reduce the error.

Feedback is appreciated.

Similar triangles approach

Adrian at pyimagesearch.com demonstrated a different technique using similar triangles. We discussed this topic beforehand and he took the similar triangles approach and I did camera intrinsics.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...