The method of calculating the focal length f from the matching points of the two images.

Source: Internet
Author: User

The method of calculating the focal length f from the matching points of the two images.

Find two focal length calculation methods from the network
1. The focal length of the two images based on the matrix H (taken from the opencv code of panorama Mosaic)

// Calculate the focal length f from the homography matrix H of the two images. Void focalsFromHomography (const Mat & H, double & f0, double & f1, bool & f0_ OK, bool & f1_ OK) {CV_Assert (H. type () = CV_64F & H. size () = Size (3, 3); const double * h = H. ptr
 
  
(); // H is a vector of double d1, d2; // denominator of double v1, v2; // candidate f1_ OK = true for focal length squared values; d1 = h [6] * h [7]; d2 = (h [7]-h [6]) * (h [7] + h [6]); v1 =-(h [0] * h [1] + h [3] * h [4])/d1; v2 = (h [0] * h [0] + h [3] * h [3]-h [1] * h [1]-h [4] * h [4]) /d2; if (v1 <v2) std: swap (v1, v2); if (v1> 0 & v2> 0) f1 = std: sqrt (std :: abs (d1)> std: abs (d2 )? V1: v2); else if (v1> 0) f1 = std: sqrt (v1); else fse OK = false; f0_ OK = true; d1 = h [0] * h [3] + h [1] * h [4]; d2 = h [0] * h [0] + h [1] * h [1]-h [3] * h [3]-h [4] * h [4 ]; v1 =-h [2] * h [5]/d1; v2 = (h [5] * h [5]-h [2] * h [2]) /d2; if (v1 <v2) std: swap (v1, v2); if (v1> 0 & v2> 0) f0 = std: sqrt (std :: abs (d1)> std: abs (d2 )? V1: v2); else if (v1> 0) f0 = std: sqrt (v1); else f0_ OK = false ;}
 
Use the following test
// Read two graphs Mat img1 = imread (name1); // Mat img2 = imread (name2); // match the vector
 
  
Kp1, kp2; vector
  
   
Matches; meMatch2 (img1, img2, kp1, kp2, matches); // generates a matching point vector
   
    
Points1, points2; get_match_points (kp1, kp2, matches, points1, points2); // four pairs of image points correspond to HomographyMat H = findHomography (points1, points2, 0); double f0; double f1; bool f0_ OK; bool f1_ OK; // calculate the focal length f from the homography matrix H of the two images. FocalsFromHomography (H, f0, f1, f0_ OK, f1_ OK); if (f0_ OK) std: cout <"Figure 1 focal length:" <f0 <'\ n '; if (f1_ OK) std: cout <"Figure 2 focal length:" <f1 <'\ n ';
   
  
 
Result:


Figure 1 focal length: 81.9963
Figure 2 focal length: 992.465
Only Figure 2 is accurate.


-= ----------------- Separation line -------------------------- =-=


2. Focal Length of matching points in two graphs f (from a pole calibration toolkit matlab)

Function [f] = compIntrinsic (ml, mr, width, height) % calculates a focal length % ml. mr is the corresponding point % width in the image, height is the size of the image (used to obtain the focal length and image Center) % options = optimset ('babal', 'off'); a0 = [0 0 0 0, 0]; [af, resnorm, residual, exitflag, output, lambda, Jacob Bian] = lsqnonlin (@ (x) costRectif (x, width, height, ml, mr), a0 ,... [-pi/2,-pi/2,-pi/2,-pi/2,-pi/2,-1.5], [pi/2, pi/2, pi/2, pi/2, pi/2, 1.5], options); counter = 0; while af (6)> 1 | af (6) <-1 counter = counter + 1; disp ('focus out of range; retry (random )... '); a0 = [0 0 0 0 0, 2 * rand-1.0]; [af, resnorm] = lsqnonlin (@ (x) costRectif (x, width, height, ml, mr), a0 ,... [-pi/2,-pi/2,-pi/2,-pi/2,-pi/2,-1.5], [pi/2, pi/2, pi/2, pi/2, pi/2, 1.5], options); if counter> 20 warning ('the focal point in the range is not found ') break endendf = 3 ^ af (6) * (width + height); % disp (['focal length: f = 'num2str (f)]);
Test
F = compIntrinsic (ml, mr, width, height); % ml, mr a matching point of 2xn matrix. width, height is the image length, width disp (['focal length: f = 'num2str (f)]);
Result
Focal Length: f = 892.7031
Basic accuracy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.