图像拼接和图像融合技术

新机器视觉

共 20204字,需浏览 41分钟

 · 2021-08-09

点击下方卡片,关注“新机器视觉”公众号

视觉/图像重磅干货,第一时间送达

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

 图像拼接在实际的应用场景很广,比如无人机航拍,遥感图像等等,图像拼接是进一步做图像理解基础步骤,拼接效果的好坏直接影响接下来的工作,所以一个好的图像拼接算法非常重要。

再举一个身边的例子吧,你用你的手机对某一场景拍照,但是你没有办法一次将所有你要拍的景物全部拍下来,所以你对该场景从左往右依次拍了好几张图,来把你要拍的所有景物记录下来。那么我们能不能把这些图像拼接成一个大图呢?我们利用opencv就可以做到图像拼接的效果!

比如我们有对这两张图进行拼接。

从上面两张图可以看出,这两张图有比较多的重叠部分,这也是拼接的基本要求。

那么要实现图像拼接需要那几步呢?简单来说有以下几步:

  1. 对每幅图进行特征点提取

  2. 对对特征点进行匹配

  3. 进行图像配准

  4. 把图像拷贝到另一幅图像的特定位置

  5. 对重叠边界进行特殊处理

好吧,那就开始正式实现图像配准。

第一步就是特征点提取。现在CV领域有很多特征点的定义,比如sift、surf、harris角点、ORB都是很有名的特征因子,都可以用来做图像拼接的工作,他们各有优势。本文将使用ORB和SURF进行图像拼接,用其他方法进行拼接也是类似的。

 

基于SURF的图像拼接

用SIFT算法来实现图像拼接是很常用的方法,但是因为SIFT计算量很大,所以在速度要求很高的场合下不再适用。所以,它的改进方法SURF因为在速度方面有了明显的提高(速度是SIFT的3倍),所以在图像拼接领域还是大有作为。虽说SURF精确度和稳定性不及SIFT,但是其综合能力还是优越一些。下面将详细介绍拼接的主要步骤。

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,获取优秀匹配点

  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算法继续筛选可靠地匹配点,这使得匹配点解更为精确。

  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. //创建拼接后的图,需提前计算图的大小

  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.图像融合(去裂缝处理)

从上图可以看出,两图的拼接并不自然,原因就在于拼接图的交界处,两图因为光照色泽的原因使得两图交界处的过渡很糟糕,所以需要特定的处理解决这种不自然。这里的处理思路是加权融合,在重叠部分由前一幅图像慢慢过渡到第二幅图像,即将图像的重叠区域的像素值按一定的权值相加合成新的图像。

  1. //优化两图的连接处,使得拼接自然

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

  3. {

  4. int start = MIN(corners.left_top.x, corners.left_bottom.x);//开始位置,即重叠区域的左边界

  5. double processWidth = img1.cols - start;//重叠区域的宽度

  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中像素的权重,与当前处理点距重叠区域左边界的距离成正比,实验证明,这种方法确实好

  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,获取优秀匹配点

  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. //创建拼接后的图,需提前计算图的大小

  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. //优化两图的连接处,使得拼接自然

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

  134. {

  135. int start = MIN(corners.left_top.x, corners.left_bottom.x);//开始位置,即重叠区域的左边界

  136. double processWidth = img1.cols - start;//重叠区域的宽度

  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中像素的权重,与当前处理点距重叠区域左边界的距离成正比,实验证明,这种方法确实好

  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,获取优秀匹配点

  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. //创建拼接后的图,需提前计算图的大小

  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. //优化两图的连接处,使得拼接自然

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

  131. {

  132. int start = MIN(corners.left_top.x, corners.left_bottom.x);//开始位置,即重叠区域的左边界

  133. double processWidth = img1.cols - start;//重叠区域的宽度

  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中像素的权重,与当前处理点距重叠区域左边界的距离成正比,实验证明,这种方法确实好

  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. }

看一看拼接效果,我觉得还是不错的。

看一下这一组图片,这组图片产生了鬼影,为什么?因为两幅图中的人物走动了啊!所以要做图像拼接,尽量保证使用的是静态图片,不要加入一些动态因素干扰拼接。

 

opencv自带的拼接算法stitch

opencv其实自己就有实现图像拼接的算法,当然效果也是相当好的,但是因为其实现很复杂,而且代码量很庞大,其实在一些小应用下的拼接有点杀鸡用牛刀的感觉。最近在阅读sticth源码时,发现其中有几个很有意思的地方。

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算法在图像拼接有着更优秀的效果。

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,获取优秀匹配点

  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,获取优秀匹配点

  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—
浏览 14
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报