計算兩幅圖的單應矩陣,實現圖像拼接


單應矩陣是一個3x3的矩陣,它有着特殊的屬性可用於特定條件下的雙視角圖像。

我們知道,3D點與它在相機圖像中像素點之間存在的關系可以用3x4矩陣表示。設想一下,同一場景中的兩個視圖的區別僅僅是一個純粹的的旋轉,那么可以發現這個矩陣的第4列都是由0組成的,沒有平移向量。因此投影關系變成了3x3矩陣,即單應矩陣。

也意味着,在特殊情況下,同一個點在不同的視圖中存在線性關系。

測試1:圖像的拼接

 1 #include<iostream>
 2 #include<opencv2/core/core.hpp>
 3 #include<opencv2/highgui/highgui.hpp>
 4 #include<opencv2/features2d/features2d.hpp>
 5 #include<opencv2/calib3d/calib3d.hpp>
 6 #include<opencv2/opencv.hpp>
 7 #include<opencv2/stitching/stitcher.hpp>
 8 using namespace cv;
 9 int main(int argc, char* argv[]){
10 
11 if (argc!=3){
12     std::cout << "usage: feature_extraction imge1 image2" << std::endl;
13     return 1;
14 }
15 //read image
16 Mat image1 =imread(argv[1],CV_LOAD_IMAGE_COLOR);
17 Mat image2 = imread(argv[2],CV_LOAD_IMAGE_COLOR);
18 //keypoint descriptor
19 vector<KeyPoint> keypoint1,keypoint2;
20 Mat descriptor1,descriptor2;
21 ORB orb;
22 orb(image1,cv::Mat(),keypoint1,descriptor1);
23 orb(image2,cv::Mat(),keypoint2,descriptor2);
24 Mat outimage;
25 drawKeypoints(image1,keypoint1,outimage);
26 //match
27 vector<DMatch> matches;
28 BFMatcher matcher(NORM_HAMMING);
29 matcher.match(descriptor1,descriptor2,matches);
30 vector<DMatch> good_matches;
31 double mindist=1000,maxdist=0;
32 for(int i=0;i<matches.size();i++){
33     if(matches[i].distance <= max(2*mindist,30.0))
34    good_matches.push_back(matches[i]);
35 }
36 Mat good_matches_out;
37 drawMatches(image1,keypoint1,image2,keypoint2,good_matches,good_matches_out);
38 namedWindow("優化后匹配點",0);
39 resizeWindow("優化后匹配點",1241,376);
40 imshow("優化后匹配點",good_matches_out);
41 imwrite("優化后匹配點.png",good_matches_out);
42 std::vector<cv::Point2f> points1, points2;
43     for (std::vector<cv::DMatch>::const_iterator it= matches.begin();
44          it!= matches.end(); ++it) {
45 
46              // Get the position of left keypoints
47              float x= keypoint1[it->queryIdx].pt.x;
48              float y= keypoint1[it->queryIdx].pt.y;
49              points1.push_back(cv::Point2f(x,y));
50              // Get the position of right keypoints
51              x= keypoint2[it->trainIdx].pt.x;
52              y= keypoint2[it->trainIdx].pt.y;
53              points2.push_back(cv::Point2f(x,y));
54     }
55    std::cout << points1.size() << " " << points2.size() << std::endl; 
56 Mat fundamental_matrix;
57 fundamental_matrix = findFundamentalMat (points1,points2,CV_FM_8POINT);
58 std::cout <<"Fundamental matrix="<< fundamental_matrix<<std::endl;
59 std::vector<uchar>inliers(points1.size(),0);
60 Mat homography_matrix =findHomography(Mat(points1),Mat(points2),inliers,CV_RANSAC,1.);
61 std::cout << "homography_matrix="<<homography_matrix<<std::endl;
62 Mat result;
63 warpPerspective(image1,result,homography_matrix,Size(1.5*image1.cols,image1.rows));
64 Mat half(result, Rect(0,0,image2.cols,image2.rows));
65 image2.copyTo(half);
66 namedWindow("result",0);
67 resizeWindow("result",640,320);
68 imshow("result",result);
69 imwrite("result.png",result);
70 waitKey(0);
71 return 0;
72 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM