-
Notifications
You must be signed in to change notification settings - Fork 0
/
midterm_Q4_v1.m
72 lines (56 loc) · 2.58 KB
/
midterm_Q4_v1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% ENPM673 Midterm Q4 Generation of the Disparity Map using cross correlation
% Canberk Suat Gurel 115595972
clear all; close all; clc
% Constants
CCsize=20; % size of cross-correlation template
Hlimit = 50; % disparity horizontal search limit
Vlimit = 8; % disparity vertical search limit
% Read and display the stereo images
I1 = imread('tsukuba_l.png');
I2 = imread('tsukuba_r.png');
figure(1), imshow(I1, []), title('Left image');
figure(2), imshow(I2, []), title('Right image');
% Calculate disparity at a set of discrete points
Xsize = CCsize+Hlimit;
Ysize = CCsize+Vlimit;
Xborder = Xsize+1;
Yborder = Ysize+1;
NUMpts = 0; % Number of found disparity points
for X=Xborder:CCsize:size(I1,2)-Xborder
for Y=Yborder:CCsize:size(I1,1)-Yborder
% Extract a template from the left image centered at X, Y
figure(1), hold on, plot(X, Y, 'rd'),hold off;
T = imcrop(I1, [X-CCsize Y-CCsize 2*CCsize 2*CCsize]);
% Search for match in the right image, in a region centered at X, Y
% and of dimensions Vlimit wide by Hlimit high
Im_R = imcrop(I2, [X-Xsize Y-Ysize 2*Xsize 2*Ysize]);
% The correlation score image is the size of Im_R, expanded by CCsize
% in each direction
CCscores = normxcorr2(T,Im_R);
% Get the location of the peak in the correlation score image
[max_score, maxindex] = max(CCscores(:));
[Ypeak, Xpeak] = ind2sub(size(CCscores),maxindex);
% If score too low, ignore this point
if (max_score < 0.8)
continue;
end
% These are the coordinates of the peak in the search image
Xpeak = Xpeak - CCsize; Ypeak = Ypeak - CCsize;
% These are the coordinates in the full sized right image
Xpeak = Xpeak + (X-Xsize); Ypeak = Ypeak + (Y-Ysize);
figure(2), hold on, plot(Xpeak, Ypeak, 'rd'),hold off;
% Save the point in a list, along with its disparity (left-right)
NUMpts = NUMpts+1;
Xpt(NUMpts) = X; Ypt(NUMpts) = Y;
Dpt(NUMpts) = Xpeak-X; % Disparity = xright-xleft
end
end
% Display the result %
figure('units','normalized','outerposition',[0 0 1 1]);
scatter3(Xpt, Ypt, Dpt, 1000, Dpt, 'Marker','.');
grid on, grid minor; zlim([-15 0]);
title('Disparity Map of the Stereo Pair')
xlabel('X Axis'),ylabel('Y Axis'),zlabel('Disparity');
font=18; set(gca,'fontsize',font);
colormap(winter(5)); c = colorbar;
c.Label.String = 'Disparity';