-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrafficSignDetection.m
203 lines (161 loc) · 8.23 KB
/
TrafficSignDetection.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
%
% Template example for using on the validation set.
%
function TrafficSignDetection(directory, pixel_method, window_method, decision_method,template_method)
addpath(genpath('.'));
% call with -> TrafficSignDetection('datasets/validationset', 'hsv-morph_op2','template_matching','')
% TrafficSignDetection('datasets/validationset', 'hsv-morph_op2','template_matching','')
% TrafficSignDetection
% Perform detection of Traffic signs on images. Detection is performed first at the pixel level
% using a color segmentation. Then, using the color segmentation as a basis, the most likely window
% candidates to contain a traffic sign are selected using basic features (form factor, filling factor).
% Finally, a decision is taken on these windows using geometric heuristics (Hough) or template matching.
%
% Parameter name Value
% -------------- -----
% 'directory' directory where the test images to analize (.jpg) reside
% 'pixel_method' Name of the color space: 'opp', 'normrgb', 'lab', 'hsv', etc. (Weeks 2-5)
% 'window_method' 'SegmentationCCL' or 'SlidingWindow' (Weeks 3-5)
% 'decision_method' 'GeometricHeuristics' or 'TemplateMatching' (Weeks 4-5)
global CANONICAL_W; CANONICAL_W = 64;
global CANONICAL_H; CANONICAL_H = 64;
global SW_STRIDEX; SW_STRIDEX = 8;
global SW_STRIDEY; SW_STRIDEY = 8;
global SW_CANONICALW; SW_CANONICALW = 32;
global SW_ASPECTRATIO; SW_ASPECTRATIO = 1;
global SW_MINS; SW_MINS = 1;
global SW_MAXS; SW_MAXS = 2.5;
global SW_STRIDES; SW_STRIDES = 1.2;
% Load models
%global circleTemplate;
%global givewayTemplate;
%global stopTemplate;
%global rectangleTemplate;
%global triangleTemplate;
%
%if strcmp(decision_method, 'TemplateMatching')
% circleTemplate = load('TemplateCircles.mat');
% givewayTemplate = load('TemplateGiveways.mat');
% stopTemplate = load('TemplateStops.mat');
% rectangleTemplate = load('TemplateRectangles.mat');
% triangleTemplate = load('TemplateTriangles.mat');
%end
results_directory=strcat(directory,'/results');
if (7==exist(results_directory,'dir'))
rmdir(results_directory, 's');
end
status = mkdir(results_directory);
if~status
error('results_directory creation');
end
windowTP=0; windowFN=0; windowFP=0; % (Needed after Week 3)
pixelTP=0; pixelFN=0; pixelFP=0; pixelTN=0;
pixelPrecision=0; pixelAccuracy=0; pixelRecall=0; pixelF1Score=0;
pixelTP=0; pixelFP=0; pixelFN=0; pixelCandidates = [];
datasetAnalysis = DatasetAnalysis('datasets/trainingset');
files = ListFiles(directory);
for i=1:size(files,1)
%for i=1:30
tic;
disp(sprintf('image%d',i));
% Read file
im = imread(strcat(directory,'/',files(i).name));
% Candidate Generation (pixel) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(window_method ~= "template_matching" && window_method ~= "hough")
pixelCandidates = CandidateGenerationPixel_Color(im, pixel_method);
% Accumulate pixel performance of the current image %%%%%%%%%%%%%%%%%
pixelAnnotation = imread(strcat(directory, '/mask/mask.', files(i).name(1:size(files(i).name,2)-3), 'png'))>0;
[localPixelTP, localPixelFP, localPixelFN, localPixelTN] = PerformanceAccumulationPixel(pixelCandidates, pixelAnnotation);
pixelTP = pixelTP + localPixelTP;
pixelFP = pixelFP + localPixelFP;
pixelFN = pixelFN + localPixelFN;
pixelTN = pixelTN + localPixelTN;
% display(pixelTP)
% display(pixelFP)
% display(pixelFN)
% display(pixelTN)
% Plot performance evaluation
[pixelPrecision, pixelAccuracy, pixelSpecificity, pixelRecall] = PerformanceEvaluationPixel(pixelTP, pixelFP, pixelFN, pixelTN);
pixelF1Score = F1Score(pixelPrecision, pixelRecall);
end
%SaveMask(pixelCandidates, results_directory, files(i).name(1:size(files(i).name,2)-3));
% Candidate Generation (window)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
windowCandidates = CandidateGenerationWindow(pixelCandidates, window_method, datasetAnalysis, im); %% (Needed after Week 3)
% Accumulate object performance of the current image %%%%%%%%%%%%%%%% (Needed after Week 3)
windowAnnotations = LoadAnnotations(strcat(directory, '/gt/gt.', files(i).name(1:size(files(i).name,2)-3), 'txt'));
[localWindowTP, localWindowFN, localWindowFP] = PerformanceAccumulationWindow(windowCandidates, windowAnnotations);
windowTP = windowTP + localWindowTP;
windowFN = windowFN + localWindowFN;
windowFP = windowFP + localWindowFP;
toc;
end
[windowPrecision, windowRecall, windowAccuracy] = PerformanceEvaluationWindow(windowTP, windowFN, windowFP); % (Needed after Week 3)
windowF1Score = F1Score(windowPrecision, windowRecall);
disp('----------------- PIXEL ------------------');
disp(strcat('Precision :', num2str(pixelPrecision)));
disp(strcat('Accuracy :', num2str(pixelAccuracy)));
disp(strcat('Recall :', num2str(pixelRecall)));
disp(strcat('F1 Score :', num2str(pixelF1Score)));
disp(strcat('TP :', num2str(pixelTP)));
disp(strcat('FP :', num2str(pixelFP)));
disp(strcat('FN :', num2str(pixelFN)));
disp('----------------- WINDOW ------------------');
disp(strcat('Precision :', num2str(windowPrecision)));
disp(strcat('Accuracy :', num2str(windowAccuracy)));
disp(strcat('Recall :', num2str(windowRecall)));
disp(strcat('F1 Score :', num2str(windowF1Score)));
disp(strcat('TP :', num2str(windowTP)));
disp(strcat('FP :', num2str(windowFP)));
disp(strcat('FN :', num2str(windowFN)));
% [windowPrecision, windowAccuracy]
% profile report
%profile off
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CandidateGeneration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function SaveMask(mask, directory, name)
uint8(mask);
imwrite(mask, strcat(directory, name,'.png'));
end
function [windowCandidates] = CandidateGenerationWindow_Example(im, pixelCandidates, window_method)
windowCandidates = [ struct('x',double(12),'y',double(17),'w',double(32),'h',double(32)) ];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Performance Evaluation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PerformanceEvaluationROC(scores, labels, thresholdRange)
% PerformanceEvaluationROC
% ROC Curve with precision and accuracy
roc = [];
for t=thresholdRange,
TP=0;
FP=0;
for i=1:size(scores,1),
if scores(i) > t % scored positive
if labels(i)==1 % labeled positive
TP=TP+1;
else % labeled negative
FP=FP+1;
end
else % scored negative
if labels(i)==1 % labeled positive
FN = FN+1;
else % labeled negative
TN = TN+1;
end
end
end
precision = TP / (TP+FP+FN+TN);
accuracy = TP / (TP+FN+FP);
roc = [roc ; precision accuracy];
end
plot(roc);
end
function [f1score] = F1Score(precision, recall)
f1score = 2 * ((precision * recall) / (precision + recall));
end