How to calculate the focal length from an image

15 views (last 30 days)
How do I get the focal length of an image taken using MATLAB (getsnapshot)

Accepted Answer

neenu jose
neenu jose on 10 Jan 2012
if the camera is of fixed focal length,using the camera caliberation toolbox,we can find the focal length as well as the extrinsic parameters.for that,use a chekerboard,take different photos of it at different angles and store them with a common basename..eg:image1, image2 etc..and add this folder to the current directory of matlab.then use the caliberation tool box.
but i dont know the case if the camera is autofocus..

More Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 20 Dec 2011
From an image you need to know the direction (phi,theta, or their proper position [x,y,z]) to a number of identifiable objects in your image. From there onwards it is just to get going!
With the camera at [0,0,0] for slight simplicity, this should be the function to minimize
function err = opt_cal(pars,imsiz,r_objs,im_pos)
err = 0;
rot1 = pars(1);
rot2 = pars(2);
rot3 = pars(3);
f = pars(4);
dudv = pars(5:6);
x = r_objs(:,1);
y = r_objs(:,2);
z = r_objs(:,3);
R = rotation_matrix(rot1,rot2,rot3);
for i1 = 1:length(z),
e_i = R*[x;y;z]/norm([x;y;z]);
theta_i = acos(e_i(3));
phi_i = atan2(e_i(2),e_i(1));
[u_par,v_par] = f*[cos(phi_i),sin(phi_i)]*tan(theta) + imsiz/2+dudv;
err = err + (u_obj-u_par)^2 + (v_obj-v_par)^2;
end
I think that should do it. Come to think about it: What have you done to solve your problem?
HTH
  2 Comments
Walter Roberson
Walter Roberson on 5 Jan 2012
opt_cal is the function name that Bjorn gave to the code. A line that starts with "function" defines a new function.

Sign in to comment.


Image Analyst
Image Analyst on 20 Dec 2011
I think you'd have to have some objects of known distance from the camera. Because you can get a scene that looks pretty much the same by being up close with a short focal length lens, or far away with a long focal length lens. But if you're looking at something with different distances they'd look different. Think of how an array of pegs would look with different focal lengths. But just a flat scene (say a picture on a wall) wouldn't look much different. I think you need to study up on your geometrical optics first and then come back here, maybe after trying in the sci.optics newsgroup.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!