-
Notifications
You must be signed in to change notification settings - Fork 1
/
PavementCrackDelineation.m
158 lines (130 loc) · 4.85 KB
/
PavementCrackDelineation.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
function [ output_args ] = PavementCrackDelineation( input_args )
return;
CRACK_IVC = 1;
CRACK_PV14 = 2;
if nargin < 8
stdFactor = 0;
inhibFactor = 0;
end
if dataset == CRACK_IVC
dname = 'crack_ivc_data';
imagesdir = 'images';
gtdir = 'gt';
prefix_gt = 'B_';
elseif dataset == CRACK_PV14
dname = 'CrackPV14';
imagesdir = 'cracks14';
gtdir = 'cracks14_gt';
prefix_gt = '';
end
outputdir = [experimentpath 's' num2str(sigma) '_r' num2str(len) '_s0_' num2str(sigma0) '_a' num2str(alpha) '_sf' num2str(stdFactor) '_if' num2str(inhibFactor) '/'];
if ~exist(outputdir)
mkdir(outputdir);
end
% Binarization thresholds
thresholds = 0.01:0.01:0.99;
nthresholds = numel(thresholds);
if ~exist([outputdir 'result.mat'], 'file')
%% Symmetric filter params and configuration
x = 101; y = 101; % center
line1(:, :) = zeros(201);
line1(:, x) = 1; %prototype line
% Symmetric filter params
symmfilter = cell(1);
symm_params = SystemConfig;
% COSFIRE params
symm_params.inputfilter.DoG.sigmalist = sigma;
symm_params.COSFIRE.rholist = 0:2:len;
symm_params.COSFIRE.sigma0 = sigma0 / 6;
symm_params.COSFIRE.alpha = alpha / 6;
% Orientations
numoriens = 12;
symm_params.invariance.rotation.psilist = 0:pi/numoriens:pi-pi/numoriens;
% Configuration
symmfilter{1} = configureCOSFIRE(line1, round([y x]), symm_params);
% Prepare the filter set
filterset(1) = symmfilter;
%% Inhibitory part configuration
if inhibFactor ~= 0
inhibSymmfilter{1} = symmfilter{1};
inhibSymmfilter{1}.tuples(1,:) = 0;
inhibSymmfilter{1}.tuples(2,:) = inhibSymmfilter{1}.tuples(2,:) * stdFactor;
filterset(2) = inhibSymmfilter;
end
files = rdir([datasetpath dname '/' imagesdir '/*.bmp']);
nfiles = size(files, 1);
% Initialize result matrix
nmetrics = 6;
RESULTS = zeros(nfiles + 1, nmetrics, nthresholds);
time = zeros(1, nfiles);
for n = 1:nfiles
imageInput = double(imread(files(n).name)) ./ 255;
% if ~isdeployed
% figure;
% subplot(2,3,1);
% imagesc(imageInput); colormap gray; title('Original');
% end
%imageInput = adapthisteq(imageInput);
%imageInput = medfilt2(imageInput, [5 5]);
%imageInput = imguidedfilter(imageInput,'NeighborhoodSize',[11 11]);
%imageInput = imadjust(imageInput);
% if ~isdeployed
% subplot(2,3,2);
% imagesc(imageInput); colormap gray; title('Filtered');
% end
[p name ext] = fileparts(files(n).name);
gt = double(imread([datasetpath dname '/' gtdir '/' prefix_gt name '.bmp'])) ./ 255;
% if ~isdeployed
% imagesc(gt); colormap gray; title('GT');
% subplot(2,3,3);
% end
tic;
imageInput = imcomplement(imageInput);
% Pad input image to avoid border effects
NP = 50; imageInput = padarray(imageInput, [NP NP], 'replicate');
%% Filter response
tuple = computeTuples(imageInput, filterset);
[response rotations] = applyCOSFIRE_inhib(imageInput, filterset, inhibFactor, tuple);
response = response{1};
response = response(NP+1:end-NP, NP+1:end-NP);
time1 = toc;
% if ~isdeployed
% subplot(2,3,4);
% imagesc(response); colormap gray;
% end
rotations_final = zeros(size(response, 1), size(response, 2), size(rotations, 3));
for j = 1:size(rotations, 3)
rotations_final(:,:,j) = rotations(NP+1:end-NP, NP+1:end-NP, j);
end
% Evaluation
for j = 1:nthresholds
binImg = binarize(rotations_final, thresholds(j));
binImg2 = bwmorph(binImg, 'close');
binImg2 = bwmorph(binImg2,'skel',Inf);
imwrite(binImg2 .* 255, 'skeleton.jpg');
[cpt2, crt2, F2] = evaluate(binImg2, gt, 2);
[cpt3, crt3, F3] = evaluate(binImg2, gt, 3);
RESULTS(n, :, j) = [cpt2, crt2, F2, cpt3, crt3, F3];
end
tic;
binImg = binarize(rotations_final, 0.49);
binImg2 = bwmorph(binImg, 'close');
binImg2 = bwmorph(binImg2,'skel',Inf);
time2 = toc;
time(n) = time1 + time2;
end
disp(['Mean execution time: ' num2str(mean(time))]);
disp(['Std execution time: ' num2str(std(time))]);
% Average Results
avg_results = reshape(mean(RESULTS(1:nfiles, :, :)), nmetrics, nthresholds)';
% for j = 1:nthresholds
% RESULTS(end, :, j) = avgRES(j, :);
% end
[M idx] = max(avg_results(:,3))
% Save results
%dlmwrite([outputdir 'result.txt'], res);
save([outputdir 'result.mat'], 'avg_results');
save([outputdir 'RESULTS.mat'], 'RESULTS');
RESULTS = [];
end
end