<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          圖像拼接和圖像融合技術

          共 20204字,需瀏覽 41分鐘

           ·

          2021-08-09 14:44

          點擊下方卡片,關注“新機器視覺”公眾號

          視覺/圖像重磅干貨,第一時間送達

          來源:https://www.cnblogs.com/skyfsm/p/7411961.html

           圖像拼接在實際的應用場景很廣,比如無人機航拍,遙感圖像等等,圖像拼接是進一步做圖像理解基礎步驟,拼接效果的好壞直接影響接下來的工作,所以一個好的圖像拼接算法非常重要。

          再舉一個身邊的例子吧,你用你的手機對某一場景拍照,但是你沒有辦法一次將所有你要拍的景物全部拍下來,所以你對該場景從左往右依次拍了好幾張圖,來把你要拍的所有景物記錄下來。那么我們能不能把這些圖像拼接成一個大圖呢?我們利用opencv就可以做到圖像拼接的效果!

          比如我們有對這兩張圖進行拼接。

          從上面兩張圖可以看出,這兩張圖有比較多的重疊部分,這也是拼接的基本要求。

          那么要實現圖像拼接需要那幾步呢?簡單來說有以下幾步:

          1. 對每幅圖進行特征點提取

          2. 對對特征點進行匹配

          3. 進行圖像配準

          4. 把圖像拷貝到另一幅圖像的特定位置

          5. 對重疊邊界進行特殊處理

          好吧,那就開始正式實現圖像配準。

          第一步就是特征點提取。現在CV領域有很多特征點的定義,比如sift、surf、harris角點、ORB都是很有名的特征因子,都可以用來做圖像拼接的工作,他們各有優(yōu)勢。本文將使用ORB和SURF進行圖像拼接,用其他方法進行拼接也是類似的。

           

          基于SURF的圖像拼接

          用SIFT算法來實現圖像拼接是很常用的方法,但是因為SIFT計算量很大,所以在速度要求很高的場合下不再適用。所以,它的改進方法SURF因為在速度方面有了明顯的提高(速度是SIFT的3倍),所以在圖像拼接領域還是大有作為。雖說SURF精確度和穩(wěn)定性不及SIFT,但是其綜合能力還是優(yōu)越一些。下面將詳細介紹拼接的主要步驟。

          1.特征點提取和匹配

          特征點提取和匹配的方法我在上一篇文章《OpenCV探索之路(二十三):特征檢測和特征匹配方法匯總》中做了詳細的介紹,在這里直接使用上文所總結的SURF特征提取和特征匹配的方法。

          1. //提取特征點

          2. SurfFeatureDetector Detector(2000);

          3. vector<KeyPoint> keyPoint1, keyPoint2;

          4. Detector.detect(image1, keyPoint1);

          5. Detector.detect(image2, keyPoint2);

          6. //特征點描述,為下邊的特征點匹配做準備

          7. SurfDescriptorExtractor Descriptor;

          8. Mat imageDesc1, imageDesc2;

          9. Descriptor.compute(image1, keyPoint1, imageDesc1);

          10. Descriptor.compute(image2, keyPoint2, imageDesc2);

          11. FlannBasedMatcher matcher;

          12. vector<vector<DMatch> > matchePoints;

          13. vector<DMatch> GoodMatchePoints;

          14. vector<Mat> train_desc(1, imageDesc1);

          15. matcher.add(train_desc);

          16. matcher.train();

          17. matcher.knnMatch(imageDesc2, matchePoints, 2);

          18. cout << "total match points: " << matchePoints.size() << endl;

          19. // Lowe's algorithm,獲取優(yōu)秀匹配點

          20. for (int i = 0; i < matchePoints.size(); i++)

          21. {

          22. if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)

          23. {

          24. GoodMatchePoints.push_back(matchePoints[i][0]);

          25. }

          26. }

          27. Mat first_match;

          28. drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);

          29. imshow("first_match ", first_match);

           

          2.圖像配準

          這樣子我們就可以得到了兩幅待拼接圖的匹配點集,接下來我們進行圖像的配準,即將兩張圖像轉換為同一坐標下,這里我們需要使用findHomography函數來求得變換矩陣。但是需要注意的是,findHomography函數所要用到的點集是Point2f類型的,所有我們需要對我們剛得到的點集GoodMatchePoints再做一次處理,使其轉換為Point2f類型的點集。

          1. vector<Point2f> imagePoints1, imagePoints2;

          2. for (int i = 0; i<GoodMatchePoints.size(); i++)

          3. {

          4. imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);

          5. imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);

          6. }

          這樣子,我們就可以拿著imagePoints1, imagePoints2去求變換矩陣了,并且實現圖像配準。值得注意的是findHomography函數的參數中我們選澤了CV_RANSAC,這表明我們選擇RANSAC算法繼續(xù)篩選可靠地匹配點,這使得匹配點解更為精確。

          1. //獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3

          2. Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);

          3. 也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過要求只能有4個點,效果稍差

          4. //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2);

          5. cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣

          6. //圖像配準

          7. Mat imageTransform1, imageTransform2;

          8. warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));

          9. //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));

          10. imshow("直接經過透視矩陣變換", imageTransform1);

          11. imwrite("trans1.jpg", imageTransform1);

           

          3. 圖像拷貝

          拷貝的思路很簡單,就是將左圖直接拷貝到配準圖上就可以了。

          1. //創(chuàng)建拼接后的圖,需提前計算圖的大小

          2. int dst_width = imageTransform1.cols; //取最右點的長度為拼接圖的長度

          3. int dst_height = image02.rows;

          4. Mat dst(dst_height, dst_width, CV_8UC3);

          5. dst.setTo(0);

          6. imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));

          7. image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

          8. imshow("b_dst", dst);

           

          4.圖像融合(去裂縫處理)

          從上圖可以看出,兩圖的拼接并不自然,原因就在于拼接圖的交界處,兩圖因為光照色澤的原因使得兩圖交界處的過渡很糟糕,所以需要特定的處理解決這種不自然。這里的處理思路是加權融合,在重疊部分由前一幅圖像慢慢過渡到第二幅圖像,即將圖像的重疊區(qū)域的像素值按一定的權值相加合成新的圖像。

          1. //優(yōu)化兩圖的連接處,使得拼接自然

          2. void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)

          3. {

          4. int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區(qū)域的左邊界

          5. double processWidth = img1.cols - start;//重疊區(qū)域的寬度

          6. int rows = dst.rows;

          7. int cols = img1.cols; //注意,是列數*通道數

          8. double alpha = 1;//img1中像素的權重

          9. for (int i = 0; i < rows; i++)

          10. {

          11. uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址

          12. uchar* t = trans.ptr<uchar>(i);

          13. uchar* d = dst.ptr<uchar>(i);

          14. for (int j = start; j < cols; j++)

          15. {

          16. //如果遇到圖像trans中無像素的黑點,則完全拷貝img1中的數據

          17. if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0)

          18. {

          19. alpha = 1;

          20. }

          21. else

          22. {

          23. //img1中像素的權重,與當前處理點距重疊區(qū)域左邊界的距離成正比,實驗證明,這種方法確實好

          24. alpha = (processWidth - (j - start)) / processWidth;

          25. }

          26. d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha);

          27. d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha);

          28. d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha);

          29. }

          30. }

          31. }

          多嘗試幾張,驗證拼接效果

          測試一

          測試二

          測試三

          最后給出完整的SURF算法實現的拼接代碼。

          1. #include "highgui/highgui.hpp"

          2. #include "opencv2/nonfree/nonfree.hpp"

          3. #include "opencv2/legacy/legacy.hpp"

          4. #include <iostream>

          5. using namespace cv;

          6. using namespace std;

          7. void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst);

          8. typedef struct

          9. {

          10. Point2f left_top;

          11. Point2f left_bottom;

          12. Point2f right_top;

          13. Point2f right_bottom;

          14. }four_corners_t;

          15. four_corners_t corners;

          16. void CalcCorners(const Mat& H, const Mat& src)

          17. {

          18. double v2[] = { 0, 0, 1 };//左上角

          19. double v1[3];//變換后的坐標值

          20. Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          21. Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          22. V1 = H * V2;

          23. //左上角(0,0,1)

          24. cout << "V2: " << V2 << endl;

          25. cout << "V1: " << V1 << endl;

          26. corners.left_top.x = v1[0] / v1[2];

          27. corners.left_top.y = v1[1] / v1[2];

          28. //左下角(0,src.rows,1)

          29. v2[0] = 0;

          30. v2[1] = src.rows;

          31. v2[2] = 1;

          32. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          33. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          34. V1 = H * V2;

          35. corners.left_bottom.x = v1[0] / v1[2];

          36. corners.left_bottom.y = v1[1] / v1[2];

          37. //右上角(src.cols,0,1)

          38. v2[0] = src.cols;

          39. v2[1] = 0;

          40. v2[2] = 1;

          41. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          42. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          43. V1 = H * V2;

          44. corners.right_top.x = v1[0] / v1[2];

          45. corners.right_top.y = v1[1] / v1[2];

          46. //右下角(src.cols,src.rows,1)

          47. v2[0] = src.cols;

          48. v2[1] = src.rows;

          49. v2[2] = 1;

          50. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          51. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          52. V1 = H * V2;

          53. corners.right_bottom.x = v1[0] / v1[2];

          54. corners.right_bottom.y = v1[1] / v1[2];

          55. }

          56. int main(int argc, char *argv[])

          57. {

          58. Mat image01 = imread("g5.jpg", 1); //右圖

          59. Mat image02 = imread("g4.jpg", 1); //左圖

          60. imshow("p2", image01);

          61. imshow("p1", image02);

          62. //灰度圖轉換

          63. Mat image1, image2;

          64. cvtColor(image01, image1, CV_RGB2GRAY);

          65. cvtColor(image02, image2, CV_RGB2GRAY);

          66. //提取特征點

          67. SurfFeatureDetector Detector(2000);

          68. vector<KeyPoint> keyPoint1, keyPoint2;

          69. Detector.detect(image1, keyPoint1);

          70. Detector.detect(image2, keyPoint2);

          71. //特征點描述,為下邊的特征點匹配做準備

          72. SurfDescriptorExtractor Descriptor;

          73. Mat imageDesc1, imageDesc2;

          74. Descriptor.compute(image1, keyPoint1, imageDesc1);

          75. Descriptor.compute(image2, keyPoint2, imageDesc2);

          76. FlannBasedMatcher matcher;

          77. vector<vector<DMatch> > matchePoints;

          78. vector<DMatch> GoodMatchePoints;

          79. vector<Mat> train_desc(1, imageDesc1);

          80. matcher.add(train_desc);

          81. matcher.train();

          82. matcher.knnMatch(imageDesc2, matchePoints, 2);

          83. cout << "total match points: " << matchePoints.size() << endl;

          84. // Lowe's algorithm,獲取優(yōu)秀匹配點

          85. for (int i = 0; i < matchePoints.size(); i++)

          86. {

          87. if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)

          88. {

          89. GoodMatchePoints.push_back(matchePoints[i][0]);

          90. }

          91. }

          92. Mat first_match;

          93. drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);

          94. imshow("first_match ", first_match);

          95. vector<Point2f> imagePoints1, imagePoints2;

          96. for (int i = 0; i<GoodMatchePoints.size(); i++)

          97. {

          98. imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);

          99. imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);

          100. }

          101. //獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3

          102. Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);

          103. 也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過要求只能有4個點,效果稍差

          104. //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2);

          105. cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣

          106. //計算配準圖的四個頂點坐標

          107. CalcCorners(homo, image01);

          108. cout << "left_top:" << corners.left_top << endl;

          109. cout << "left_bottom:" << corners.left_bottom << endl;

          110. cout << "right_top:" << corners.right_top << endl;

          111. cout << "right_bottom:" << corners.right_bottom << endl;

          112. //圖像配準

          113. Mat imageTransform1, imageTransform2;

          114. warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));

          115. //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));

          116. imshow("直接經過透視矩陣變換", imageTransform1);

          117. imwrite("trans1.jpg", imageTransform1);

          118. //創(chuàng)建拼接后的圖,需提前計算圖的大小

          119. int dst_width = imageTransform1.cols; //取最右點的長度為拼接圖的長度

          120. int dst_height = image02.rows;

          121. Mat dst(dst_height, dst_width, CV_8UC3);

          122. dst.setTo(0);

          123. imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));

          124. image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

          125. imshow("b_dst", dst);

          126. OptimizeSeam(image02, imageTransform1, dst);

          127. imshow("dst", dst);

          128. imwrite("dst.jpg", dst);

          129. waitKey();

          130. return 0;

          131. }

          132. //優(yōu)化兩圖的連接處,使得拼接自然

          133. void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)

          134. {

          135. int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區(qū)域的左邊界

          136. double processWidth = img1.cols - start;//重疊區(qū)域的寬度

          137. int rows = dst.rows;

          138. int cols = img1.cols; //注意,是列數*通道數

          139. double alpha = 1;//img1中像素的權重

          140. for (int i = 0; i < rows; i++)

          141. {

          142. uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址

          143. uchar* t = trans.ptr<uchar>(i);

          144. uchar* d = dst.ptr<uchar>(i);

          145. for (int j = start; j < cols; j++)

          146. {

          147. //如果遇到圖像trans中無像素的黑點,則完全拷貝img1中的數據

          148. if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0)

          149. {

          150. alpha = 1;

          151. }

          152. else

          153. {

          154. //img1中像素的權重,與當前處理點距重疊區(qū)域左邊界的距離成正比,實驗證明,這種方法確實好

          155. alpha = (processWidth - (j - start)) / processWidth;

          156. }

          157. d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha);

          158. d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha);

          159. d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha);

          160. }

          161. }

          162. }

           

          基于ORB的圖像拼接

          利用ORB進行圖像拼接的思路跟上面的思路基本一樣,只是特征提取和特征點匹配的方式略有差異罷了。這里就不再詳細介紹思路了,直接貼代碼看效果。

          1. #include "highgui/highgui.hpp"

          2. #include "opencv2/nonfree/nonfree.hpp"

          3. #include "opencv2/legacy/legacy.hpp"

          4. #include <iostream>

          5. using namespace cv;

          6. using namespace std;

          7. void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst);

          8. typedef struct

          9. {

          10. Point2f left_top;

          11. Point2f left_bottom;

          12. Point2f right_top;

          13. Point2f right_bottom;

          14. }four_corners_t;

          15. four_corners_t corners;

          16. void CalcCorners(const Mat& H, const Mat& src)

          17. {

          18. double v2[] = { 0, 0, 1 };//左上角

          19. double v1[3];//變換后的坐標值

          20. Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          21. Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          22. V1 = H * V2;

          23. //左上角(0,0,1)

          24. cout << "V2: " << V2 << endl;

          25. cout << "V1: " << V1 << endl;

          26. corners.left_top.x = v1[0] / v1[2];

          27. corners.left_top.y = v1[1] / v1[2];

          28. //左下角(0,src.rows,1)

          29. v2[0] = 0;

          30. v2[1] = src.rows;

          31. v2[2] = 1;

          32. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          33. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          34. V1 = H * V2;

          35. corners.left_bottom.x = v1[0] / v1[2];

          36. corners.left_bottom.y = v1[1] / v1[2];

          37. //右上角(src.cols,0,1)

          38. v2[0] = src.cols;

          39. v2[1] = 0;

          40. v2[2] = 1;

          41. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          42. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          43. V1 = H * V2;

          44. corners.right_top.x = v1[0] / v1[2];

          45. corners.right_top.y = v1[1] / v1[2];

          46. //右下角(src.cols,src.rows,1)

          47. v2[0] = src.cols;

          48. v2[1] = src.rows;

          49. v2[2] = 1;

          50. V2 = Mat(3, 1, CV_64FC1, v2); //列向量

          51. V1 = Mat(3, 1, CV_64FC1, v1); //列向量

          52. V1 = H * V2;

          53. corners.right_bottom.x = v1[0] / v1[2];

          54. corners.right_bottom.y = v1[1] / v1[2];

          55. }

          56. int main(int argc, char *argv[])

          57. {

          58. Mat image01 = imread("t1.jpg", 1); //右圖

          59. Mat image02 = imread("t2.jpg", 1); //左圖

          60. imshow("p2", image01);

          61. imshow("p1", image02);

          62. //灰度圖轉換

          63. Mat image1, image2;

          64. cvtColor(image01, image1, CV_RGB2GRAY);

          65. cvtColor(image02, image2, CV_RGB2GRAY);

          66. //提取特征點

          67. OrbFeatureDetector surfDetector(3000);

          68. vector<KeyPoint> keyPoint1, keyPoint2;

          69. surfDetector.detect(image1, keyPoint1);

          70. surfDetector.detect(image2, keyPoint2);

          71. //特征點描述,為下邊的特征點匹配做準備

          72. OrbDescriptorExtractor SurfDescriptor;

          73. Mat imageDesc1, imageDesc2;

          74. SurfDescriptor.compute(image1, keyPoint1, imageDesc1);

          75. SurfDescriptor.compute(image2, keyPoint2, imageDesc2);

          76. flann::Index flannIndex(imageDesc1, flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING);

          77. vector<DMatch> GoodMatchePoints;

          78. Mat macthIndex(imageDesc2.rows, 2, CV_32SC1), matchDistance(imageDesc2.rows, 2, CV_32FC1);

          79. flannIndex.knnSearch(imageDesc2, macthIndex, matchDistance, 2, flann::SearchParams());

          80. // Lowe's algorithm,獲取優(yōu)秀匹配點

          81. for (int i = 0; i < matchDistance.rows; i++)

          82. {

          83. if (matchDistance.at<float>(i, 0) < 0.4 * matchDistance.at<float>(i, 1))

          84. {

          85. DMatch dmatches(i, macthIndex.at<int>(i, 0), matchDistance.at<float>(i, 0));

          86. GoodMatchePoints.push_back(dmatches);

          87. }

          88. }

          89. Mat first_match;

          90. drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);

          91. imshow("first_match ", first_match);

          92. vector<Point2f> imagePoints1, imagePoints2;

          93. for (int i = 0; i<GoodMatchePoints.size(); i++)

          94. {

          95. imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);

          96. imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);

          97. }

          98. //獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3

          99. Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);

          100. 也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過要求只能有4個點,效果稍差

          101. //Mat homo=getPerspectiveTransform(imagePoints1,imagePoints2);

          102. cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣

          103. //計算配準圖的四個頂點坐標

          104. CalcCorners(homo, image01);

          105. cout << "left_top:" << corners.left_top << endl;

          106. cout << "left_bottom:" << corners.left_bottom << endl;

          107. cout << "right_top:" << corners.right_top << endl;

          108. cout << "right_bottom:" << corners.right_bottom << endl;

          109. //圖像配準

          110. Mat imageTransform1, imageTransform2;

          111. warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));

          112. //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));

          113. imshow("直接經過透視矩陣變換", imageTransform1);

          114. imwrite("trans1.jpg", imageTransform1);

          115. //創(chuàng)建拼接后的圖,需提前計算圖的大小

          116. int dst_width = imageTransform1.cols; //取最右點的長度為拼接圖的長度

          117. int dst_height = image02.rows;

          118. Mat dst(dst_height, dst_width, CV_8UC3);

          119. dst.setTo(0);

          120. imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));

          121. image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

          122. imshow("b_dst", dst);

          123. OptimizeSeam(image02, imageTransform1, dst);

          124. imshow("dst", dst);

          125. imwrite("dst.jpg", dst);

          126. waitKey();

          127. return 0;

          128. }

          129. //優(yōu)化兩圖的連接處,使得拼接自然

          130. void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)

          131. {

          132. int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區(qū)域的左邊界

          133. double processWidth = img1.cols - start;//重疊區(qū)域的寬度

          134. int rows = dst.rows;

          135. int cols = img1.cols; //注意,是列數*通道數

          136. double alpha = 1;//img1中像素的權重

          137. for (int i = 0; i < rows; i++)

          138. {

          139. uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址

          140. uchar* t = trans.ptr<uchar>(i);

          141. uchar* d = dst.ptr<uchar>(i);

          142. for (int j = start; j < cols; j++)

          143. {

          144. //如果遇到圖像trans中無像素的黑點,則完全拷貝img1中的數據

          145. if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0)

          146. {

          147. alpha = 1;

          148. }

          149. else

          150. {

          151. //img1中像素的權重,與當前處理點距重疊區(qū)域左邊界的距離成正比,實驗證明,這種方法確實好

          152. alpha = (processWidth - (j - start)) / processWidth;

          153. }

          154. d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha);

          155. d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha);

          156. d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha);

          157. }

          158. }

          159. }

          看一看拼接效果,我覺得還是不錯的。

          看一下這一組圖片,這組圖片產生了鬼影,為什么?因為兩幅圖中的人物走動了啊!所以要做圖像拼接,盡量保證使用的是靜態(tài)圖片,不要加入一些動態(tài)因素干擾拼接。

           

          opencv自帶的拼接算法stitch

          opencv其實自己就有實現圖像拼接的算法,當然效果也是相當好的,但是因為其實現很復雜,而且代碼量很龐大,其實在一些小應用下的拼接有點殺雞用牛刀的感覺。最近在閱讀sticth源碼時,發(fā)現其中有幾個很有意思的地方。

          1.opencv stitch選擇的特征檢測方式

          一直很好奇opencv stitch算法到底選用了哪個算法作為其特征檢測方式,是ORB,SIFT還是SURF?讀源碼終于看到答案。

          1. #ifdef HAVE_OPENCV_NONFREE

          2. stitcher.setFeaturesFinder(new detail::SurfFeaturesFinder());

          3. #else

          4. stitcher.setFeaturesFinder(new detail::OrbFeaturesFinder());

          5. #endif

          在源碼createDefault函數中(默認設置),第一選擇是SURF,第二選擇才是ORB(沒有NONFREE模塊才選),所以既然大牛們這么選擇,必然是經過綜合考慮的,所以應該SURF算法在圖像拼接有著更優(yōu)秀的效果。

          2.opencv stitch獲取匹配點的方式

          以下代碼是opencv stitch源碼中的特征點提取部分,作者使用了兩次特征點提取的思路:先對圖一進行特征點提取和篩選匹配(1->2),再對圖二進行特征點的提取和匹配(2->1),這跟我們平時的一次提取的思路不同,這種二次提取的思路可以保證更多的匹配點被選中,匹配點越多,findHomography求出的變換越準確。這個思路值得借鑒。

          1. matches_info.matches.clear();

          2. Ptr<flann::IndexParams> indexParams = new flann::KDTreeIndexParams();

          3. Ptr<flann::SearchParams> searchParams = new flann::SearchParams();

          4. if (features2.descriptors.depth() == CV_8U)

          5. {

          6. indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);

          7. searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);

          8. }

          9. FlannBasedMatcher matcher(indexParams, searchParams);

          10. vector< vector<DMatch> > pair_matches;

          11. MatchesSet matches;

          12. // Find 1->2 matches

          13. matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);

          14. for (size_t i = 0; i < pair_matches.size(); ++i)

          15. {

          16. if (pair_matches[i].size() < 2)

          17. continue;

          18. const DMatch& m0 = pair_matches[i][0];

          19. const DMatch& m1 = pair_matches[i][1];

          20. if (m0.distance < (1.f - match_conf_) * m1.distance)

          21. {

          22. matches_info.matches.push_back(m0);

          23. matches.insert(make_pair(m0.queryIdx, m0.trainIdx));

          24. }

          25. }

          26. LOG("\n1->2 matches: " << matches_info.matches.size() << endl);

          27. // Find 2->1 matches

          28. pair_matches.clear();

          29. matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);

          30. for (size_t i = 0; i < pair_matches.size(); ++i)

          31. {

          32. if (pair_matches[i].size() < 2)

          33. continue;

          34. const DMatch& m0 = pair_matches[i][0];

          35. const DMatch& m1 = pair_matches[i][1];

          36. if (m0.distance < (1.f - match_conf_) * m1.distance)

          37. if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())

          38. matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));

          39. }

          40. LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);

          這里我仿照opencv源碼二次提取特征點的思路對我原有拼接代碼進行改寫,實驗證明獲取的匹配點確實較一次提取要多。

          1. //提取特征點

          2. SiftFeatureDetector Detector(1000); // 海塞矩陣閾值,在這里調整精度,值越大點越少,越精準

          3. vector<KeyPoint> keyPoint1, keyPoint2;

          4. Detector.detect(image1, keyPoint1);

          5. Detector.detect(image2, keyPoint2);

          6. //特征點描述,為下邊的特征點匹配做準備

          7. SiftDescriptorExtractor Descriptor;

          8. Mat imageDesc1, imageDesc2;

          9. Descriptor.compute(image1, keyPoint1, imageDesc1);

          10. Descriptor.compute(image2, keyPoint2, imageDesc2);

          11. FlannBasedMatcher matcher;

          12. vector<vector<DMatch> > matchePoints;

          13. vector<DMatch> GoodMatchePoints;

          14. MatchesSet matches;

          15. vector<Mat> train_desc(1, imageDesc1);

          16. matcher.add(train_desc);

          17. matcher.train();

          18. matcher.knnMatch(imageDesc2, matchePoints, 2);

          19. // Lowe's algorithm,獲取優(yōu)秀匹配點

          20. for (int i = 0; i < matchePoints.size(); i++)

          21. {

          22. if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)

          23. {

          24. GoodMatchePoints.push_back(matchePoints[i][0]);

          25. matches.insert(make_pair(matchePoints[i][0].queryIdx, matchePoints[i][0].trainIdx));

          26. }

          27. }

          28. cout<<"\n1->2 matches: " << GoodMatchePoints.size() << endl;

          29. #if 1

          30. FlannBasedMatcher matcher2;

          31. matchePoints.clear();

          32. vector<Mat> train_desc2(1, imageDesc2);

          33. matcher2.add(train_desc2);

          34. matcher2.train();

          35. matcher2.knnMatch(imageDesc1, matchePoints, 2);

          36. // Lowe's algorithm,獲取優(yōu)秀匹配點

          37. for (int i = 0; i < matchePoints.size(); i++)

          38. {

          39. if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)

          40. {

          41. if (matches.find(make_pair(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx)) == matches.end())

          42. {

          43. GoodMatchePoints.push_back(DMatch(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx, matchePoints[i][0].distance));

          44. }

          45. }

          46. }

          47. cout<<"1->2 & 2->1 matches: " << GoodMatchePoints.size() << endl;

          48. #endif

          最后再看一下opencv stitch的拼接效果吧~速度雖然比較慢,但是效果還是很好的。

          1. #include <iostream>

          2. #include <opencv2/core/core.hpp>

          3. #include <opencv2/highgui/highgui.hpp>

          4. #include <opencv2/imgproc/imgproc.hpp>

          5. #include <opencv2/stitching/stitcher.hpp>

          6. using namespace std;

          7. using namespace cv;

          8. bool try_use_gpu = false;

          9. vector<Mat> imgs;

          10. string result_name = "dst1.jpg";

          11. int main(int argc, char * argv[])

          12. {

          13. Mat img1 = imread("34.jpg");

          14. Mat img2 = imread("35.jpg");

          15. imshow("p1", img1);

          16. imshow("p2", img2);

          17. if (img1.empty() || img2.empty())

          18. {

          19. cout << "Can't read image" << endl;

          20. return -1;

          21. }

          22. imgs.push_back(img1);

          23. imgs.push_back(img2);

          24. Stitcher stitcher = Stitcher::createDefault(try_use_gpu);

          25. // 使用stitch函數進行拼接

          26. Mat pano;

          27. Stitcher::Status status = stitcher.stitch(imgs, pano);

          28. if (status != Stitcher::OK)

          29. {

          30. cout << "Can't stitch images, error code = " << int(status) << endl;

          31. return -1;

          32. }

          33. imwrite(result_name, pano);

          34. Mat pano2 = pano.clone();

          35. // 顯示源圖像,和結果圖像

          36. imshow("全景圖像", pano);

          37. if (waitKey() == 27)

          38. return 0;

          39. }

          —版權聲明—

          僅用于學術分享,版權屬于原作者。

          若有侵權,請聯系微信號:yiyang-sy 刪除或修改!


          —THE END—
          瀏覽 38
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天日天天草 | 亚洲日韩一级精品片在线播放 | 色视频在线播放 | 靠逼视频免费 | 黄片子视频 |