当前位置:   article > 正文

双目立体视觉匹配算法之视差图disparity计算——SAD算法、SGBM算法_sad和sgbm

sad和sgbm

一、SAD算法

1.算法原理
        SAD(Sum of absolute differences)是一种图像匹配算法。基本思想:差的绝对值之和。此算法常用于图像块匹配,将每个像素对应数值之差的绝对值求和,据此评估两个图像块的相似度。该算法快速、但并不精确,通常用于多级处理的初步筛选。

2.基本流程

输入:两幅图像,一幅Left-Image,一幅Right-Image

对左图,依次扫描,选定一个锚点:

(1)构造一个小窗口,类似于卷积核;
(2)用窗口覆盖左边的图像,选择出窗口覆盖区域内的所有像素点;
(3)同样用窗口覆盖右边的图像并选择出覆盖区域的像素点;
(4)左边覆盖区域减去右边覆盖区域,并求出所有像素点灰度差的绝对值之和;
(5)移动右边图像的窗口,重复(3)-(4)的处理(这里有个搜索范围,超过这个范围跳出);
(6)找到这个范围内SAD值最小的窗口,即找到了左图锚点的最佳匹配的像素块。

3、代码

  1. #include<iostream>
  2. #include<opencv2/opencv.hpp>
  3. using namespace std;
  4. using namespace cv;
  5. class SAD
  6. {
  7. private:
  8. int winSize;//卷积核尺寸
  9. int DSR;//视差搜索范围
  10. public:
  11. SAD() :winSize(7), DSR(30){}
  12. SAD(int _winSize, int _DSR) :winSize(_winSize), DSR(_DSR){}
  13. Mat computerSAD(Mat&L, Mat&R);//计算SAD
  14. };
  15. Mat SAD::computerSAD(Mat&L, Mat&R)
  16. {
  17. int Height = L.rows;
  18. int Width = L.cols;
  19. Mat Kernel_L(Size(winSize, winSize), CV_8U, Scalar::all(0));
  20. //CV_8U:0~255的值,大多数图像/视频的格式,该段设置全0矩阵
  21. Mat Kernel_R(Size(winSize, winSize), CV_8U, Scalar::all(0));
  22. Mat Disparity(Height, Width, CV_8U, Scalar(0));
  23. for (int i = 0; i < Width - winSize; ++i){
  24. for (int j = 0; j < Height - winSize; ++j){
  25. Kernel_L = L(Rect(i, j, winSize, winSize));//L为做图像,Kernel为这个范围内的左图
  26. Mat MM(1, DSR, CV_32F, Scalar(0));//定义匹配范围
  27. for (int k = 0; k < DSR; ++k){
  28. int x = i - k;
  29. if (x >= 0){
  30. Kernel_R = R(Rect(x, j, winSize, winSize));
  31. Mat Dif;
  32. absdiff(Kernel_L, Kernel_R, Dif);
  33. Scalar ADD = sum(Dif);
  34. float a = ADD[0];
  35. MM.at<float>(k) = a;
  36. }
  37. Point minLoc;
  38. minMaxLoc(MM, NULL, NULL, &minLoc, NULL);
  39. int loc = minLoc.x;
  40. Disparity.at<char>(j, i) = loc * 16;
  41. }
  42. double rate = double(i) / (Width);
  43. cout << "已完成" << setprecision(2) << rate * 100 << "%" << endl;
  44. }
  45. }
  46. return Disparity;
  47. }
  48. int main()
  49. {
  50. Mat left = imread("Left.png");
  51. Mat right = imread("Right.png");
  52. //-------图像显示-----------
  53. namedWindow("leftimag");
  54. imshow("leftimag", left);
  55. namedWindow("rightimag");
  56. imshow("rightimag", right);
  57. //--------由SAD求取视差图-----
  58. Mat Disparity;
  59. SAD mySAD(7, 30);
  60. Disparity = mySAD.computerSAD(left, right);
  61. //-------结果显示------
  62. namedWindow("Disparity");
  63. imshow("Disparity", Disparity);
  64. //-------收尾------
  65. waitKey(0);
  66. return 0;
  67. }

4、结果

左图:

右图:

视差图结果:

二、SGBM算法

1、SGBM算法作为一种全局匹配算法,立体匹配的效果明显好于局部匹配算法,但是同时复杂度上也要远远大于局部匹配算法。算法主要是参考Stereo Processing by Semiglobal Matching and Mutual Information。

