8
\$\begingroup\$

I posted this question over on Stack Overflow, but someone suggested this site might be of more help to me.

I'm doing a college project where I'm trying to calculate the focal length of a camera from a photo. The project is part of my final year computing course (that's why I assumed Stack Overflow would be the best place to ask)

I'm using vanishing lines identified by the user to try and calculate the focal length. say you have a cube, the user can select 3 sides to intersect. For each side, 2 lines are drawn to infinity. Where 2 lines intersect = intersection point. This is done for all 3 sides giving 3 intersection points. These points are then used to draw a new triangle and from this triangle to focal length can be calculated as shown in this Research research. But my values are way off. For this image the focal length is known as 18mm but the value I get is 478.634....

enter image description here

Would anyone have any experience with this type of problem?

I have also come across this formula on this website. Does anyone know what

alphaX (fx = (imageWidth)2*tan(alphaX))

would represent.

Any help would be appreciated

UPDATE:.

Ive made a few pics to show how I calculate the focal length (images not to scale or size). Starting with a photo of a cube Cube I extend each one of the 3 sides till they intersect (shown by the green dots). Then using those intersection points I construct a rectangle as shown here Intersecting points. Then I calculate the ortho-centre of the triangle. Then using the formula here I calculate the focal length as shown here enter image description here. In my application the focal length is simply a value (integer) that is calculated from the square root of the length of the intersecting points (IP1) to the centre, multiplied by the length from the centre to the opposite midpoint (MP2)

\$\endgroup\$
7
  • \$\begingroup\$ The last part of your question (the meaning of the formula) is addressed at: photo.stackexchange.com/questions/13603/… \$\endgroup\$
    – coneslayer
    Mar 23, 2012 at 11:59
  • 1
    \$\begingroup\$ alphaX will be an angle in radians. You haven't shown your calculations, but if you were passing an angle in degrees into the tan function it could explain the large discrepancy. \$\endgroup\$
    – ChrisF
    Mar 23, 2012 at 11:59
  • \$\begingroup\$ I don't know what you are calculating the formula "(fx = " (imageWidth)2*tan(alphaX))" on BUT as shown it is incredibly dangerous. If you use it as shown some systems with give the intended answer, some will return error messages and others will return erroneous results with no warning. I've recently participated in an extensive discussion re expressions which leave assumed multipliers out (as here) and which presume that processing order will follow certain rules (not an issue here). THen use radians. Then ... \$\endgroup\$ Mar 23, 2012 at 13:17
  • \$\begingroup\$ Ive madea few pics to show how I calculate the focal length (images not to scale or size). \$\endgroup\$
    – heyred
    Mar 23, 2012 at 14:23
  • \$\begingroup\$ Sorry guys, I should have made myself a bit more clear. Im not actually using the (fx = " (imageWidth)2*tan(alphaX) formula. Im actually using the formula as per my update above. I was just wondering if this (fx = " (imageWidth)2*tan(alphaX) formula would work better. But seeing as you have to calculate the alpha from the height of the camera etc., this wont suit me as I "pretend" not to know any camera parameters in trying to solve for the focal length. \$\endgroup\$
    – heyred
    Mar 23, 2012 at 14:51

3 Answers 3

10
\$\begingroup\$

(1) By inspection it is "clear" [tm] that a relatively wide angle lens is in use. In a 35mm full frame system a guesstimate far far far closer to 18mm than 480 mm would be arrived at.

(2) Without having got my brain fully around the triple vanishing point method described in a reference, I would think that for vanishing points to be relevant you would need to deal with lines which were parallel in reality but rendered apparently not so by perspective OR perhaps wit lines whose actual interposing angle was known and which were rendered at another apparent angle in the image. However, in the example photo given, the actual angle between the two walls on either side of the island seems to be arbitrary and unknown (except to their builders) and that changing this angle changes the answer you would get. HOWEVER, I may just be wholly missing the point :-).

(3) I am reasonably certain that the formula quoted by the original poster in your reference as

  • fx = (imageWidth)2*tan(alphaX))

Is intended to read

  • fx = (imageWidth) / (2 * tan(alphaX) )

Where:

fx = focal length
imagewidth = sensor width
alphaX = HALF angle of view

This is consistent with the diagram below whose precision of rendition will hopefully be excused or ignored. The above formula then reduces to a simple geometric statement.
I have used slightly different terms to assist and/or confuse the mental process :-)

enter image description here

HALF the angle of view = alpha has tan(alpha) = Y/X = height/distance.
The identical tan is produced by half the image height on the sensor / focal length.
So
(sensor_height/2) / focal_length = height/distance = tan (alpha)
so
(IW/2) / fx = tan_alpha
invert
fx / (Iw/2) = 1/tan_alpha
fx = Iw/(2 x tan_alpha)

\$\endgroup\$
10
\$\begingroup\$

You're missing a calibration step. You're calculation is probably correct, the focal length is indeed 478.634, it's just not 478.634 millimeters.

The vanishing points you've calculated in image space have no units unless you know the size of the camera sensor. So from a formula with no units you can't apply mm to the answer and expect it to make sense!

Without calibration for sensor size your best hope is to calculate the angle of view rather than the focal length, as the units cancel (angles have no real units as they're based on ratios).

Provided your assumption of parallel lines holds then I believe it's possible to calculate the angle of view though I've never tried it.

\$\endgroup\$
6
  • \$\begingroup\$ Without calibration for sensor size...... Is there a way to calculate sensor size or is this something that I can find on the manufacturers website? And yeah Ive been starting to think it had something to do with my measurements (or lack thereof) because all the research Ive done indicates that this is indeed how the focal length can be calculated. \$\endgroup\$
    – heyred
    Mar 23, 2012 at 16:50
  • \$\begingroup\$ Ok Ive found the sensor size for my Sony A330 (its 23.6 x 15.8mm - that was easier than I thought). Any idea how I can use this new information to scale my focal length to the right size? \$\endgroup\$
    – heyred
    Mar 23, 2012 at 16:55
  • \$\begingroup\$ +1: Matt, I think you've made a good point. Based on the formulas I would assume that the units are "pixels" but, the answer of 478.6 pixels would be equivalent to 18mm only if the pixel density of the sensor is ~675 ppi which would mean awful big pixels for a digital sensor. Of course something else could be amiss. \$\endgroup\$ Mar 23, 2012 at 16:58
  • \$\begingroup\$ ...the answer of 478.6 pixels would be equivalent to 18mm only if the pixel density of the sensor is ~675 ppi which would mean awful big pixels for a digital sensor... The example I put up of 478.6.. was actually one of the smaller values that I got. Usually the values are between 2 - 4 thousand. So if I have a 10.2 MP sensor with a sensor size of 23.6 x 15.8mm, how do I calculate the PPI? And how can I then use this to convert to mm as you have done here _ the answer of 478.6 pixels would be equivalent to 18mm only if the pixel density of the sensor is ~675 ppi_... \$\endgroup\$
    – heyred
    Mar 23, 2012 at 17:52
  • \$\begingroup\$ You don't need to know the sensor actual size to get the 35 mm equivalent focal length. For that just assume the sensor was 35x24 mm. \$\endgroup\$ Mar 24, 2012 at 0:06
4
\$\begingroup\$

I appreciate this is an old post, but I'd like to share a tool which does all of this for you and more, in case someone else comes across this question like I did.

fSpy is a free tool allowing you to import a photo and automatically calculate camera position, rotation, and focal length using 1, 2 or 3-point perspective.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.