-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.m
398 lines (324 loc) · 17.2 KB
/
test.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
%% CS413 Coursework Assignment 2017 - Object Matching in Videos
%
% Most of the code used in this project is a combination of examples provided in the
% laboratory as well as my own understanding of the material taught. Some
% bits of the code is attributed to the multiple online MATLAB(R) tutorials
% on object detection available on their website. [https://uk.mathworks.com/help/vision/examples/object-detection-in-a-cluttered-scene-using-point-feature-matching.html]
clear all
clc
warning off;
im1 = (imread('objects-1.jpeg'));
im2 = (imread('objects-2.jpeg'));
im3 = (imread('objects-3.jpeg'));
v1 = VideoReader('objects-test-1.mov');
v2 = VideoReader('objects-test-2.mov');
v3 = VideoReader('objects-test-3.mov');
v4 = VideoReader('objects-test-4.mov');
%% Video Properties
vid1width = v1.Width;
vid1height = v1.Height;
vid1_frames = v1.NumberOfFrames;
vid2width = v2.Width;
vid2height = v2.Height;
vid2_frames = v2.NumberOfFrames;
vid3width = v3.Width;
vid3height = v3.Height;
vid3_frames = v3.NumberOfFrames;
vid4width = v4.Width;
vid4height = v4.Height;
vid4_frames = v4.NumberOfFrames;
%% SURF Feature Extraction, Matching and Object Detection for Video 1 -> objects-test-1.mov
points1 = detectSURFFeatures(rgb2gray(im1),'MetricThreshold',800);
points2 = detectSURFFeatures(rgb2gray(im2),'MetricThreshold',800);
[fp1, vp1] = extractFeatures(rgb2gray(im1), points1, 'SURFSize', 64); %SURF Feature Descriptors
[fp2, vp2] = extractFeatures(rgb2gray(im2), points2, 'SURFSize', 64); %SURF Feature Descriptors
figure('Position',[720, 720, 720, 1080]);
status1 = 0;
flag1 = 0;
check1 = 0;
for n = 1: ((vid1_frames / 4) -4)
thisFrame = imresize(read(v1,4*n),1.5);
points_v1 = detectSURFFeatures(rgb2gray(thisFrame),'MetricThreshold',1000);
[fpv1, vpv1] = extractFeatures(rgb2gray(thisFrame), points_v1, 'SURFSize', 64);
pointspairs1 = matchFeatures(fp1,fpv1,'Unique',true); %Matching Features between the first training image and the video frame
matchedPointsPairs1 = vp1(pointspairs1(:,1),:);
matchedScenePoints1 = vpv1(pointspairs1(:,2),:);
pointspairs2 = matchFeatures(fp2,fpv1,'Unique',true); %Matching Features between the second training image and the video frame
matchedPointsPairs2 = vp2(pointspairs2(:,1),:);
matchedScenePoints2 = vpv1(pointspairs2(:,2),:);
if length(pointspairs2) < length(pointspairs1) %Metric used to verify the similarity of a video frame to its corresponding training image
if (size(pointspairs1,1) >=10) %Used to remove outliers apart from the methods implemented below
[tform1, inlierPointsPairs1, inlierScenePoints1,status1] = estimateGeometricTransform(matchedPointsPairs1, matchedScenePoints1, 'projective');
if (inlierPointsPairs1.Count <=5)
flag1 = -1;
subplot(2,1,2)
imshow(thisFrame);
else
flag1 = 1;
check1 = 1;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs1)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%'', Thresh +/- = 1000, Octaves = 3'];
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white'); %Labeling the recognised Object
corners = [0,0;960,0;960,720;0,720];
imshow(text); %Locating the recognised object
new_corners = transformPointsForward(tform1, corners);
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag1 = -1;
subplot(2,1,2)
imshow(thisFrame);
end
else
if (size(pointspairs2,1) >=10) %Used to remove outliers apart from the methods implemented below
[tform2, inlierPointsPairs1, inlierScenePoints1,status1] = estimateGeometricTransform(matchedPointsPairs2, matchedScenePoints2, 'projective');
if (inlierPointsPairs1.Count <=5)
flag1 = -2;
subplot(2,1,2)
imshow(thisFrame);
else
flag1 = 2;
check1 = 2;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs2)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%'', Thresh +/- = 1000, Octaves = 3'];
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white'); %Labeling the recognised Object
corners = [0,0;960,0;960,720;0,720];
new_corners = transformPointsForward(tform2, corners);
imshow(text); %Locating the recognised object
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag1 = -2;
subplot(2,1,2)
imshow(thisFrame);
end
end
if flag1 == 1
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage'); %Shows the matching features montage on the same figure as the detection method
%
elseif flag1 == 2
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage');
%
elseif (flag1 == -1 && check1 ~=2 )
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, [nan nan], [nan nan], 'montage'); %Method used to neglect false positive inlier points
%
elseif (flag1 == -2 && check1 ~=1)
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, [nan nan], [nan nan], 'montage');
%
elseif (flag1 == -2 && check1 ==1)
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, [nan nan], [nan nan], 'montage');
%
elseif (flag1 == -1 && check1 ==2)
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, [nan nan], [nan nan], 'montage');
%
end
end
%% SURF Feature Extraction, Matching and Object Detection for Video 2 (Cluttered) -> objects-test-2.mov
clear pointspairs1 matchedPointsPairs1 matchedScenePoints1 pointspairs2 matchedPointsPairs2 matchedScenePoints2 percentage
clear tform1 inlierPointsPairs1 inlierScenePoints1 tform2 inlierPointsPairs1 inlierScenePoints1
clear points1 points2 fp1 vp1 fp2 vp2
points1 = detectSURFFeatures(rgb2gray(im1),'MetricThreshold',1000);
points2 = detectSURFFeatures(rgb2gray(im2),'MetricThreshold',1000);
[fp1, vp1] = extractFeatures(rgb2gray(im1), points1, 'SURFSize', 64);
[fp2, vp2] = extractFeatures(rgb2gray(im2), points2, 'Method', 'SURF');
figure('Position',[720, 720, 720, 1080]);
status2 = 0;
flag2 = 0;
check2 = 0;
for n = 1: ((vid2_frames / 4) -4)
thisFrame = imresize(read(v2,4*n),1.5);
points_v2 = detectSURFFeatures(rgb2gray(thisFrame),'MetricThreshold',800);
[fpv2, vpv2] = extractFeatures(rgb2gray(thisFrame), points_v2, 'Method', 'SURF');
pointspairs1 = matchFeatures(fp1,fpv2,'Unique',true); %Matching Features between the first training image and the video frame
matchedPointsPairs1 = vp1(pointspairs1(:,1),:);
matchedScenePoints1 = vpv2(pointspairs1(:,2),:);
pointspairs2 = matchFeatures(fp2,fpv2,'Unique',true); %Matching Features between the second training image and the video frame
matchedPointsPairs2 = vp2(pointspairs2(:,1),:);
matchedScenePoints2 = vpv2(pointspairs2(:,2),:);
if length(pointspairs2) < length(pointspairs1)
if (size(pointspairs1,1) >15)
[tform1, inlierPointsPairs1, inlierScenePoints1,status2] = estimateGeometricTransform(matchedPointsPairs1, matchedScenePoints1, 'affine');
if (inlierPointsPairs1.Count <5)
flag2 = -1;
subplot(2,1,2)
imshow(thisFrame);
else
flag2 = 1;
check2 = 1;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs1)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%'', Thresh +/- = 800, Octaves = 3']; %Labeling the recognised Object
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white');
corners = [0,0;960,0;960,720;0,720];
new_corners = transformPointsForward(tform1, corners);
imshow(text); %Locating the recognised Object
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag2 = -1;
subplot(2,1,2)
imshow(thisFrame);
end
else
if (size(pointspairs2,1) >15)
[tform2, inlierPointsPairs1, inlierScenePoints1,status2] = estimateGeometricTransform(matchedPointsPairs2, matchedScenePoints2, 'affine');
if (inlierPointsPairs1.Count <5)
flag2 = -2;
subplot(2,1,2)
imshow(thisFrame);
else
flag2 = 2;
check2 = 2;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs2)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%'', Thresh +/- = 800, Octaves = 3']; %Labeling the recognised Object
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white');
corners = [0,0;960,0;960,720;0,720];
new_corners = transformPointsForward(tform2, corners);
imshow(text); %Locating the recognised Object
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag2 = -2;
subplot(2,1,2)
imshow(thisFrame);
end
end
if flag2 == 1
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage'); %Shows the matching features montage on the same figure as the detection method
elseif flag2 == 2
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage');
elseif (flag2 == -1 && check2 ~=2 )
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, [nan nan], [nan nan], 'montage'); %Method used to neglect false positive inlier points
elseif (flag2 == -2 && check2 ~=1)
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, [nan nan], [nan nan], 'montage');
elseif (flag2 == -2 && check2 ==1)
subplot(2,1,1)
showMatchedFeatures(im1, thisFrame, [nan nan], [nan nan], 'montage');
elseif (flag2 == -1 && check2 ==2)
subplot(2,1,1)
showMatchedFeatures(im2, thisFrame, [nan nan], [nan nan], 'montage');
end
end
%% SURF Feature Extraction, Matching and Object Detection for Video 3 -> objects-test-3.mov
clear pointspairs1 matchedPointsPairs1 matchedScenePoints1 pointspairs2 matchedPointsPairs2 matchedScenePoints2 percentage
clear tform1 inlierPointsPairs1 inlierScenePoints1 tform2 inlierPointsPairs1 inlierScenePoints1
clear points1 points2 fp1 vp1 fp2 vp2
points1 = detectSURFFeatures(rgb2gray(im3),'MetricThreshold',100);
[fp1, vp1] = extractFeatures(rgb2gray(im3), points1, 'SURFSize', 64);
figure('Position',[720, 720, 720, 1080]);
status3 = 0;
flag3 = 0;
for n = 1: ((vid3_frames / 4) -4)
thisFrame = imresize(read(v3,4*n),1.0);
points_v2 = detectSURFFeatures(rgb2gray(thisFrame),'MetricThreshold',500);
[fpv2, vpv2] = extractFeatures(rgb2gray(thisFrame), points_v2, 'Method', 'SURF'); %Matching Features between the third training image and the video frame
pointspairs1 = matchFeatures(fp1,fpv2,'Unique',true);
matchedPointsPairs1 = vp1(pointspairs1(:,1),:);
matchedScenePoints1 = vpv2(pointspairs1(:,2),:);
if (size(pointspairs1,1) >=10)
[tform1, inlierPointsPairs1, inlierScenePoints1,status3] = estimateGeometricTransform(matchedPointsPairs1, matchedScenePoints1, 'affine');
if (inlierPointsPairs1.Count <5)
flag3 = -1;
subplot(2,1,2)
imshow(thisFrame);
else
flag3 = 1;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs1)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%'', Thresh +/- = 100, Octaves = 3']; %Labeling the recognised Object
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white');
corners = [0,0;960,0;960,720;0,720];
new_corners = transformPointsForward(tform1, corners);
imshow(text); %Locating the recognised Object
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised Object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag3 = -1;
subplot(2,1,2)
imshow(thisFrame);
end
if flag3 == 1
subplot(2,1,1)
showMatchedFeatures(im3, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage');
elseif (flag3 == -1)
subplot(2,1,1)
showMatchedFeatures(im3, thisFrame, [nan nan], [nan nan], 'montage'); %Method used to neglect false positive inlier points
end
end
%% SURF Feature Extraction, Matching and Object Detection for Video 4 (Cluttered) -> objects-test-4.mov
clear pointspairs1 matchedPointsPairs1 matchedScenePoints1 pointspairs2 matchedPointsPairs2 matchedScenePoints2 percentage
clear tform1 inlierPointsPairs1 inlierScenePoints1 tform2 inlierPointsPairs1 inlierScenePoints1
clear points1 points2 fp1 vp1 fp2 vp2
points1 = detectSURFFeatures(rgb2gray(im3),'MetricThreshold',100);
[fp1, vp1] = extractFeatures(rgb2gray(im3), points1, 'SURFSize', 64);
figure('Position',[720, 720, 720, 1080]);
status4 = 0;
flag4 = 0;
for n = 1: ((vid4_frames / 4) -4)
thisFrame = imresize(read(v4,4*n),1.0);
points_v2 = detectSURFFeatures(rgb2gray(thisFrame),'MetricThreshold',500);
[fpv2, vpv2] = extractFeatures(rgb2gray(thisFrame), points_v2, 'Method', 'SURF'); %Matching Features between the third training image and the video frame
pointspairs1 = matchFeatures(fp1,fpv2,'Unique',true);
matchedPointsPairs1 = vp1(pointspairs1(:,1),:);
matchedScenePoints1 = vpv2(pointspairs1(:,2),:);
if (size(pointspairs1,1) >=10)
[tform1, inlierPointsPairs1, inlierScenePoints1,status4] = estimateGeometricTransform(matchedPointsPairs1, matchedScenePoints1, 'affine');
if (inlierPointsPairs1.Count <5)
flag4 = -1;
subplot(2,1,2)
imshow(thisFrame);
else
flag4 = 1;
subplot(2,1,2)
percentage = (length(inlierPointsPairs1) / length(pointspairs1)) * 100;
text_input = ['Confidence: ' num2str(percentage,'%0.2f') '%' ', Thresh +/- = 100, Octaves = 3']; %Labeling the recognised Object
text = insertText(thisFrame,[230 680],text_input,'FontSize',22,'BoxColor','blue','BoxOpacity',0.4,'TextColor','white');
corners = [0,0;960,0;960,720;0,720];
new_corners = transformPointsForward(tform1, corners);
imshow(text); %Locating the recognised Object
hold on;
patch(new_corners(:,1),new_corners(:,2),[0 1 0],'FaceAlpha',0.5); %Drawing a box around the recognised Object
title('Object Recognised','interpreter','latex','fontsize',16);
hold off;
end
else
flag4 = -1;
subplot(2,1,2)
imshow(thisFrame);
end
if flag4 == 1
subplot(2,1,1)
showMatchedFeatures(im3, thisFrame, inlierPointsPairs1, inlierScenePoints1, 'montage');
elseif (flag4 == -1)
subplot(2,1,1)
showMatchedFeatures(im3, thisFrame, [nan nan], [nan nan], 'montage'); %Method used to neglect false positive inlier points
end
end