-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineaments_Auto.m
More file actions
365 lines (290 loc) · 11.3 KB
/
Copy pathLineaments_Auto.m
File metadata and controls
365 lines (290 loc) · 11.3 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
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
function [fbeta_score, N_CWT] = Lineaments_Auto(CWT_Input_Features_Matrix, N_Scales, Scale_D, Sigma3, N_Angles, orderx, ordery, D_Reduct, Line_Res, SF_W, SF_Angles, curv, valu_auto)
% N_Scales
% Scale_D
% Sigma3
% D_Reduct
% SF_W
% LINEAMENTS_AUTO - Automatically detect and tune lineament detection algorithm hyperparameters with Bayesian optimization.
%
% This function processes the input features using Continuous Wavelet Transform (CWT),
% dimensionality reduction, and fault detection techniques to detect lineaments and incorporation Bayesian optiomization techniques to fine-tune the underlying parameters.
% It incorporates the F-beta score for the detected lineaments as a measure for optimization.
%
% Syntax:
% [fbeta_score, N_CWT] = Lineaments_Auto(CWT_Input_Features_Matrix, N_Scales, Scale_D, Sigma3, N_Angles, orderx, ordery, D_Reduct, Line_Res, SF_W, SF_Angles, curv, valu_auto)
%
% Inputs:
% CWT_Input_Features_Matrix - Input feature matrix.
% N_Scales - Number of scales for CWT.
% Scale_D - Scale difference.
% Sigma3 - Sigma value for Gaussian filtering.
% N_Angles - Number of angles for CWT.
% orderx - Order of the derivative in x direction.
% ordery - Order of the derivative in y direction.
% D_Reduct - Dimensionality reduction parameter.
% Line_Res - Resolution for lineaments.
% SF_W - Step filter width.
% SF_Angles - Step filter angles.
% curv - Curvature parameter.
% valu_auto - Automatic value flag.
%
% Outputs:
% fbeta_score - F-beta score for the detected lineaments.
% N_CWT - Number of CWT features.
% Declare global variables
global XI
global YI
global Y_Pix
global X_Pix
global N_Fs
% Determine the beta angle for the CWT based on the derivative orders
if orderx == ordery
betad = 90;
else
betad = 180;
end
% Calculate scales and angles for the CWT
Scales = 1:Scale_D:(N_Scales * Scale_D);
Angless = betad/(N_Angles) - betad/(N_Angles) :betad/(N_Angles) : betad - betad/(N_Angles);
Angles = Angless * pi / 180;
% Determine the number of features
if ndims(CWT_Input_Features_Matrix) == 2
N_Fs = 1;
elseif ndims(CWT_Input_Features_Matrix) == 3
CWT_Input_Features_Matrix_Size = size(CWT_Input_Features_Matrix);
N_Fs = CWT_Input_Features_Matrix_Size(3);
end
% Calculate the total number of CWT features
N_CWT = N_Scales * N_Angles * N_Fs;
% Initialize the feature matrix
Fe = zeros(size(CWT_Input_Features_Matrix, 1) * size(CWT_Input_Features_Matrix, 2), N_Scales * N_Angles, N_Fs);
% Perform CWT on the input features
if valu_auto == 0
for kkk = 1: N_Fs
Feature = CWT_Input_Features_Matrix(:,:,kkk);
tX = fft2(double(Feature)); % Precompute FFT
cwtmor = cwt2d(tX, 'dergauss2d', Scales, Angles, orderx, ordery);
cwtout = cwtmor.data; % X-Y-Scales-Angles
cwtout_abs = abs(cwtout);
for jj = 1:N_Scales
for jjj = 1 : N_Angles
WL = cwtout_abs(:,:,jj,jjj);
if Sigma3 > 0
WL2 = imgaussfilt(WL, Sigma3 * Scales(jj));
else
WL2 = WL;
end
WLFs(:, (jj-1) * N_Angles + jjj) = cat(3, WL2(:));
end
end
Fe(:,:,kkk) = WLFs;
end
else
for kkk = 1: N_Fs
Feature = CWT_Input_Features_Matrix(:,:,kkk);
tX = fft2(double(Feature));
for jj = 1 : N_Angles
for jjj = 1 : N_Scales
if jjj == N_Scales
orderx = 1;
ordery = 0;
elseif jjj == N_Scales -1
orderx = 2;
ordery = 0;
elseif jjj == N_Scales -2
orderx = 1;
ordery = 1;
elseif jjj == N_Scales -3
orderx = 2;
ordery = 1;
elseif jjj == N_Scales -4
orderx = 2;
ordery = 2;
end
if orderx == ordery
betad = 90;
else
betad = 180;
end
Angles = betad/(N_Angles) - betad/(N_Angles) :betad/(N_Angles) : betad - betad/(N_Angles);
cwtmor = cwt2d(tX, 'dergauss2d', jjj, Angles(jj), orderx, ordery);
cwtout = cwtmor.data;
WL = abs(cwtout);
if Sigma3 > 0
WL2 = imgaussfilt(WL, Sigma3 * Scales(jjj));
else
WL2 = WL;
end
WLFs(:, (jj-1) * N_Scales + jjj) = cat(3, WL2(:));
end
end
Fe(:,:,kkk) = WLFs;
end
end
% Reshape the feature matrix for dimensionality reduction
for fss = 1:N_Fs
Fs(:, ((fss-1)*(N_Scales*N_Angles)+1):fss*(N_Scales*N_Angles)) = Fe(:,:,fss);
end
Size_Fs = size(Fs);
for ss = 1:Size_Fs(2)
Fs0(:,ss) = nDstrb1D(Fs(:,ss));
end
for ss = 1:N_CWT
Fs_(:,:,ss) = griddata(XI(:), YI(:), Fs0(:,ss), XI, YI, 'nearest');
global xv
global yv
in = inpolygon(XI, YI, xv, yv);
Fs_ = in .* Fs_;
Fs_(Fs_ == 0) = NaN;
end
% Perform PCA on the CWT features
for vvv = 1:N_CWT
Fs_2d_3 = Fs_(:,:,vvv); Fs_2d_3 = Fs_2d_3(:);
Fs_2d_4(:, vvv) = Fs_2d_3(:);
end
CWT_Features_Columns = Fs_2d_4;
global XI_G
global YI_G
CWT_Features_Columns_PPP = [XI(:) YI(:) CWT_Features_Columns];
CWT_Features_Columns_PPP(any(isnan(CWT_Features_Columns_PPP), 2), :) = []; % Removing the NaNs
XI_G = CWT_Features_Columns_PPP(:, 1);
YI_G = CWT_Features_Columns_PPP(:, 2);
CWT_Features_Columns = CWT_Features_Columns_PPP(:, 3:end);
% Perform S-PCA (Spectral-PCA)
CWT_Features_Columns_Size = size(CWT_Features_Columns);
if D_Reduct <= CWT_Features_Columns_Size(2)
[S_PCA_pp0, U, mu] = PCA(CWT_Features_Columns', D_Reduct);
end
S_PCA_pp00 = S_PCA_pp0';
for ss = 1:D_Reduct
S_PCA_pp(:, ss) = nDstrb1D(S_PCA_pp00(:, ss));
end
siu = size(XI);
S_PCA_pp_Grid = zeros(siu(1), siu(2), D_Reduct);
for ss = 1:D_Reduct
S_PCA_pp_Grid(:,:,ss) = griddata(XI_G(:), YI_G(:), S_PCA_pp(:,ss), XI, YI, 'nearest');
global xv
global yv
in = inpolygon(XI, YI, xv, yv);
S_PCA_pp_Grid = in .* S_PCA_pp_Grid;
S_PCA_pp_Grid(S_PCA_pp_Grid == 0) = NaN;
end
Spectral_PCA_Features_Matrix = S_PCA_pp_Grid;
% Extract lineaments using the PCA features
All_Inputs = Spectral_PCA_Features_Matrix;
originalArrayNoNaNPages = All_Inputs;
nanPageIndices = all(all(isnan(originalArrayNoNaNPages), 1), 2);
originalArrayNoNaNPages(:,:,nanPageIndices) = [];
originalArrayNoNaNPages_Size = size(originalArrayNoNaNPages);
if ndims(originalArrayNoNaNPages) == 2
Number_of_Features_used_for_Lineaments_Detection = 1;
elseif ndims(originalArrayNoNaNPages) == 3
Number_of_Features_used_for_Lineaments_Detection = originalArrayNoNaNPages_Size(3);
end
global grm
grm = SF_W;
global GSF_Angles
GSF_Angles = SF_Angles;
global sigma2
Grid = originalArrayNoNaNPages;
numberoffeatures = Number_of_Features_used_for_Lineaments_Detection;
for ttt6 = 1:numberoffeatures
Grid0_ = (Grid(:,:,ttt6));
Grid2_ = FilterB(Grid0_, sigma2); Grid2_(isnan(Grid2_)) = 0;
vari1(ttt6) = 1 / var(double(Grid2_(:)));
vari1_std = zscore(vari1);
end
for tttt6 = 1:numberoffeatures
Grid0 = Grid(:,:,tttt6);
Grid2 = FilterB(Grid0, sigma2); Grid2(isnan(Grid2)) = 0;
grm2 = round(grm + (curv * grm) * vari1_std(tttt6));
if grm2 <= 1
grm2 = 1;
end
Griddddd = imresize(Grid2, Line_Res / min(X_Pix, Y_Pix));
data1 = FilterB(Griddddd, sigma2);
data1 = im2double(data1);
save('data1.mat', 'data1')
SSA = (grm2 / 15.2) - mod((grm2 / 15.2), 2) + 1;
fsize = round(grm2 / 2);
H = fspecial('gaussian', fsize, round(grm2 / 2));
blurreddata1 = imfilter(data1, H, 'symmetric');
[Asp, Slope] = getSlopeAndAspect(blurreddata1, SSA, SSA);
[~, DSlope] = getSlopeAndAspect(Slope, SSA, SSA);
[DAspect] = getSlopeOfAspect(Asp, SSA, SSA);
FDSlope = imfilter(DSlope, H, 'replicate');
FDAspect = imfilter(DAspect, H, 'replicate');
I = (Slope.^2 .* FDSlope.^1 .* FDAspect.^1).^(1/4);
grammes = grm2;
Step_Filter_nAng = GSF_Angles;
Step_Filter_step = pi / Step_Filter_nAng;
Step_Filter_DTheta = 0:Step_Filter_step:pi - Step_Filter_step;
Step_Filter_Dbw = [2 4 6];
[Y] = step_Filtering(I, grammes, Step_Filter_DTheta, Step_Filter_Dbw, 1);
[BW, plotSkel] = getFaultDetection(Y, data1);
BWorig = BW;
C = bwconncomp(BW);
RS = regionprops(C, 'Area', 'Orientation', 'Centroid');
L = length(RS);
area = [RS(1:L).Area];
Map = bwlabel(BW, 8);
RS_Y = regionprops(C, Y, 'PixelValues');
feat = zeros(L, 2);
for i = 1:L
vec = RS_Y(i).PixelValues;
feat(i, 1) = sum(vec);
end
feat(:, 2) = feat(:, 1);
sv = sort(feat(:, 1), 'descend');
Number_OF_Faults = round(0.85 * length(sv));
Thresh = sv(Number_OF_Faults);
Number_OF_Faults = num2str(Number_OF_Faults);
Labels = find(feat(:, 1) >= Thresh);
[plotH_] = plotNewColorMap0_(Y, zeros(size(data1)), Map, Labels, 'Detected Lineaments', YI(:), XI(:));
plotH00 = (plotH_(:,:,1) + plotH_(:,:,2) + plotH_(:,:,3)) / 3;
plotH00 = imbinarize(plotH00);
plotH00 = imcomplement(plotH00);
plotH0(:,:,tttt6) = plotH00;
end
plotH1 = zeros(size(plotH_));
for n = 1:numberoffeatures
plotH1 = imfuse(plotH1, plotH0(:,:,n), 'blend');
plotH1 = imbinarize(plotH1);
end
plotH1_rgb = im2double(plotH1);
global All_Trg_Matrix
All_Trg_Matrix2 = FilterB(All_Trg_Matrix, sigma2);
All_Trg_Matrix2 = imbinarize(All_Trg_Matrix2);
All_Trg_Matrix2 = im2double(All_Trg_Matrix2);
global X_Pix
global Y_Pix
plotH_rgb_size = size(plotH1_rgb);
All_Trg_Matrix2 = imresize(All_Trg_Matrix2, [plotH_rgb_size(1) plotH_rgb_size(2)]);
data1 = FilterB(All_Trg_Matrix2, sigma2);
data1 = imbinarize(data1);
data1 = im2double(data1);
data1 = cat(3, data1, data1, data1);
plotH1_rgb2 = imcomplement(plotH1_rgb);
real_faults = (data1(:,:,1) + data1(:,:,2) + data1(:,:,3)) / 3;
detected_faults = (plotH1_rgb2(:,:,1) + plotH1_rgb2(:,:,2) + plotH1_rgb2(:,:,3)) / 3;
radius = 1;
radius1 = 10;
radius2 = radius1 + radius;
beta = 0.2;
se1 = strel('rectangle', [radius1 radius1]);
se2 = strel('rectangle', [radius2 radius2]);
real_faults_neighborhood1 = imdilate(real_faults, se1);
real_faults_neighborhood2 = imdilate(real_faults, se2);
detected_faults_neighborhood = detected_faults;
real_faults_neighborhood1 = real_faults_neighborhood1(:);
real_faults_neighborhood2 = real_faults_neighborhood2(:);
detected_faults_neighborhood = detected_faults_neighborhood(:);
tp = sum(real_faults_neighborhood1 & detected_faults_neighborhood);
fp1 = sum(~real_faults_neighborhood1 & detected_faults_neighborhood);
fp2 = sum(~real_faults_neighborhood2 & detected_faults_neighborhood);
fp = fp1 - fp2;
fn = sum(real_faults_neighborhood1 & ~detected_faults_neighborhood);
precision = tp / (tp + fp);
recall = tp / (tp + fn);
fbeta_score = (1 + beta^2) * ((precision * recall) / ((beta^2 * precision) + recall));
end