opencv學習 第十章 估算影象之間的投影關係
阿新 • • 發佈:2018-12-17
opencv學習 第十章 估算影象之間的投影關係
10.2 計算影象對的基礎矩陣
#include"stdafx.h" #include<iostream> #include<opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/features2d/features2d.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/xfeatures2d.hpp> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat image1 =imread("故宮5.jpg"); Mat image2 =imread("故宮7.jpg"); resize(image1, image1, Size(360, 480)); resize(image2, image2, Size(360, 480)); if (!image1.data || !image2.data) return 0; //特徵點的向量 vector<KeyPoint> keypoints1, keypoints2; //構造SURF特徵檢測器 Ptr<xfeatures2d::SurfFeatureDetector> ptrSURF = xfeatures2d::SurfFeatureDetector::create(2000.0);//閾值 //檢測SURF特徵 ptrSURF->detect(image1, keypoints1); ptrSURF->detect(image2, keypoints2); //提取SURF描述子 Mat descriptors1, descriptors2; ptrSURF->compute(image1, keypoints1, descriptors1); ptrSURF->compute(image2, keypoints2, descriptors2); //構造匹配器 BFMatcher matcher(NORM_L2); //匹配兩幅影象的描述子 vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); nth_element(matches.begin(), //初始位置 matches.begin() +7, //排序元素的位置 matches.end()); //終止位置 //移除第7位之後所有的元素 matches.erase(matches.begin() + 7, matches.end()); Mat imageMatches; drawMatches( image1, keypoints1, //第一幅影象及其特徵點 image2, keypoints2, //第二幅影象及其特徵點 matches, //匹配結果 imageMatches, //生成的影象 Scalar(255, 255, 255),//直線的顏色 Scalar(0,255,0)); //點的顏色 imshow("match", imageMatches); //轉換KeyPoint型別到Point2f vector<Point2f>selPoints1, selPoints2; vector<int>pointIndexes1, pointIndexes2; /* for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) { // Get the indexes of the selected matched keypoints pointIndexes1.push_back(it->queryIdx); pointIndexes2.push_back(it->trainIdx); } */ KeyPoint::convert(keypoints1, selPoints1, pointIndexes1); KeyPoint::convert(keypoints2, selPoints2, pointIndexes2); //從7個矩陣中計算F矩陣 Mat fundemental =findFundamentalMat( Mat(selPoints1), //圖1中的點 Mat(selPoints2), //圖2中的點 CV_FM_7POINT); //使用7個點的方法 //計算左圖中點的極線 繪製在右圖中 在右圖中繪製對應的極線 vector<Vec3f> lines1; computeCorrespondEpilines( Mat(selPoints1), //影象點 1, //圖1(也可以是2) fundemental, //F矩陣 lines1); //一組極線 //對於所有極線 for (vector<Vec3f>::const_iterator it = lines1.begin(); it != lines1.end(); ++it) { //繪製第一列與最後一列之間的直線 line(image2, Point(0, -(*it)[2] / (*it)[1]), Point(image2.cols, -((*it)[2] + (*it)[0] * image2.cols) / (*it)[1]), Scalar(255, 255, 255)); } namedWindow("Left Image Epilines"); //imshow("right", image1); imshow("Left Image Epilines", image2); waitKey(0); return 0; }
執行結果: