create opencv camera matrix for iPhone 5 solvepnp

Isaac picture Isaac · Feb 4, 2013 · Viewed 7.3k times · Source

I am developing an application for the iPhone using opencv. I have to use the method solvePnPRansac:

http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html

For this method I need to provide a camera matrix:
__ __
| fx 0 cx |
| 0 fy cy |
|_0 0 1 _|

where cx and cy represent the center pixel positions of the image and fx and fy represent focal lengths, but that is all the documentation says. I am unsure what to provide for these focal lengths. The iPhone 5 has a focal length of 4.1 mm, but I do not think that this value is usable as is.

I checked another website:

http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html

which shows how opencv creates camera matrices. Here it states that focal lengths are measured in pixel units.

I checked another website:

http://www.velocityreviews.com/forums/t500283-focal-length-in-pixels.html

(about half way down) it says that focal length can be converted from units of millimeters to pixels using the equation: fx = fy = focalMM * pixelDensity / 25.4;

Another Link I found states that fx = focalMM * width / (sensorSizeMM); fy = focalMM * length / (sensorSizeMM);

I am unsure about these equations and how to properly create this matrix.

Any help, advice, or links on how to create an accurate camera matrix (especially for the iPhone 5) would be greatly appreciated,

Isaac

p.s. I think that (fx/fy) or (fy/fx) might be equal to the aspect ratio of the camera, but that might be completely wrong.

UPDATE:

Pixel coordinates to 3D line (opencv)

using this link, I can figure out how they want fx and fy to be formatted because they use it to scale angles relative to their distance from the center. therefore, fx and fy are likely in pixels/(unit length) but im still not sure what this unit length needs to be, can it be arbitrary as long as x and y are scaled to each other?

Answer

Francesco Callari picture Francesco Callari · Feb 5, 2013

You can get an initial (rough) estimate of the focal length in pixel dividing the focal length in mm by the width of a pixel of the camera' sensor (CCD, CMOS, whatever).

You get the former from the camera manual, or read it from the EXIF header of an image taken at full resolution. Finding out the latter is a little more complicated: you may look up on the interwebs the sensor's spec sheet, if you know its manufacturer and model number, or you may just divide the overall width of its sensitive area by the number of pixels on the side.

Absent other information, it's usually safe to assume that the pixels are square (i.e. fx == fy), and that the sensor is orthogonal to the lens's focal axis (i.e. that the term in the first row and second column of the camera matrix is zero). Also, the pixel coordinates of the principal point (cx, cy) are usually hard to estimate accurately without a carefully designed calibration rig, and an as-carefully executed calibration procedure (that's because they are intrinsically confused with the camera translation parallel to the image plane). So it's best to just set them equal to the geometrical geometrical center of the image, unless you know that the image has been cropped asymmetrically.

Therefore, your simplest camera model has only one unknown parameter, the focal length f = fx = fy.

Word of advice: in your application is usually more convenient to carry around the horizontal (or vertical) field-of-view angle, rather than the focal length in pixels. This is because the FOV is invariant to image scaling.