FROM:http://blog.csdn.net/tulun/article/details/6456188

SAD算法具体原理见相关图像处理书籍。

该程序是opencv中文论坛的牛人贡献的,感谢他的工作。

(程序所需图片可以在网上找如http://vision.middlebury.edu/stereo/data/scenes2003/)

[c-sharp] view plaincopyprint?
  1. // Sum of Absolute Difference(SAD)
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <cv.h>
  6. #include <cxcore.h>
  7. #include <highgui.h>
  8. #include <math.h>
  9. #include <ctime>
  10. using namespace std;
  11. template<class T> class Image
  12. {
  13. private:
  14. IplImage* imgp;
  15. public:
  16. Image(IplImage* img=0){imgp=img;}
  17. ~Image(){imgp=0;}
  18. void operator=(IplImage* img){imgp=img;}
  19. inline T* operator[](const int rowIndx)
  20. {
  21. return((T*)(imgp->imageData+rowIndx*imgp->widthStep));
  22. }
  23. };
  24. typedef struct
  25. {
  26. unsigned char b,g,r;
  27. }RgbPixel;
  28. typedef struct
  29. {
  30. float b,g,r;
  31. }RgbPixelFloat;
  32. typedef Image<RgbPixel> RgbImage;
  33. typedef Image<RgbPixelFloat> RgbImageFloat;
  34. typedef Image<unsigned char> BwImage;
  35. typedef Image<float> BwImageFloat;
  36. //display an image in a new window with title to be given.
  37. void displayImageNewWindow(char* title,CvArr* img)
  38. {
  39. cvNamedWindow(title, CV_WINDOW_AUTOSIZE );
  40. cvShowImage(title,img);
  41. }
  42. int getMaxMin(double value[],int valueSize, int maxmin)
  43. {
  44. int pos=0;
  45. int i=0;
  46. double max1=-1;//?-999999;
  47. double min1=999999;
  48. if (maxmin==1)
  49. {
  50. //find max
  51. for (i=0;i<valueSize;i++)
  52. {
  53. //find the index with the max value;
  54. if (value[i]>max1)
  55. {
  56. pos=i;
  57. max1=value[i];
  58. }
  59. }
  60. }
  61. if (maxmin==0)
  62. {
  63. //find min
  64. for (i=0;i<valueSize;i++)
  65. {
  66. //find the index with the minimum value;
  67. if (value[i]<min1)
  68. {
  69. pos=i;
  70. min1=value[i];
  71. }
  72. }
  73. }
  74. return pos;
  75. }
  76. IplImage* generateDisparityImage(IplImage* greyLeftImg32,IplImage* greyRightImg32,int windowSize,int DSR)
  77. {
  78. int offset=floor((double)windowSize/2);
  79. int height=greyLeftImg32->height;
  80. int width=greyLeftImg32->width;
  81. double* localSAD=new double[DSR];
  82. int x=0, y=0,d=0,m=0;
  83. int N=windowSize;
  84. IplImage* winImg=cvCreateImage(cvSize(N,N),32,1);//mySubImage(greyLeftImg32,cvRect(0,0,N,N));
  85. IplImage* disparity=cvCreateImage(cvSize(width,height),8,1);//or IPL_DEPTH_8U
  86. BwImage imgA(disparity);
  87. for (y=0;y<height;y++)
  88. {
  89. for (x=0;x<width;x++)
  90. {
  91. imgA[y][x]=0;
  92. }
  93. }
  94. CvScalar sum;
  95. //CvScalar s2;
  96. for (y=0;y<height-N;y++)
  97. {
  98. //height-N
  99. for (x=0;x<width-N;x++)
  100. {
  101. //width-N
  102. cvSetImageROI(greyLeftImg32, cvRect(x,y,N,N));
  103. d=0;
  104. //initialise localSAD
  105. for (m=0;m<DSR;m++)
  106. {
  107. localSAD[m]=0;
  108. }
  109. //start matching
  110. do{
  111. if (x-d>=0)
  112. {
  113. cvSetImageROI(greyRightImg32, cvRect(x-d,y,N,N));
  114. }
  115. else
  116. {
  117. break;
  118. }
  119. cvAbsDiff(greyLeftImg32,greyRightImg32,winImg);//absolute difference
  120. sum=cvSum(winImg);//sum
  121. localSAD[d]=sum.val[0];//0 means single channel
  122. cvResetImageROI(greyRightImg32);
  123. d++;
  124. }while(d<=DSR);
  125. //to find the best d and store
  126. imgA[y+offset][x+offset]=getMaxMin(localSAD,DSR,0)*16; //0 means return minimum index
  127. cvResetImageROI(greyLeftImg32);
  128. }//x
  129. if (y%10==0)
  130. cout<<"row="<<y<<" of "<<height<<endl;
  131. }//y
  132. cvReleaseImage(&winImg);
  133. //cvReleaseImage(&rightWinImg);
  134. return disparity;
  135. }
  136. int main (int argc, char * const argv[])
  137. {
  138. cout << "Sum of Absolute Difference(SAD) Strereo Vision"<<endl;
  139. //**********image input*********************//
  140. char* filename1="L.jpg";//im2_cone.png
  141. IplImage* greyLeftImg= cvLoadImage(filename1,0);
  142. char* filename2="R.jpg";
  143. IplImage* greyRightImg= cvLoadImage(filename2,0);
  144. if (greyLeftImg==NULL){cout << "No valid image input."<<endl; return 1;}
  145. if (greyRightImg==NULL){cout << "No valid image input."<<endl; return 1;}
  146. int width=greyLeftImg->width;
  147. int height=greyLeftImg->height;
  148. /****************8U to 32F**********************/
  149. IplImage* greyLeftImg32=cvCreateImage(cvSize(width,height),32,1);//IPL_DEPTH_32F
  150. IplImage* greyRightImg32=cvCreateImage(cvSize(width,height),32,1);
  151. cvConvertScale(greyLeftImg, greyLeftImg32, 1/255.);
  152. cvConvertScale(greyRightImg, greyRightImg32, 1/255.);//1/255. equals to 1/255.0
  153. //-------------obtain disparity image----------------
  154. time_t tstart, tend;
  155. tstart = time(0);
  156. int windowSize=13,DSR=20;//Disparity Search Range
  157. IplImage* disparity32=generateDisparityImage(greyLeftImg32,greyRightImg32,windowSize,DSR);
  158. tend = time(0);
  159. cout << "It took "<< difftime(tend, tstart) <<" second(s)."<< endl;
  160. displayImageNewWindow("Dispairty Image",disparity32);
  161. displayImageNewWindow("Left Image",greyLeftImg32);
  162. displayImageNewWindow("Right Image",greyRightImg32);
  163. //cvSaveImage("D:/OpenCV_stuff/SampleImages/disparitySAD.jpg",disparity32);
  164. //********destroy window************/
  165. cvWaitKey(0);
  166. cvReleaseImage(&greyLeftImg32);
  167. cvReleaseImage(&greyRightImg32);
  168. cvReleaseImage(&greyLeftImg);
  169. cvReleaseImage(&greyRightImg);
  170. cvReleaseImage(&disparity32);
  171. cvDestroyWindow("Left Image");
  172. cvDestroyWindow("Right Image");
  173. cvDestroyWindow("Dispairty Image");
  174. return 0;
  175. }

SAD立体匹配算法在opencv中的实现相关推荐

  1. python立体匹配评价,sad立体匹配算法的Python实现,SAD,PYTHON

    SAD立体匹配算法的PYTHON实现 这是第一次发CSDN博客,因为在机器视觉的学习中CSDN帮助了我很多,那么我也应该为CSDN社区做一些贡献,所以本文将介绍我用python实现的SAD匹配算法. ...

  2. 双目立体匹配算法漫谈

    双目立体匹配算法漫谈 双目立体匹配算法漫谈 前提 一些基本假设 框架 matching cost computation cost (support) aggregation;代价聚合 双目立体匹配算 ...

  3. OpenCv中实现了三种立体匹配算法:

    OpenCv中实现了三种立体匹配算法: BM算法 SGBM算法 Stereo Processing by Semiglobal Matching and Mutual Information GC算法 ...

  4. python立体匹配误匹配率_立体匹配算法(Stereo Matching)及其在OpenCV中的应用

    模拟人的两只眼睛的Stereo相机最近变得很受欢迎.通过对stereo相机拍摄的左右两张图进行匹配找出视差图,可以还原物体的3D信息. 立体匹配(Stereo matching)的步骤如下: 1: 预 ...

  5. 立体匹配算法-SAD

    目录 前言 SAD 是一种简单高效的立体匹配算法,虽然由于精度等原因很少被实际应用,但可以帮助我们理解立体匹配过程 一.SAD算法原理 SAD计算过程主要包括以下步骤: 二.代码示例 1.引入库 2. ...

  6. OpenCV中的立体图像创建深度图

    OpenCV中的立体图像创建深度图 1. 效果图 2. 源码 参考 这篇博客将介绍如何从立体图像创建深度图. 1. 效果图 原图 VS 视差图效果如下: 可以看到结果受到高度噪音的污染.通过调整 nu ...

  7. 基于深度学习算法和传统立体匹配算法的双目立体视觉

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 01 立体视觉是什么? 在开始之前,我相信很多站友都会有这个疑问, ...

  8. 局部立体匹配算法介绍及代码实现

    作者I dulingwen@CSDN 编辑I 3D视觉开发者社区 01 什么是局部匹配算法?优势如何? 局部(Local)立体匹配是相对于半全局以及全局(Non-Local)立体匹配算法而言的,它不构 ...

  9. 双目立体匹配算法SGBM

    semi-global matching(SGM)是一种用于计算双目视觉中视差(disparity)的半全局匹配算法,在OpenCV中的实现为semi-global block matching(SG ...

最新文章

  1. 在CentOS/Debian/Ubuntu上编译安装最新版gnu make 和GNU 'binutils' (as and ld)
  2. 简化工作流程,10款必备的HTML5开发工具
  3. java趣味题-打印杨辉三角
  4. 牛客小白月赛8: E. 诡异数字(数位DP)
  5. Zabbix安装界面显示PHP time zone 为“红色”的解决办法
  6. “广” “专”的抉择 -- 个人技术发展之我见!
  7. Android 直接生成实体类工具GsonFormat,一键生成实体类,对象;GsonFormat插件安装步骤gsonformatplus生成对象报错解决;jason,json
  8. 国内外最佳的photoshop教程网站推荐
  9. excel mysql 财务_excel函数 数据库 财务函数 统计函数 信息函数
  10. Onvif协议之服务端开发基本流程
  11. CG资源网 - Maya教程
  12. 因使用率过低,谷歌翻译退出中国
  13. 平板手写笔有必要买吗?开学季便宜又好用电容笔推荐
  14. PUBG国际服怎么下载 苹果安卓官网下载方法教程
  15. 【算法】【链表模块】删除链表的中间节点或a/b节点
  16. 電影嘗試三 webtorrent-desktop在線觀看電影magnet
  17. 福州大学计算机学硕分数,2021年福州大学考研录取分数线应该在哪里查询?
  18. 编程课程与数学的关系
  19. 大数据开发的面试总结
  20. Process ‘command ‘C:/Program Files/Java/jdk1.8.0_192/bin/java.exe‘‘ finished with non-zero exit valu

热门文章

  1. leetcode算法题--K 个一组翻转链表
  2. oracle第二章数据的运用,第二章:oracle_sql语句之限制(where子句)和排列数据(order by子句)...
  3. LightOJ 1084 Winter(记忆化搜索)
  4. 如何更好的利用Node.js的性能极限
  5. 使用Gson 解析json
  6. 上市公司相关财务指标
  7. 对学习JAVA的总结 第一次课
  8. 多功能选择列表(左右选择)
  9. FCKeditor在线文本编辑器初级应用
  10. 剑指offer 算法 (递归与循环)