资源简介
基于SAD算法的图像处理,图像的最小值与最大值
代码片段和文件信息
#include “cv.h“
#include “highgui.h“
#include
#include
#include
int SAD(IplImage *image1IplImage*image2int x1int x2int y12int WindowSize)
{
//H_CHECK(CV_ARE_SIZES_EQ(image1image2));
int W=image1->widthH=image1->height;
// H_CHECK(x1>=WindowSize && x1 // &&x2>=WindowSize && x2 // &&y12>=WindowSize && y12 // );
int dxdy;
int sum=0;
for (dy=-WindowSize;dy<=WindowSize;dy++)
{
LPBYTE p1=&CV_IMAGE_ELEM(image1BYTEy12+dyx1-WindowSize);
LPBYTE p2=&CV_IMAGE_ELEM(image2BYTEy12+dyx2-WindowSize);
for (dx=-WindowSize;dx<=WindowSize;dx++)
{
sum+=abs(*p1-*p2);
p1++;
p2++;
}
}
return sum;
}
int GetDisparitySAD(IplImage *image1IplImage*image2int xint yint WindowSizeint maxDX)
{
//H_CHECK(x-maxDX-WindowSize >=0 && x+WindowSizewidth );
int x2i;
int sadMin=99999dispBest=0;
for(i=1;i {
x2=x-i;
int sad=SAD(image1image2xx2yWindowSize);
if(sadMin >sad)
{
sadMin=sad;
dispBest=i;
}
}
return dispBest;
}
int GetDisparitySAD_Reverse(IplImage *image1IplImage*image2int xrint yint WindowSizeint maxDX)
{
//H_CHECK(x-maxDX-WindowSize >=0 && x+WindowSizewidth );
int xli;
int sadMin=99999dispBest=0;
for(i=1;i {
xl=xr+i;
int sad=SAD(image1image2xlxryWindowSize);
if(sadMin >sad)
{
sadMin=sad;
dispBest=i;
}
}
return dispBest;
}
int main(int argcchar** argv)
{
IplImage *left*right*depthmap;
if(argc!=4)
{
printf(“Error! \n“);
printf(“usage mysad (left.bmp) (right.bmp) (result.bmp)“);
return -1;
}
double tt = (double)cvGetTickCount();
left=cvLoadImage(argv[1]CV_LOAD_IMAGE_GRAYSCALE);
right=cvLoadImage(argv[2]CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow(“left image“1);
cvNamedWindow(“right image“1);
cvShowImage(“left image“left);
cvShowImage(“right image“right);
depthmap=cvCreateImage(cvGetSize(left)IPL_DEPTH_8U1);
int W=left->widthH=left->height;
int windowSize=4;
int MaxDispX=20;
int xy;
int Scale=255/MaxDispX;
printf(“图像大小%d*%d最大视差%d窗口大小%d“WHMaxDispXwindowSize);
for (y=windowSize;y {
for (x=windowSize;x {
int disp=GetDisparitySAD(leftrightxywindowSizeMaxDispX);
//CV_IMAGE_ELEM(imageDisp.m_IplImageBYTEyx)=disp*Scale;
if(disp && abs(disp-GetDisparitySAD_Reverse(leftrightx-dispywindowSizeMaxDispX))<5)
((uchar*)(depthmap->imageData+y*depthmap->widthStep))[x]=disp*Scale;
//((uchar* )(DepthMap->imageData+DispMap[i].y*DepthMap->widthStep))[DispMap[i].x]=t2;
}
// TRACE(“y %d“y);
}
tt = (double)cvGetTickCount() - tt;
printf( “Extraction time = %gms\n“ tt/(cvGetTickFrequency()*1000.));
cvNamedWindow(“depth“1);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 111670 2009-05-04 23:09 mySad\Debug\9.bmp
文件 389120 2008-04-14 08:12 mySad\Debug\cmd.exe
文件 111670 2009-05-04 18:52 mySad\Debug\depth.bmp
文件 331830 2009-04-27 11:03 mySad\Debug\left.bmp
文件 2130944 2009-05-04 18:35 mySad\Debug\mySad.bsc
文件 188468 2009-05-05 10:39 mySad\Debug\mySad.exe
文件 205388 2009-05-05 10:39 mySad\Debug\mySad.ilk
文件 4136268 2009-05-04 18:40 mySad\Debug\mySad.pch
文件 451584 2009-05-05 10:39 mySad\Debug\mySad.pdb
文件 111670 2009-05-04 23:07 mySad\Debug\new.bmp
文件 111670 2009-05-05 15:31 mySad\Debug\res.bmp
文件 111670 2009-05-04 18:46 mySad\Debug\result.bmp
文件 331830 2009-04-27 11:04 mySad\Debug\right.bmp
文件 18954 2009-05-05 10:39 mySad\Debug\sad.obj
文件 1059043 2009-05-04 18:37 mySad\Debug\sad.sbr
文件 164864 2009-05-05 10:39 mySad\Debug\vc60.idb
文件 102400 2009-05-05 10:39 mySad\Debug\vc60.pdb
文件 4098 2009-05-04 18:53 mySad\mySad.dsp
文件 533 2009-05-04 18:08 mySad\mySad.dsw
文件 41984 2009-05-05 10:40 mySad\mySad.ncb
文件 48640 2009-05-05 10:40 mySad\mySad.opt
文件 1119 2009-05-05 10:39 mySad\mySad.plg
文件 3290 2009-05-04 23:12 mySad\sad.cpp
目录 0 2009-05-05 15:31 mySad\Debug
目录 0 2009-05-05 10:40 mySad
----------- --------- ---------- ----- ----
10168707 25
评论
共有 条评论