opencv中实现的SGBM算法计算匹配代价没有按照原始论文的互信息作为代价,而是按照块匹配的代价。

对该算法的具体讲解可以参考:https://www.cnblogs.com/hrlnw/p/4746170.html

参考:http://www.opencv.org.cn/forum.php?mod=viewthread&tid=23854

2、代码

  1. #include<iostream>
  2. #include<opencv2/opencv.hpp>
  3. using namespace std;
  4. using namespace cv;
  5. class SGBM
  6. {
  7. private:
  8. enum mode_view { LEFT, RIGHT };
  9. mode_view view; //输出左视差图or右视差图
  10. public:
  11. SGBM() {};
  12. SGBM(mode_view _mode_view) :view(_mode_view) {};
  13. ~SGBM() {};
  14. Mat computersgbm(Mat &L, Mat &R); //计算SGBM
  15. };
  16. Mat SGBM::computersgbm(Mat &L, Mat &R)
  17. /*SGBM_matching SGBM算法
  18. *@param Mat &left_image :左图像
  19. *@param Mat &right_image:右图像
  20. */
  21. {
  22. Mat disp;
  23. int numberOfDisparities = ((L.size().width / 8) + 15)&-16;
  24. Ptr<StereoSGBM> sgbm = StereoSGBM::create(0, 16, 3);
  25. sgbm->setPreFilterCap(32);
  26. int SADWindowSize = 5;
  27. int sgbmWinSize = SADWindowSize > 0 ? SADWindowSize : 3;
  28. sgbm->setBlockSize(sgbmWinSize);
  29. int cn = L.channels();
  30. sgbm->setP1(8 * cn*sgbmWinSize*sgbmWinSize);
  31. sgbm->setP2(32 * cn*sgbmWinSize*sgbmWinSize);
  32. sgbm->setMinDisparity(0);
  33. sgbm->setNumDisparities(numberOfDisparities);
  34. sgbm->setUniquenessRatio(10);
  35. sgbm->setSpeckleWindowSize(100);
  36. sgbm->setSpeckleRange(32);
  37. sgbm->setDisp12MaxDiff(1);
  38. Mat left_gray, right_gray;
  39. cvtColor(L, left_gray, CV_BGR2GRAY);
  40. cvtColor(R, right_gray, CV_BGR2GRAY);
  41. view = LEFT;
  42. if (view == LEFT) //计算左视差图
  43. {
  44. sgbm->compute(left_gray, right_gray, disp);
  45. disp.convertTo(disp, CV_32F, 1.0 / 16); //除以16得到真实视差值
  46. Mat disp8U = Mat(disp.rows, disp.cols, CV_8UC1);
  47. normalize(disp, disp8U, 0, 255, NORM_MINMAX, CV_8UC1);
  48. imwrite("results/SGBM.jpg", disp8U);
  49. return disp8U;
  50. }
  51. else if (view == RIGHT) //计算右视差图
  52. {
  53. sgbm->setMinDisparity(-numberOfDisparities);
  54. sgbm->setNumDisparities(numberOfDisparities);
  55. sgbm->compute(left_gray, right_gray, disp);
  56. disp.convertTo(disp, CV_32F, 1.0 / 16); //除以16得到真实视差值
  57. Mat disp8U = Mat(disp.rows, disp.cols, CV_8UC1);
  58. normalize(disp, disp8U, 0, 255, NORM_MINMAX, CV_8UC1);
  59. imwrite("results/SGBM.jpg", disp8U);
  60. return disp8U;
  61. }
  62. else
  63. {
  64. return Mat();
  65. }
  66. }
  67. int main()
  68. {
  69. Mat left = imread("Left.png");
  70. Mat right = imread("Right.png");
  71. //-------图像显示-----------
  72. namedWindow("leftimag");
  73. imshow("leftimag", left);
  74. namedWindow("rightimag");
  75. imshow("rightimag", right);
  76. //--------由SAD求取视差图-----
  77. Mat Disparity;
  78. SGBM mySGBM;
  79. Disparity = mySGBM.computersgbm(left, right);
  80. //-------结果显示------
  81. namedWindow("Disparity");
  82. imshow("Disparity", Disparity);
  83. //-------收尾------
  84. waitKey(0);
  85. return 0;
  86. }

3、结果

所用的左右图同上,所得结果为:

 

 

 

NB:对于使用的其他算法本次没有实验,故没有介绍,可以参考:https://blog.csdn.net/liulina603/article/details/53302168

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/596472
推荐阅读
相关标签
  

闽ICP备14008679号