-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.m
156 lines (133 loc) · 4.69 KB
/
demo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
close all
clear
warning("off", "all")
% % % ACTUAL DEMO FOR THE WHOLE PURPOSE OF THE CODE AKA PART 2
img1 = im2double(imread('imForest1.png'));
img2 = im2double(imread('imForest2.png'));
stitched = myStitch(img1, img2);
figure, imshow(stitched)
img1 = im2double(imread('im1.png'));
img2 = im2double(imread('im2.png'));
stitched = myStitch(img1, img2);
figure, imshow(stitched)
% % % FOR PART 1.1
% % find descriptor for point
p = [100, 100];
rhom = 5;
rhoM = 20;
rhostep = 1;
N = 8;
grayScale1 = rgb2gray(img1);
p1 = myLocalDescriptor(grayScale1, p, rhom, rhoM, rhostep, N);
p = [108, 198];
p2 = myLocalDescriptor(imrotate(grayScale1, -5), p, rhom, rhoM, rhostep, N);
q = [200, 200];
q1 = myLocalDescriptor(grayScale1, q, rhom, rhoM, rhostep, N);
q = [202, 202];
q2 = myLocalDescriptor(grayScale1, q, rhom, rhoM, rhostep, N);
% % not working well
% xy = [-1, -1, -1; -1, 8, -1; -1, -1, -1];
% lapl = imfilter(grayScale1, xy);
% lapl2 = imfilter(imrotate(grayScale1, -5), xy);
% p = [1072, 689];
% p1 = myLocalDescriptorUpgrade(lapl, p, rhom, rhoM, rhostep, N);
% p = [1128, 708];
% p2 = myLocalDescriptorUpgrade(lapl2, p, rhom, rhoM, rhostep, N);
% q = [200, 200];
% q1 = myLocalDescriptorUpgrade(lapl, q, rhom, rhoM, rhostep, N);
% q = [202, 202];
% q2 = myLocalDescriptorUpgrade(lapl, q, rhom, rhoM, rhostep, N);
% % HARRIS CORNER DETECTOR PART 1.2
% corner detection
corners1 = myDetectHarrisFeatures(grayScale1);
cornersMatlab = detectHarrisFeatures(grayScale1);
% figures
figure
imshow(img1)
hold on
scatter(corners1(:, 1), corners1(:, 2), ...
'Marker', '+', 'MarkerEdgeColor', 'red');
figure
imshow(img1)
hold on
scatter(cornersMatlab.Location(:, 1), ...
cornersMatlab.Location(:, 2), 'Marker', '+', 'MarkerEdgeColor', 'red');
grayScale2 = rgb2gray(img2);
corners2 = myDetectHarrisFeatures(grayScale2);
figure
imshow(img2)
hold on
scatter(corners2(:, 1), corners2(:, 2), ...
'Marker', '+', 'MarkerEdgeColor', 'red');
% % % DESCRIPTOR MATCHING AND RANSAC
% % % PART 1.3
% descriptors
N = 30;
descSize = (rhoM - rhom + 1) / rhostep;
cornersNum = size(corners1, 1);
descriptors1 = zeros(cornersNum, descSize);
for i = 1 : cornersNum
descriptors1(i, :) = myLocalDescriptor(grayScale1, ...
corners1(i, :), rhom, rhoM, rhostep, N);
end
% % remove corner points with zero descriptors
% % i.e. too close to the edges of the image
nonZeroIndices = any(descriptors1, 2);
descriptors1 = descriptors1(nonZeroIndices, :);
corners1 = corners1(nonZeroIndices, :);
% % get descriptors of corners for image 2
cornersNum = size(corners2, 1);
descriptors2 = zeros(cornersNum, descSize);
for i = 1 : cornersNum
descriptors2(i, :) = myLocalDescriptor(grayScale2, corners2(i, :), ...
rhom, rhoM, rhostep, N);
end
% % remove corner points with zero descriptors
nonZeroIndices = any(descriptors2, 2);
descriptors2 = descriptors2(nonZeroIndices, :);
corners2 = corners2(nonZeroIndices, :);
% % match descriptors between the two images
matching = descriptorMatching(descriptors1, descriptors2, 0.1);
matchingPoints = [corners1(matching(:, 1), :), ...
corners2(matching(:, 2), :)];
% % find transformation from matched points
[H, inliers, outliers] = myRANSAC(matchingPoints, 10, ...
100 * length(matchingPoints));
% make a merged image to display results
[height1, width1, ~] = size(img1);
[height2, width2, ~] = size(img2);
maxHeight = max(height1, height2);
mergedWidth = width1 + width2;
mergedImage = zeros(maxHeight, mergedWidth, 3, 'like', img1);
mergedImage(1:height1, 1:width1, :) = img1;
mergedImage(1:height2, width1+1:end, :) = img2;
figure, imshow(mergedImage)
hold on
for i = 1 : length(outliers)
index = outliers(i);
corner1 = matchingPoints(index, 1:2);
corner2 = matchingPoints(index, 3:4);
plot(corner1(2), corner1(1), 's', ...
'MarkerFaceColor', 'none', ...
'MarkerEdgeColor', [.5 .5 .5], 'MarkerSize', 10);
plot(corner2(2) + width1, corner2(1), 's', ...
'MarkerFaceColor', 'none', ...
'MarkerEdgeColor', [.5 .5 .5], 'MarkerSize', 10);
end
hold off
figure, imshow(mergedImage)
hold on
for i = 1 : length(inliers)
index = inliers(i);
corner1 = matchingPoints(index, 1:2);
corner2 = matchingPoints(index, 3:4);
ran = [rand rand rand];
plot(corner1(2), corner1(1), 'o', ...
'MarkerFaceColor', 'none', 'MarkerEdgeColor', ...
ran, 'MarkerSize', 10);
plot(corner2(2) + width1, corner2(1), 'o', ...
'MarkerFaceColor', 'none', 'MarkerEdgeColor', ...
ran, 'MarkerSize', 10);
plot([corner1(2) corner2(2) + width1], [corner1(1) corner2(1)], ...
'color', ran);
end