Ubuntu16.04 + opencv2.4.9
一、特征提取与匹配
(以ORB特征为例)
features.cpp
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 using namespace cv; 7 int main(int argc, char* argv[]){ 8 9 if (argc!=3){ 10 std::cout << "usage: feature_extraction imge1 image2" << std::endl; 11 return 1; 12 } 13 //read image 14 Mat image1 =imread(argv[1],CV_LOAD_IMAGE_COLOR); 15 Mat image2 = imread(argv[2],CV_LOAD_IMAGE_COLOR); 16 Mat describtor1, describtor2; 17 vector<KeyPoint> keypoint1,keypoint2; 18 //step2 orb 19 ORB orb; 20 orb(image1,Mat(),keypoint1,describtor1); 21 orb(image2,Mat(),keypoint2,describtor2); 22 Mat outimage1; 23 drawKeypoints(image1,keypoint1,outimage1,Scalar::all(-1),DrawMatchesFlags::DEFAULT); 24 namedWindow("orb特征点",0); 25 cvResizeWindow("orb特征点", 640,480); 26 imshow("orb特征点",outimage1); 27 //step3 match 28 vector<DMatch> matches; 29 BFMatcher matcher(NORM_HAMMING); 30 matcher.match(describtor1,describtor2,matches); 31 //step4 find min dist and max dist of all matches 32 double mindist=1000,maxdist=0; 33 for(int i=0; i<describtor1.rows;i++){ 34 double dist =matches[i].distance; 35 if(dist<mindist) mindist = dist; 36 if(dist>maxdist) maxdist = dist; 37 38 } 39 std::cout <<"Max="<<maxdist <<std::endl; 40 std::cout<<"Min=" <<mindist <<std::endl; 41 //5 42 std::vector<DMatch> good_matches; 43 for(int i=0; i<describtor1.rows; i++){ 44 if(matches[i].distance <= max(2*mindist,30.0)) 45 good_matches.push_back(matches[i]); 46 } 47 //step6 plot results 48 Mat img_match,img_goodmatch; 49 drawMatches(image1,keypoint1,image2,keypoint2,matches,img_match); 50 drawMatches(image1,keypoint1,image2,keypoint2,good_matches,img_goodmatch); 51 namedWindow("所有匹配点",0); 52 namedWindow("优化后匹配点",0); 53 cvResizeWindow("所有匹配点", 1021,376); 54 cvResizeWindow("优化后匹配点", 1021,376); 55 imshow("所有匹配点",img_match); 56 imwrite("img_match.png",img_match); 57 imshow("优化后匹配点",img_goodmatch); 58 imwrite("img_goodmatch.png",img_goodmatch); 59 waitKey(0); 60 return 0; 61 }
结果:
Max=94 Min=21
orb特征点:
所有匹配点:
优化后匹配点:
二、基础矩阵、单应矩阵
(调用opencv函数)
1 vector<Point2f> points1; 2 vector<Point2f> points2; 3 for (int i=0; i<matches.size();i++){ 4 points1.push_back(keypoint1[matches[i].queryIdx].pt); 5 points2.push_back(keypoint2[matches[i].queryIdx].pt); 6 } 7 Mat fundamental_matrix; 8 fundamental_matrix = findFundamentalMat (points1,points2,CV_FM_8POINT); 9 std::cout <<"Fundamental_matrix="<< fundamental_matrix<<std::endl; 10 11 Mat homography_matrix ; 12 homography_matrix= findHomography (points1,points2,RANSAC); 13 std::cout << "homography_matrix="<<homography_matrix <<std::endl;
结果:
Fundamental_matrix=[-4.075224349747993e-09, 2.477107638875577e-06, -0.0003483120020484568; -5.388675340517034e-08, 3.801248867773318e-05, -0.005352597475279493; 2.16130923207796e-05, -0.007172831622169928, 1] Homography_matrix=[-0.2147533899810728, -1.588025524342637, 437.941430304518; -0.07845693563076726, -0.580883857003762, 160.1035190035557; -0.0004891439919827992, -0.003633821403150171, 1]
三、极线约束
1 //极线约束 2 std::vector<Vec<float,3> > epilines1,epilines2; 3 computeCorrespondEpilines(points1,1,fundamental_matrix,epilines1); 4 computeCorrespondEpilines(points2,2,fundamental_matrix,epilines2); 5 RNG rng; 6 for(uint i=0;i<10;i++){ 7 Scalar color = Scalar(rng(256),rng(256),rng(256)); 8 circle(image2,points2[i],6,color,6); 9 line(image2,Point(0,-epilines1[i][2]/epilines1[i][1]), Point(image2.cols,-(epilines1[i][2]+epilines1[i][0]*image2.cols)/epilines1[i][1]),color); 10 circle(image1,points1[i],6,color,6); 11 line(image1,Point(0,-epilines2[i][2]/epilines2[i][1]), Point(image1.cols,-(epilines2[i][2]+epilines2[i][0]*image1.cols)/epilines2[i][1]),color); 12 } 13 namedWindow("epilines1",0); 14 resizeWindow("epilines1",640,480); 15 imshow("epilines1",image1); 16 imwrite("epilines1.png",image1); 17 namedWindow("epilines2",0); 18 resizeWindow("epilines2",640,480); 19 imshow("epilines2",image1); 20 imwrite("epilines2.png",image1); 21 waitKey(0);
结果: