-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneral_script.m
More file actions
310 lines (257 loc) · 8.62 KB
/
Copy pathgeneral_script.m
File metadata and controls
310 lines (257 loc) · 8.62 KB
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
%% Problem1script
imname = 'test1.jpg';
imageData = im2double(rgb2gray(imread(imname)));
ohist = hog(imageData);
for k=1:9
figure
imshow(ohist(:,:,k));
end
%% Problem2script
% load a training example image
Itrain = im2double(rgb2gray(imread('pedSignTrain.jpg')));
% Itrain = im2double(imread('pedSignTemplate.jpg'));
%Itrain = im2double(rgb2gray(imread('pedSign3Train.jpg')));
% Itrain = im2double(rgb2gray(imread('test0.jpg')));
% nclick = 1;
% figure(1); clf;
% imshow(Itrain);
% [x,y] = ginput(nclick); %get nclicks from the user
%
% %compute 8x8 block in which the user clicked
% blockx = round(x/8);
% blocky = round(y/8);
%
% % COMPUTE the template from user click:
% % the following code assumes the template is 128x128 pixels
% % (16x16 hog blocks) so you will want to eventually modify
% % it to handle different sized templates.
% %
%
% %visualize image patches that the user clicked on
% figure(2); clf;
% for i = 1:nclick
% patch = Itrain(8*blocky(i)+(-63:64),8*blockx(i)+(-63:64));
% figure(2); subplot(3,2,i); imshow(patch);
% end
% compute the hog features
template = hog(Itrain);
figure; imshow(Itrain)
figure; clf;
for i=1:size(template,3)
subplot(3,3,i); imagesc(template(:,:,i))
end
%
% load a test image
%
Itest= im2double(rgb2gray(imread('test3.jpg')));
% find top 5 detections in Itest
ndet = 5;
[x,y,score] = detect(Itest,template,ndet);
%display top ndet detections
figure(3); clf; imshow(Itest);
for i = 1:ndet
% draw a rectangle. use color to encode confidence of detection
% top scoring are green, fading to red
hold on;
h = rectangle('Position',[x(i)-64 y(i)-64 128 128],'EdgeColor',[(i/ndet) ((ndet-i)/ndet) 0],'LineWidth',3,'Curvature',[0.3 0.3]);
hold off;
end
%% Problem 3 Script
Itrain = im2double(rgb2gray(imread('test1.jpg')));
template = hog(Itrain);
Itest = im2double(rgb2gray(imread('test0.jpg')));
currentTest= Itest;
resizeFactor = 0.7;
properTest = size(Itrain) < size(currentTest)
% properTest = size(Itrain) > size(currentTest);
blockWidth = 128;
level = 0;
xVals = []; yVals = []; scoreVals = [];
levelVals = [];
while(properTest(1) && properTest(2))
% find top 5 detections in Itest
ndet = 5;
[x,y,score] = detect(currentTest,template,ndet);
newX = x./(resizeFactor^level);
newY = y./(resizeFactor^level);
xVals = [xVals newX];
yVals = [yVals newY];
scoreVals = [scoreVals score];
levelVal = (x.*0) + level;
levelVals = [levelVals levelVal];
%display top ndet detections
%figure(3); clf;
figure
imshow(currentTest);
for i = 1:ndet
% draw a rectangle. use color to encode confidence of detection
% top scoring are green, fading to red
hold on;
h = rectangle('Position',[x(i)-(blockWidth/2) y(i)-(blockWidth/2) blockWidth blockWidth],...
'EdgeColor',[(i/ndet) ((ndet-i)/ndet) 0],...
'LineWidth',3,'Curvature',[0.3 0.3]);
hold off;
end
blockWidth = blockWidth*resizeFactor;
currentTest = imresize(currentTest,resizeFactor);
properTest = size(Itrain)<size(currentTest);
level = level + 1;
end
[val ind] = sort(score,'descend');
xFinal = xVals(ind);
yFinal = yVals(ind);
levelFinal = levelVals(ind);
%display top ndet detections
figure
imshow(Itest);
blockWidth = 128;
for i = 1:ndet
blockWidth = 128*(resizeFactor^levelFinal(i));
% draw a rectangle. use color to encode confidence of detection
% top scoring are green, fading to red
hold on;
h = rectangle('Position',[xFinal(i)-(blockWidth/2) yFinal(i)-(blockWidth/2) blockWidth blockWidth],...
'EdgeColor',[(i/ndet) ((ndet-i)/ndet) 0],...
'LineWidth',3,'Curvature',[0.3 0.3]);
hold off;
end
%% Problem 4 Script Picking Images
% this is the script used when you want
% to pick the images. The script used
% for the final result is the next one.
% load a training example image
Itrain = im2double(rgb2gray(imread('test3.jpg')));
%have the user click on some training examples.
% If there is more than 1 example in the training image (e.g. faces),
% you could set nclicks higher here and average together
figure(1); clf;
imshow(Itrain);
numRects = 8;
numPosRects = 3; %number of initial rectangles which will be used for positive template
patches = cell(1,numRects);
widthValues = zeros(1,numRects);
heightValues = zeros(1,numRects);
for num = 1:numRects
rect = getrect(figure(1));
xmin = floor(rect(1));
ymin = floor(rect(2));
width = floor(rect(3));
height = floor(rect(4));
patch = Itrain(ymin:(ymin+height),xmin:(xmin+width));
widthValues(num) = width;
heightValues(num) = height;
patches{num} = patch;
end
aspectRatioValues = widthValues./heightValues;
resizeHeight = mean(heightValues);
avgAspectRatio = mean(aspectRatioValues);
resizeWidth = resizeHeight*avgAspectRatio;
numHeightBlocks = floor(resizeHeight/8);
numWidthBlocks = floor(resizeWidth/8);
resizeHeight = numHeightBlocks*8;
resizeWidth = numWidthBlocks*8;
resizedPatches = zeros(resizeHeight,resizeWidth,numRects);
for num = 1:numRects
figure
resizedPatch = imresize(patches{num},[resizeHeight resizeWidth]);
resizedPatches(:,:,num) = resizedPatch;
imshow(resizedPatch)
end
% compute the hog features
%f = hog(Itrain);
negTemplate = zeros(numHeightBlocks,numWidthBlocks,9);
posTemplate = zeros(numHeightBlocks,numWidthBlocks,9);
for i = 1:numPosRects
f = hog(resizedPatches(:,:,i));
posTemplate = posTemplate + f;
end
for i = numPosRects+1:numRects
f = hog(resizedPatches(:,:,i));
negTemplate = negTemplate + f;
end
posTemplate = posTemplate/numPosRects;
negTemplate = negTemplate/(numRects-numPosRects);
template = posTemplate-negTemplate;
%
% load a test image
%
Itest= im2double(rgb2gray(imread('test4.jpg')));
% find top 5 detections in Itest
ndet = 5;
[x,y,score] = detect(Itest,template,ndet);
%display top ndet detections
figure(3); clf; imshow(Itest);
for i = 1:ndet
% draw a rectangle. use color to encode confidence of detection
% top scoring are green, fading to red
hold on;
h = rectangle('Position',[x(i)-64 y(i)-64 128 128],'EdgeColor',[(i/ndet) ((ndet-i)/ndet) 0],'LineWidth',3,'Curvature',[0.3 0.3]);
hold off;
end
%% Script for getting negative images
% This gets random image patches from the test images, which were
% later checked to make sure they don't contain the pedestrian sign
% Used to gather images for problem 4
Itrain = im2double(rgb2gray(imread('test0.jpg')));
imsize = size(Itrain);
height = imsize(1)-168;
width = imsize(2)-168;
numPics = 100;
randXvals = floor(rand(1,numPics)*width) + 84;
randYvals = floor(rand(1,numPics)*height) + 84;
for picNum = 1:numPics
imname = strcat('prob4negTrain/negPatchX_',num2str(picNum),'.jpg');
patch = Itrain(randYvals(picNum)-84:randYvals(picNum)+84,...
randXvals(picNum)-84:randXvals(picNum)+84);
imwrite(patch,imname,'JPEG');
end
%% Problem 4 Script Actual Run
% load a training example image
resizeHeight = 168;
resizeWidth = 168;
numHeightBlocks = floor(resizeHeight/8);
numWidthBlocks = floor(resizeWidth/8);
numPosRects = 5;
posPatches = zeros(resizeHeight,resizeWidth,numPosRects);
posPatches(:,:,1) = im2double(rgb2gray(imread('prob4posTrain/pedSign2Train.jpg')));
posPatches(:,:,2) = im2double(rgb2gray(imread('prob4posTrain/test3train.jpg')));
posPatches(:,:,3) = im2double(rgb2gray(imread('prob4posTrain/test4train.jpg')));
posPatches(:,:,4) = im2double(rgb2gray(imread('prob4posTrain/test5train.jpg')));
posPatches(:,:,5) = im2double(rgb2gray(imread('prob4posTrain/test6train.jpg')));
posTemplate = zeros(numHeightBlocks,numWidthBlocks,9);
for i = 1:numPosRects
f = hog(posPatches(:,:,i));
posTemplate = posTemplate + f;
end
posTemplate = posTemplate/numPosRects;
negTemplate = zeros(numHeightBlocks,numWidthBlocks,9);
%comment out this block to get one with just 5 positive templates
files = dir('prob4negTrain');
filesSize = size(files);
numFiles = filesSize(1);
images = zeros(168,168,100);
for index = 3:numFiles
imname = strcat('prob4negTrain/',files(index).name);
image = im2double(imread(imname));
imageToUse = image(1:168,1:168);
f = hog(imageToUse);
negTemplate = negTemplate+f;
end
negTemplate = negTemplate/(numFiles-2);
template = posTemplate-negTemplate;
%
% load a test image
%
Itest= im2double(rgb2gray(imread('test6.jpg')));
% find top 5 detections in Itest
ndet = 5;
[x,y,score] = detect(Itest,template,ndet);
%display top ndet detections
figure(3); clf; imshow(Itest);
for i = 1:ndet
% draw a rectangle. use color to encode confidence of detection
% top scoring are green, fading to red
hold on;
h = rectangle('Position',[x(i)-64 y(i)-64 128 128],'EdgeColor',[(i/ndet) ((ndet-i)/ndet) 0],'LineWidth',3,'Curvature',[0.3 0.3]);
hold off;
end