How to calculate the focal length from an image
Show older comments
How do I get the focal length of an image taken using MATLAB (getsnapshot)
Accepted Answer
More Answers (2)
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
neenu jose
on 5 Jan 2012
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.
Image Analyst
on 20 Dec 2011
0 votes
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.
Categories
Find more on Image Preview and Device Configuration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!