圖像拼接在實際的應用場景很廣,好比無人機航拍,遙感圖像等等,圖像拼接是進一步作圖像理解基礎步驟,拼接效果的好壞直接影響接下來的工做,因此一個好的圖像拼接算法很是重要。html
再舉一個身邊的例子吧,你用你的手機對某一場景拍照,可是你沒有辦法一次將全部你要拍的景物所有拍下來,因此你對該場景從左往右依次拍了好幾張圖,來把你要拍的全部景物記錄下來。那麼咱們能不能把這些圖像拼接成一個大圖呢?咱們利用opencv就能夠作到圖像拼接的效果!ios
好比咱們有對這兩張圖進行拼接。算法
從上面兩張圖能夠看出,這兩張圖有比較多的重疊部分,這也是拼接的基本要求。函數
那麼要實現圖像拼接須要那幾步呢?簡單來講有如下幾步:測試
好吧,那就開始正式實現圖像配準。優化
第一步就是特徵點提取。如今CV領域有不少特徵點的定義,好比sift、surf、harris角點、ORB都是頗有名的特徵因子,均可以用來作圖像拼接的工做,他們各有優點。本文將使用ORB和SURF進行圖像拼接,用其餘方法進行拼接也是相似的。ui
用SIFT算法來實現圖像拼接是很經常使用的方法,可是由於SIFT計算量很大,因此在速度要求很高的場合下再也不適用。因此,它的改進方法SURF由於在速度方面有了明顯的提升(速度是SIFT的3倍),因此在圖像拼接領域仍是大有做爲。雖然說SURF精確度和穩定性不及SIFT,可是其綜合能力仍是優越一些。下面將詳細介紹拼接的主要步驟。spa
特徵點提取和匹配的方法我在上一篇文章《OpenCV探索之路(二十三):特徵檢測和特徵匹配方法彙總》中作了詳細的介紹,在這裏直接使用上文所總結的SURF特徵提取和特徵匹配的方法。3d
//提取特徵點 SurfFeatureDetector Detector(2000); vector<KeyPoint> keyPoint1, keyPoint2; Detector.detect(image1, keyPoint1); Detector.detect(image2, keyPoint2); //特徵點描述,爲下邊的特徵點匹配作準備 SurfDescriptorExtractor Descriptor; Mat imageDesc1, imageDesc2; Descriptor.compute(image1, keyPoint1, imageDesc1); Descriptor.compute(image2, keyPoint2, imageDesc2); FlannBasedMatcher matcher; vector<vector<DMatch> > matchePoints; vector<DMatch> GoodMatchePoints; vector<Mat> train_desc(1, imageDesc1); matcher.add(train_desc); matcher.train(); matcher.knnMatch(imageDesc2, matchePoints, 2); cout << "total match points: " << matchePoints.size() << endl; // Lowe's algorithm,獲取優秀匹配點 for (int i = 0; i < matchePoints.size(); i++) { if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance) { GoodMatchePoints.push_back(matchePoints[i][0]); } } Mat first_match; drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match); imshow("first_match ", first_match);
這樣子咱們就能夠獲得了兩幅待拼接圖的匹配點集,接下來咱們進行圖像的配準,即將兩張圖像轉換爲同一座標下,這裏咱們須要使用findHomography函數來求得變換矩陣。可是須要注意的是,findHomography函數所要用到的點集是Point2f類型的,全部咱們須要對咱們剛獲得的點集GoodMatchePoints再作一次處理,使其轉換爲Point2f類型的點集。code
vector<Point2f> imagePoints1, imagePoints2; for (int i = 0; i<GoodMatchePoints.size(); i++) { imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt); imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt); }
這樣子,咱們就能夠拿着imagePoints1, imagePoints2去求變換矩陣了,而且實現圖像配準。值得注意的是findHomography函數的參數中咱們選澤了CV_RANSAC,這代表咱們選擇RANSAC算法繼續篩選可靠地匹配點,這使得匹配點解更爲精確。
//獲取圖像1到圖像2的投影映射矩陣 尺寸爲3*3 Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC); ////也可使用getPerspectiveTransform方法得到透視變換矩陣,不過要求只能有4個點,效果稍差 //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2); cout << "變換矩陣爲:\n" << homo << endl << endl; //輸出映射矩陣 //圖像配準 Mat imageTransform1, imageTransform2; warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows)); //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8)); imshow("直接通過透視矩陣變換", imageTransform1); imwrite("trans1.jpg", imageTransform1);
拷貝的思路很簡單,就是將左圖直接拷貝到配準圖上就能夠了。
//建立拼接後的圖,需提早計算圖的大小 int dst_width = imageTransform1.cols; //取最右點的長度爲拼接圖的長度 int dst_height = image02.rows; Mat dst(dst_height, dst_width, CV_8UC3); dst.setTo(0); imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows))); image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows))); imshow("b_dst", dst);
從上圖能夠看出,兩圖的拼接並不天然,緣由就在於拼接圖的交界處,兩圖由於光照色澤的緣由使得兩圖交界處的過渡很糟糕,因此須要特定的處理解決這種不天然。這裏的處理思路是加權融合,在重疊部分由前一幅圖像慢慢過渡到第二幅圖像,即將圖像的重疊區域的像素值按必定的權值相加合成新的圖像。
//優化兩圖的鏈接處,使得拼接天然 void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst) { int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區域的左邊界 double processWidth = img1.cols - start;//重疊區域的寬度 int rows = dst.rows; int cols = img1.cols; //注意,是列數*通道數 double alpha = 1;//img1中像素的權重 for (int i = 0; i < rows; i++) { uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址 uchar* t = trans.ptr<uchar>(i); uchar* d = dst.ptr<uchar>(i); for (int j = start; j < cols; j++) { //若是遇到圖像trans中無像素的黑點,則徹底拷貝img1中的數據 if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0) { alpha = 1; } else { //img1中像素的權重,與當前處理點距重疊區域左邊界的距離成正比,實驗證實,這種方法確實好 alpha = (processWidth - (j - start)) / processWidth; } d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha); d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha); d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha); } } }
多嘗試幾張,驗證拼接效果
測試一
測試二
測試三
最後給出完整的SURF算法實現的拼接代碼。
#include "highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/legacy/legacy.hpp" #include <iostream> using namespace cv; using namespace std; void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst); typedef struct { Point2f left_top; Point2f left_bottom; Point2f right_top; Point2f right_bottom; }four_corners_t; four_corners_t corners; void CalcCorners(const Mat& H, const Mat& src) { double v2[] = { 0, 0, 1 };//左上角 double v1[3];//變換後的座標值 Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量 Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; //左上角(0,0,1) cout << "V2: " << V2 << endl; cout << "V1: " << V1 << endl; corners.left_top.x = v1[0] / v1[2]; corners.left_top.y = v1[1] / v1[2]; //左下角(0,src.rows,1) v2[0] = 0; v2[1] = src.rows; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.left_bottom.x = v1[0] / v1[2]; corners.left_bottom.y = v1[1] / v1[2]; //右上角(src.cols,0,1) v2[0] = src.cols; v2[1] = 0; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.right_top.x = v1[0] / v1[2]; corners.right_top.y = v1[1] / v1[2]; //右下角(src.cols,src.rows,1) v2[0] = src.cols; v2[1] = src.rows; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.right_bottom.x = v1[0] / v1[2]; corners.right_bottom.y = v1[1] / v1[2]; } int main(int argc, char *argv[]) { Mat image01 = imread("g5.jpg", 1); //右圖 Mat image02 = imread("g4.jpg", 1); //左圖 imshow("p2", image01); imshow("p1", image02); //灰度圖轉換 Mat image1, image2; cvtColor(image01, image1, CV_RGB2GRAY); cvtColor(image02, image2, CV_RGB2GRAY); //提取特徵點 SurfFeatureDetector Detector(2000); vector<KeyPoint> keyPoint1, keyPoint2; Detector.detect(image1, keyPoint1); Detector.detect(image2, keyPoint2); //特徵點描述,爲下邊的特徵點匹配作準備 SurfDescriptorExtractor Descriptor; Mat imageDesc1, imageDesc2; Descriptor.compute(image1, keyPoint1, imageDesc1); Descriptor.compute(image2, keyPoint2, imageDesc2); FlannBasedMatcher matcher; vector<vector<DMatch> > matchePoints; vector<DMatch> GoodMatchePoints; vector<Mat> train_desc(1, imageDesc1); matcher.add(train_desc); matcher.train(); matcher.knnMatch(imageDesc2, matchePoints, 2); cout << "total match points: " << matchePoints.size() << endl; // Lowe's algorithm,獲取優秀匹配點 for (int i = 0; i < matchePoints.size(); i++) { if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance) { GoodMatchePoints.push_back(matchePoints[i][0]); } } Mat first_match; drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match); imshow("first_match ", first_match); vector<Point2f> imagePoints1, imagePoints2; for (int i = 0; i<GoodMatchePoints.size(); i++) { imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt); imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt); } //獲取圖像1到圖像2的投影映射矩陣 尺寸爲3*3 Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC); ////也可使用getPerspectiveTransform方法得到透視變換矩陣,不過要求只能有4個點,效果稍差 //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2); cout << "變換矩陣爲:\n" << homo << endl << endl; //輸出映射矩陣 //計算配準圖的四個頂點座標 CalcCorners(homo, image01); cout << "left_top:" << corners.left_top << endl; cout << "left_bottom:" << corners.left_bottom << endl; cout << "right_top:" << corners.right_top << endl; cout << "right_bottom:" << corners.right_bottom << endl; //圖像配準 Mat imageTransform1, imageTransform2; warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows)); //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8)); imshow("直接通過透視矩陣變換", imageTransform1); imwrite("trans1.jpg", imageTransform1); //建立拼接後的圖,需提早計算圖的大小 int dst_width = imageTransform1.cols; //取最右點的長度爲拼接圖的長度 int dst_height = image02.rows; Mat dst(dst_height, dst_width, CV_8UC3); dst.setTo(0); imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows))); image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows))); imshow("b_dst", dst); OptimizeSeam(image02, imageTransform1, dst); imshow("dst", dst); imwrite("dst.jpg", dst); waitKey(); return 0; } //優化兩圖的鏈接處,使得拼接天然 void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst) { int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區域的左邊界 double processWidth = img1.cols - start;//重疊區域的寬度 int rows = dst.rows; int cols = img1.cols; //注意,是列數*通道數 double alpha = 1;//img1中像素的權重 for (int i = 0; i < rows; i++) { uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址 uchar* t = trans.ptr<uchar>(i); uchar* d = dst.ptr<uchar>(i); for (int j = start; j < cols; j++) { //若是遇到圖像trans中無像素的黑點,則徹底拷貝img1中的數據 if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0) { alpha = 1; } else { //img1中像素的權重,與當前處理點距重疊區域左邊界的距離成正比,實驗證實,這種方法確實好 alpha = (processWidth - (j - start)) / processWidth; } d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha); d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha); d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha); } } }
利用ORB進行圖像拼接的思路跟上面的思路基本同樣,只是特徵提取和特徵點匹配的方式略有差別罷了。這裏就再也不詳細介紹思路了,直接貼代碼看效果。
#include "highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/legacy/legacy.hpp" #include <iostream> using namespace cv; using namespace std; void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst); typedef struct { Point2f left_top; Point2f left_bottom; Point2f right_top; Point2f right_bottom; }four_corners_t; four_corners_t corners; void CalcCorners(const Mat& H, const Mat& src) { double v2[] = { 0, 0, 1 };//左上角 double v1[3];//變換後的座標值 Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量 Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; //左上角(0,0,1) cout << "V2: " << V2 << endl; cout << "V1: " << V1 << endl; corners.left_top.x = v1[0] / v1[2]; corners.left_top.y = v1[1] / v1[2]; //左下角(0,src.rows,1) v2[0] = 0; v2[1] = src.rows; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.left_bottom.x = v1[0] / v1[2]; corners.left_bottom.y = v1[1] / v1[2]; //右上角(src.cols,0,1) v2[0] = src.cols; v2[1] = 0; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.right_top.x = v1[0] / v1[2]; corners.right_top.y = v1[1] / v1[2]; //右下角(src.cols,src.rows,1) v2[0] = src.cols; v2[1] = src.rows; v2[2] = 1; V2 = Mat(3, 1, CV_64FC1, v2); //列向量 V1 = Mat(3, 1, CV_64FC1, v1); //列向量 V1 = H * V2; corners.right_bottom.x = v1[0] / v1[2]; corners.right_bottom.y = v1[1] / v1[2]; } int main(int argc, char *argv[]) { Mat image01 = imread("t1.jpg", 1); //右圖 Mat image02 = imread("t2.jpg", 1); //左圖 imshow("p2", image01); imshow("p1", image02); //灰度圖轉換 Mat image1, image2; cvtColor(image01, image1, CV_RGB2GRAY); cvtColor(image02, image2, CV_RGB2GRAY); //提取特徵點 OrbFeatureDetector surfDetector(3000); vector<KeyPoint> keyPoint1, keyPoint2; surfDetector.detect(image1, keyPoint1); surfDetector.detect(image2, keyPoint2); //特徵點描述,爲下邊的特徵點匹配作準備 OrbDescriptorExtractor SurfDescriptor; Mat imageDesc1, imageDesc2; SurfDescriptor.compute(image1, keyPoint1, imageDesc1); SurfDescriptor.compute(image2, keyPoint2, imageDesc2); flann::Index flannIndex(imageDesc1, flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING); vector<DMatch> GoodMatchePoints; Mat macthIndex(imageDesc2.rows, 2, CV_32SC1), matchDistance(imageDesc2.rows, 2, CV_32FC1); flannIndex.knnSearch(imageDesc2, macthIndex, matchDistance, 2, flann::SearchParams()); // Lowe's algorithm,獲取優秀匹配點 for (int i = 0; i < matchDistance.rows; i++) { if (matchDistance.at<float>(i, 0) < 0.4 * matchDistance.at<float>(i, 1)) { DMatch dmatches(i, macthIndex.at<int>(i, 0), matchDistance.at<float>(i, 0)); GoodMatchePoints.push_back(dmatches); } } Mat first_match; drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match); imshow("first_match ", first_match); vector<Point2f> imagePoints1, imagePoints2; for (int i = 0; i<GoodMatchePoints.size(); i++) { imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt); imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt); } //獲取圖像1到圖像2的投影映射矩陣 尺寸爲3*3 Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC); ////也可使用getPerspectiveTransform方法得到透視變換矩陣,不過要求只能有4個點,效果稍差 //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2); cout << "變換矩陣爲:\n" << homo << endl << endl; //輸出映射矩陣 //計算配準圖的四個頂點座標 CalcCorners(homo, image01); cout << "left_top:" << corners.left_top << endl; cout << "left_bottom:" << corners.left_bottom << endl; cout << "right_top:" << corners.right_top << endl; cout << "right_bottom:" << corners.right_bottom << endl; //圖像配準 Mat imageTransform1, imageTransform2; warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows)); //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8)); imshow("直接通過透視矩陣變換", imageTransform1); imwrite("trans1.jpg", imageTransform1); //建立拼接後的圖,需提早計算圖的大小 int dst_width = imageTransform1.cols; //取最右點的長度爲拼接圖的長度 int dst_height = image02.rows; Mat dst(dst_height, dst_width, CV_8UC3); dst.setTo(0); imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows))); image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows))); imshow("b_dst", dst); OptimizeSeam(image02, imageTransform1, dst); imshow("dst", dst); imwrite("dst.jpg", dst); waitKey(); return 0; } //優化兩圖的鏈接處,使得拼接天然 void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst) { int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區域的左邊界 double processWidth = img1.cols - start;//重疊區域的寬度 int rows = dst.rows; int cols = img1.cols; //注意,是列數*通道數 double alpha = 1;//img1中像素的權重 for (int i = 0; i < rows; i++) { uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址 uchar* t = trans.ptr<uchar>(i); uchar* d = dst.ptr<uchar>(i); for (int j = start; j < cols; j++) { //若是遇到圖像trans中無像素的黑點,則徹底拷貝img1中的數據 if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0) { alpha = 1; } else { //img1中像素的權重,與當前處理點距重疊區域左邊界的距離成正比,實驗證實,這種方法確實好 alpha = (processWidth - (j - start)) / processWidth; } d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha); d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha); d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha); } } }
看一看拼接效果,我以爲仍是不錯的。
看一下這一組圖片,這組圖片產生了鬼影,爲何?由於兩幅圖中的人物走動了啊!因此要作圖像拼接,儘可能保證使用的是靜態圖片,不要加入一些動態因素干擾拼接。
opencv其實本身就有實現圖像拼接的算法,固然效果也是至關好的,可是由於其實現很複雜,並且代碼量很龐大,其實在一些小應用下的拼接有點殺雞用牛刀的感受。最近在閱讀sticth源碼時,發現其中有幾個頗有意思的地方。
一直很好奇opencv stitch算法到底選用了哪一個算法做爲其特徵檢測方式,是ORB,SIFT仍是SURF?讀源碼終於看到答案。
#ifdef HAVE_OPENCV_NONFREE stitcher.setFeaturesFinder(new detail::SurfFeaturesFinder()); #else stitcher.setFeaturesFinder(new detail::OrbFeaturesFinder()); #endif
在源碼createDefault函數中(默認設置),第一選擇是SURF,第二選擇纔是ORB(沒有NONFREE模塊才選),因此既然大牛們這麼選擇,必然是通過綜合考慮的,因此應該SURF算法在圖像拼接有着更優秀的效果。
如下代碼是opencv stitch源碼中的特徵點提取部分,做者使用了兩次特徵點提取的思路:先對圖一進行特徵點提取和篩選匹配(1->2),再對圖二進行特徵點的提取和匹配(2->1),這跟咱們平時的一次提取的思路不一樣,這種二次提取的思路能夠保證更多的匹配點被選中,匹配點越多,findHomography求出的變換越準確。這個思路值得借鑑。
matches_info.matches.clear(); Ptr<flann::IndexParams> indexParams = new flann::KDTreeIndexParams(); Ptr<flann::SearchParams> searchParams = new flann::SearchParams(); if (features2.descriptors.depth() == CV_8U) { indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH); searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH); } FlannBasedMatcher matcher(indexParams, searchParams); vector< vector<DMatch> > pair_matches; MatchesSet matches; // Find 1->2 matches matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2); for (size_t i = 0; i < pair_matches.size(); ++i) { if (pair_matches[i].size() < 2) continue; const DMatch& m0 = pair_matches[i][0]; const DMatch& m1 = pair_matches[i][1]; if (m0.distance < (1.f - match_conf_) * m1.distance) { matches_info.matches.push_back(m0); matches.insert(make_pair(m0.queryIdx, m0.trainIdx)); } } LOG("\n1->2 matches: " << matches_info.matches.size() << endl); // Find 2->1 matches pair_matches.clear(); matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2); for (size_t i = 0; i < pair_matches.size(); ++i) { if (pair_matches[i].size() < 2) continue; const DMatch& m0 = pair_matches[i][0]; const DMatch& m1 = pair_matches[i][1]; if (m0.distance < (1.f - match_conf_) * m1.distance) if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end()) matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance)); } LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);
這裏我仿照opencv源碼二次提取特徵點的思路對我原有拼接代碼進行改寫,實驗證實獲取的匹配點確實較一次提取要多。
//提取特徵點 SiftFeatureDetector Detector(1000); // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 vector<KeyPoint> keyPoint1, keyPoint2; Detector.detect(image1, keyPoint1); Detector.detect(image2, keyPoint2); //特徵點描述,爲下邊的特徵點匹配作準備 SiftDescriptorExtractor Descriptor; Mat imageDesc1, imageDesc2; Descriptor.compute(image1, keyPoint1, imageDesc1); Descriptor.compute(image2, keyPoint2, imageDesc2); FlannBasedMatcher matcher; vector<vector<DMatch> > matchePoints; vector<DMatch> GoodMatchePoints; MatchesSet matches; vector<Mat> train_desc(1, imageDesc1); matcher.add(train_desc); matcher.train(); matcher.knnMatch(imageDesc2, matchePoints, 2); // Lowe's algorithm,獲取優秀匹配點 for (int i = 0; i < matchePoints.size(); i++) { if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance) { GoodMatchePoints.push_back(matchePoints[i][0]); matches.insert(make_pair(matchePoints[i][0].queryIdx, matchePoints[i][0].trainIdx)); } } cout<<"\n1->2 matches: " << GoodMatchePoints.size() << endl; #if 1 FlannBasedMatcher matcher2; matchePoints.clear(); vector<Mat> train_desc2(1, imageDesc2); matcher2.add(train_desc2); matcher2.train(); matcher2.knnMatch(imageDesc1, matchePoints, 2); // Lowe's algorithm,獲取優秀匹配點 for (int i = 0; i < matchePoints.size(); i++) { if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance) { if (matches.find(make_pair(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx)) == matches.end()) { GoodMatchePoints.push_back(DMatch(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx, matchePoints[i][0].distance)); } } } cout<<"1->2 & 2->1 matches: " << GoodMatchePoints.size() << endl; #endif
最後再看一下opencv stitch的拼接效果吧~速度雖然比較慢,可是效果仍是很好的。
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/stitching/stitcher.hpp> using namespace std; using namespace cv; bool try_use_gpu = false; vector<Mat> imgs; string result_name = "dst1.jpg"; int main(int argc, char * argv[]) { Mat img1 = imread("34.jpg"); Mat img2 = imread("35.jpg"); imshow("p1", img1); imshow("p2", img2); if (img1.empty() || img2.empty()) { cout << "Can't read image" << endl; return -1; } imgs.push_back(img1); imgs.push_back(img2); Stitcher stitcher = Stitcher::createDefault(try_use_gpu); // 使用stitch函數進行拼接 Mat pano; Stitcher::Status status = stitcher.stitch(imgs, pano); if (status != Stitcher::OK) { cout << "Can't stitch images, error code = " << int(status) << endl; return -1; } imwrite(result_name, pano); Mat pano2 = pano.clone(); // 顯示源圖像,和結果圖像 imshow("全景圖像", pano); if (waitKey() == 27) return 0; }