SAD匹配产生致密的深度图
今晚突然想写一个matlab的,基于SAD的区域法立体匹配方法。
下面以并列出matlab和c++版本的代码及实验结果。
MATLAB版本:
有个地方36~42行应该还能优化,只是我没找到相应的函数,这个地方运行很慢
加入了crosscheck功能
下面以并列出matlab和c++版本的代码及实验结果。
MATLAB版本:
有个地方36~42行应该还能优化,只是我没找到相应的函数,这个地方运行很慢
1
%function SADMatch
2
clear,clc
3
tic
4
%im1=imread('Test_left.jpg');
5
%im2=imread('Test_right.jpg');
6
% im1=imread('im0.jpg');
7
% im2=imread('im1.jpg');
8
im1=imread('left.BMP');
9
im2=imread('right.BMP');
10![](/Images/OutliningIndicators/None.gif)
11![](/Images/OutliningIndicators/None.gif)
12
if isrgb(im1)
13
im1=rgb2gray(im1);
14
end
15
%imshow(im1);
16
im1=double(im1);
17![](/Images/OutliningIndicators/None.gif)
18
if isrgb(im2)
19
im2=rgb2gray(im2);
20
end
21
%figure
22
%imshow(im2);
23
im2=double(im2);
24![](/Images/OutliningIndicators/None.gif)
25
D=20; %最大视差
26
N=5; %窗口大小的一半
27
[H,W]=size(im1);
28![](/Images/OutliningIndicators/None.gif)
29
%计算右图减去左图,相减产生D个矩阵放到imgDiff中
30
imgDiff=zeros(H,W,D);
31
e=zeros(H,W);
32
for i=1:D
33
fprintf('%g\n',i)
34
e(:,1:(W-i))=abs(im2(:,1:(W-i))- im1(:,(i+1):W));
35
%e=conv2(e,e,'same');
36
e2=zeros(H,W);%计算窗口内的和
37
for y=(N+1):(H-N)
38
for x=(N+1):(W-N)
39
e2(y,x)=sum(sum(e((y-N):(y+N),(x-N):(x+N))));
40
end
41
end
42
imgDiff(:,:,i)=e2;
43
end
44![](/Images/OutliningIndicators/None.gif)
45
%找到最小的视差,到dispMap
46
dispMap=zeros(H,W);
47
for x=1:W
48
for y=1:H
49
%[val,id]=min(imgDiff(y,x,:));
50
[val,id]=sort(imgDiff(y,x,:));
51
if abs(val(1)-val(2))>10
52
dispMap(y,x)=id(1);
53
end
54
end
55
end
56
dispMap=dispMap*200/D;
57
dispMap=uint8(dispMap);
58
toc
59
imshow(dispMap)
60
%imwrite(dispMap,'dispMap.jpg')
c版本:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/None.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/None.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/None.gif)
52
![](/Images/OutliningIndicators/None.gif)
53
![](/Images/OutliningIndicators/None.gif)
54
![](/Images/OutliningIndicators/None.gif)
55
![](/Images/OutliningIndicators/None.gif)
56
![](/Images/OutliningIndicators/None.gif)
57
![](/Images/OutliningIndicators/None.gif)
58
![](/Images/OutliningIndicators/None.gif)
59
![](/Images/OutliningIndicators/None.gif)
60
![](/Images/OutliningIndicators/None.gif)
加入了crosscheck功能
1
/************************************************************************
2
功能:SAD匹配
3
输入:用图像image1的(x1,y12)和image2的(x2,y12)处建立大小为2*windowSize大小的窗口计算SAD误差并返回
4
图像必须为灰度
5
************************************************************************/
6
int SAD(IplImage *image1,IplImage*image2,int x1,int x2,int y12,int WindowSize)
7
{
8
//H_CHECK(CV_ARE_SIZES_EQ(image1,image2));
9
int W=image1->width,H=image1->height;
10
// H_CHECK(x1>=WindowSize && x1<W-WindowSize
11
// &&x2>=WindowSize && x2<W-WindowSize
12
// &&y12>=WindowSize && y12<H-WindowSize
13
// );
14![](/Images/OutliningIndicators/InBlock.gif)
15
int dx,dy;
16
int sum=0;
17
for (dy=-WindowSize;dy<=WindowSize;dy++)
18
{
19
LPBYTE p1=&CV_IMAGE_ELEM(image1,BYTE,y12+dy,x1-WindowSize);
20
LPBYTE p2=&CV_IMAGE_ELEM(image2,BYTE,y12+dy,x2-WindowSize);
21
for (dx=-WindowSize;dx<=WindowSize;dx++)
22
{
23
sum+=abs(*p1-*p2);
24
p1++;
25
p2++;
26
}
27
}
28![](/Images/OutliningIndicators/InBlock.gif)
29
return sum;
30
}
31
int GetDisparitySAD(IplImage *image1,IplImage*image2,int x,int y,int WindowSize,int maxDX)
32
{
33
//H_CHECK(x-maxDX-WindowSize >=0 && x+WindowSize<image1->width );
34
int x2,i;
35
int sadMin=99999,dispBest=0;
36
for(i=1;i<maxDX;i++)
37
{
38
x2=x-i;
39
int sad=SAD(image1,image2,x,x2,y,WindowSize);
40
if(sadMin >sad)
41
{
42
sadMin=sad;
43
dispBest=i;
44
}
45
}
46![](/Images/OutliningIndicators/InBlock.gif)
47
return dispBest;
48
}
49
//反相匹配
50
int GetDisparitySAD_Reverse(IplImage *image1,IplImage*image2,int xr,int y,int WindowSize,int maxDX)
51
{
52
//H_CHECK(x-maxDX-WindowSize >=0 && x+WindowSize<image1->width );
53
int xl,i;
54
int sadMin=99999,dispBest=0;
55
for(i=1;i<maxDX;i++)
56
{
57
xl=xr+i;
58
int sad=SAD(image1,image2,xl,xr,y,WindowSize);
59
if(sadMin >sad)
60
{
61
sadMin=sad;
62
dispBest=i;
63
}
64
}
65![](/Images/OutliningIndicators/InBlock.gif)
66
return dispBest;
67
}
68
void CDlgImagePanorama::OnBnClickedBnMatchSad()
69
{
70
// TODO: 在此添加控件通知处理程序代码
71
// HImage im1(5,5,1);
72
// HImage im2(5,5,1);
73
// cvSet(im2,cvScalar(1));
74
// int sum=SAD(im1,im2,2,2,2,2);
75
// TRACE("sum %d ",sum);
76
AfxMessageBox("选择左图");
77
CString fileNameL=GetFileName("imageL");
78
if(fileNameL.GetLength()==0)
79
return;
80
AfxMessageBox("选择右图");
81
CString fileNameR=GetFileName("imageR");
82
if(fileNameR.GetLength()==0)
83
return;
84
HImage im1(fileNameL,0);
85
HImage im2(fileNameR,0);
86
H_CHECK(im1.w()==im2.w() && im1.h()==im2.h() && im1.nChannels()==im2.nChannels() && im1.nChannels()==1);
87
88
if(AfxMessageBox("resample to width 320?",MB_YESNO)==IDYES)
89
{
90
int h=((double)im1.h())*320.0/im1.w();
91
HImage imgTmp(320,h,1);
92
cvResize(im1,imgTmp);
93
im1=imgTmp;
94![](/Images/OutliningIndicators/InBlock.gif)
95
cvResize(im2,imgTmp);
96
im2=imgTmp;
97
}
98![](/Images/OutliningIndicators/InBlock.gif)
99
int W=im1.w(),H=im1.h();
100
H_CHECK(W>0 && H>0);
101
OutputDebugString("SAD Start");
102
DWORD dwTime=GetTickCount();
103
int windowSize=GetDlgItemInt(IDC_EDIT_WINSIZE)/2;
104
ASSERT(windowSize>0);
105
int MaxDispX=W/6;
106
int x,y;
107
int Scale=255/MaxDispX;
108![](/Images/OutliningIndicators/InBlock.gif)
109
char s[1000];
110
sprintf(s,"图像大小%d*%d,搜索范围%d,窗口大小%d",W,H,MaxDispX,windowSize);
111
SetDlgItemText(IDC_MATCH_INFO,s);
112![](/Images/OutliningIndicators/InBlock.gif)
113
HImage imageDisp(W,H,1);
114
TRACE("MaxDispX %d",MaxDispX);
115
for (y=windowSize;y<H-windowSize;y++)
116
{
117
for (x=MaxDispX+windowSize;x<W-windowSize;x++)
118
{
119![](/Images/OutliningIndicators/InBlock.gif)
120
int disp=GetDisparitySAD(im1,im2,x,y,windowSize,MaxDispX);
121
//CV_IMAGE_ELEM(imageDisp.m_IplImage,BYTE,y,x)=disp*Scale;
122
if(disp && abs(disp-GetDisparitySAD_Reverse(im1,im2,x-disp,y,windowSize,MaxDispX))<5)
123
imageDisp(x,y)=disp*Scale;
124
}
125
TRACE("y %d",y);
126
}
127![](/Images/OutliningIndicators/InBlock.gif)
128![](/Images/OutliningIndicators/InBlock.gif)
129
TRACE("SAD End(%d ms)",GetTickCount()-dwTime);
130
imageDisp.Show("disp");
131
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
68
![](/Images/OutliningIndicators/None.gif)
69
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/InBlock.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)