SAD立体匹配算法在opencv中的实现
FROM:http://blog.csdn.net/tulun/article/details/6456188
SAD算法具体原理见相关图像处理书籍。
该程序是opencv中文论坛的牛人贡献的,感谢他的工作。
(程序所需图片可以在网上找如http://vision.middlebury.edu/stereo/data/scenes2003/)
- // Sum of Absolute Difference(SAD)
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- #include <math.h>
- #include <ctime>
- using namespace std;
- template<class T> class Image
- {
- private:
- IplImage* imgp;
- public:
- Image(IplImage* img=0){imgp=img;}
- ~Image(){imgp=0;}
- void operator=(IplImage* img){imgp=img;}
- inline T* operator[](const int rowIndx)
- {
- return((T*)(imgp->imageData+rowIndx*imgp->widthStep));
- }
- };
- typedef struct
- {
- unsigned char b,g,r;
- }RgbPixel;
- typedef struct
- {
- float b,g,r;
- }RgbPixelFloat;
- typedef Image<RgbPixel> RgbImage;
- typedef Image<RgbPixelFloat> RgbImageFloat;
- typedef Image<unsigned char> BwImage;
- typedef Image<float> BwImageFloat;
- //display an image in a new window with title to be given.
- void displayImageNewWindow(char* title,CvArr* img)
- {
- cvNamedWindow(title, CV_WINDOW_AUTOSIZE );
- cvShowImage(title,img);
- }
- int getMaxMin(double value[],int valueSize, int maxmin)
- {
- int pos=0;
- int i=0;
- double max1=-1;//?-999999;
- double min1=999999;
- if (maxmin==1)
- {
- //find max
- for (i=0;i<valueSize;i++)
- {
- //find the index with the max value;
- if (value[i]>max1)
- {
- pos=i;
- max1=value[i];
- }
- }
- }
- if (maxmin==0)
- {
- //find min
- for (i=0;i<valueSize;i++)
- {
- //find the index with the minimum value;
- if (value[i]<min1)
- {
- pos=i;
- min1=value[i];
- }
- }
- }
- return pos;
- }
- IplImage* generateDisparityImage(IplImage* greyLeftImg32,IplImage* greyRightImg32,int windowSize,int DSR)
- {
- int offset=floor((double)windowSize/2);
- int height=greyLeftImg32->height;
- int width=greyLeftImg32->width;
- double* localSAD=new double[DSR];
- int x=0, y=0,d=0,m=0;
- int N=windowSize;
- IplImage* winImg=cvCreateImage(cvSize(N,N),32,1);//mySubImage(greyLeftImg32,cvRect(0,0,N,N));
- IplImage* disparity=cvCreateImage(cvSize(width,height),8,1);//or IPL_DEPTH_8U
- BwImage imgA(disparity);
- for (y=0;y<height;y++)
- {
- for (x=0;x<width;x++)
- {
- imgA[y][x]=0;
- }
- }
- CvScalar sum;
- //CvScalar s2;
- for (y=0;y<height-N;y++)
- {
- //height-N
- for (x=0;x<width-N;x++)
- {
- //width-N
- cvSetImageROI(greyLeftImg32, cvRect(x,y,N,N));
- d=0;
- //initialise localSAD
- for (m=0;m<DSR;m++)
- {
- localSAD[m]=0;
- }
- //start matching
- do{
- if (x-d>=0)
- {
- cvSetImageROI(greyRightImg32, cvRect(x-d,y,N,N));
- }
- else
- {
- break;
- }
- cvAbsDiff(greyLeftImg32,greyRightImg32,winImg);//absolute difference
- sum=cvSum(winImg);//sum
- localSAD[d]=sum.val[0];//0 means single channel
- cvResetImageROI(greyRightImg32);
- d++;
- }while(d<=DSR);
- //to find the best d and store
- imgA[y+offset][x+offset]=getMaxMin(localSAD,DSR,0)*16; //0 means return minimum index
- cvResetImageROI(greyLeftImg32);
- }//x
- if (y%10==0)
- cout<<"row="<<y<<" of "<<height<<endl;
- }//y
- cvReleaseImage(&winImg);
- //cvReleaseImage(&rightWinImg);
- return disparity;
- }
- int main (int argc, char * const argv[])
- {
- cout << "Sum of Absolute Difference(SAD) Strereo Vision"<<endl;
- //**********image input*********************//
- char* filename1="L.jpg";//im2_cone.png
- IplImage* greyLeftImg= cvLoadImage(filename1,0);
- char* filename2="R.jpg";
- IplImage* greyRightImg= cvLoadImage(filename2,0);
- if (greyLeftImg==NULL){cout << "No valid image input."<<endl; return 1;}
- if (greyRightImg==NULL){cout << "No valid image input."<<endl; return 1;}
- int width=greyLeftImg->width;
- int height=greyLeftImg->height;
- /****************8U to 32F**********************/
- IplImage* greyLeftImg32=cvCreateImage(cvSize(width,height),32,1);//IPL_DEPTH_32F
- IplImage* greyRightImg32=cvCreateImage(cvSize(width,height),32,1);
- cvConvertScale(greyLeftImg, greyLeftImg32, 1/255.);
- cvConvertScale(greyRightImg, greyRightImg32, 1/255.);//1/255. equals to 1/255.0
- //-------------obtain disparity image----------------
- time_t tstart, tend;
- tstart = time(0);
- int windowSize=13,DSR=20;//Disparity Search Range
- IplImage* disparity32=generateDisparityImage(greyLeftImg32,greyRightImg32,windowSize,DSR);
- tend = time(0);
- cout << "It took "<< difftime(tend, tstart) <<" second(s)."<< endl;
- displayImageNewWindow("Dispairty Image",disparity32);
- displayImageNewWindow("Left Image",greyLeftImg32);
- displayImageNewWindow("Right Image",greyRightImg32);
- //cvSaveImage("D:/OpenCV_stuff/SampleImages/disparitySAD.jpg",disparity32);
- //********destroy window************/
- cvWaitKey(0);
- cvReleaseImage(&greyLeftImg32);
- cvReleaseImage(&greyRightImg32);
- cvReleaseImage(&greyLeftImg);
- cvReleaseImage(&greyRightImg);
- cvReleaseImage(&disparity32);
- cvDestroyWindow("Left Image");
- cvDestroyWindow("Right Image");
- cvDestroyWindow("Dispairty Image");
- return 0;
- }
SAD立体匹配算法在opencv中的实现相关推荐
- python立体匹配评价,sad立体匹配算法的Python实现,SAD,PYTHON
SAD立体匹配算法的PYTHON实现 这是第一次发CSDN博客,因为在机器视觉的学习中CSDN帮助了我很多,那么我也应该为CSDN社区做一些贡献,所以本文将介绍我用python实现的SAD匹配算法. ...
- 双目立体匹配算法漫谈
双目立体匹配算法漫谈 双目立体匹配算法漫谈 前提 一些基本假设 框架 matching cost computation cost (support) aggregation;代价聚合 双目立体匹配算 ...
- OpenCv中实现了三种立体匹配算法:
OpenCv中实现了三种立体匹配算法: BM算法 SGBM算法 Stereo Processing by Semiglobal Matching and Mutual Information GC算法 ...
- python立体匹配误匹配率_立体匹配算法(Stereo Matching)及其在OpenCV中的应用
模拟人的两只眼睛的Stereo相机最近变得很受欢迎.通过对stereo相机拍摄的左右两张图进行匹配找出视差图,可以还原物体的3D信息. 立体匹配(Stereo matching)的步骤如下: 1: 预 ...
- 立体匹配算法-SAD
目录 前言 SAD 是一种简单高效的立体匹配算法,虽然由于精度等原因很少被实际应用,但可以帮助我们理解立体匹配过程 一.SAD算法原理 SAD计算过程主要包括以下步骤: 二.代码示例 1.引入库 2. ...
- OpenCV中的立体图像创建深度图
OpenCV中的立体图像创建深度图 1. 效果图 2. 源码 参考 这篇博客将介绍如何从立体图像创建深度图. 1. 效果图 原图 VS 视差图效果如下: 可以看到结果受到高度噪音的污染.通过调整 nu ...
- 基于深度学习算法和传统立体匹配算法的双目立体视觉
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 01 立体视觉是什么? 在开始之前,我相信很多站友都会有这个疑问, ...
- 局部立体匹配算法介绍及代码实现
作者I dulingwen@CSDN 编辑I 3D视觉开发者社区 01 什么是局部匹配算法?优势如何? 局部(Local)立体匹配是相对于半全局以及全局(Non-Local)立体匹配算法而言的,它不构 ...
- 双目立体匹配算法SGBM
semi-global matching(SGM)是一种用于计算双目视觉中视差(disparity)的半全局匹配算法,在OpenCV中的实现为semi-global block matching(SG ...
最新文章
- 在CentOS/Debian/Ubuntu上编译安装最新版gnu make 和GNU 'binutils' (as and ld)
- 简化工作流程,10款必备的HTML5开发工具
- java趣味题-打印杨辉三角
- 牛客小白月赛8: E. 诡异数字(数位DP)
- Zabbix安装界面显示PHP time zone 为“红色”的解决办法
- “广” “专”的抉择 -- 个人技术发展之我见!
- Android 直接生成实体类工具GsonFormat,一键生成实体类,对象;GsonFormat插件安装步骤gsonformatplus生成对象报错解决;jason,json
- 国内外最佳的photoshop教程网站推荐
- excel mysql 财务_excel函数 数据库 财务函数 统计函数 信息函数
- Onvif协议之服务端开发基本流程
- CG资源网 - Maya教程
- 因使用率过低,谷歌翻译退出中国
- 平板手写笔有必要买吗?开学季便宜又好用电容笔推荐
- PUBG国际服怎么下载 苹果安卓官网下载方法教程
- 【算法】【链表模块】删除链表的中间节点或a/b节点
- 電影嘗試三 webtorrent-desktop在線觀看電影magnet
- 福州大学计算机学硕分数,2021年福州大学考研录取分数线应该在哪里查询?
- 编程课程与数学的关系
- 大数据开发的面试总结
- Process ‘command ‘C:/Program Files/Java/jdk1.8.0_192/bin/java.exe‘‘ finished with non-zero exit valu