-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_Filtering.m
More file actions
185 lines (152 loc) · 4.69 KB
/
Copy pathstep_Filtering.m
File metadata and controls
185 lines (152 loc) · 4.69 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
% STEP_FILTERING - Perform step filtering on the input image using Gabor filters.
%
% This function applies Gabor filters at different scales and orientations to detect lineaments
% in the input image. The maximum response across all filters is taken as the final result.
%
% Syntax:
% [Y] = step_Filtering(I, grammes, DTheta, DPaxos, typeOfFilter)
%
% Inputs:
% I - Input image.
% grammes - Size of the filter.
% DTheta - Vector of angles for filter orientations.
% DPaxos - Vector of filter widths.
% typeOfFilter - Type of filter to use (1 for regular, otherwise smooth).
%
% Outputs:
% Y - Filtered image.
function [Y] = step_Filtering(I, grammes, DTheta, DPaxos, typeOfFilter)
gabors = length(DTheta); % Number of Gabor filter orientations
gabors2 = length(DPaxos); % Number of Gabor filter widths
% Apply Gabor filters
for j = 1:gabors2
bw = DPaxos(j); % Filter width
for i = 1:gabors
theta = DTheta(i); % Filter orientation
if typeOfFilter == 1
gb = mask_fn(grammes, bw, theta, 0);
else
gb = mask_fn_smooth(grammes, bw, theta, 0);
end
gb = gb / sqrt(sum(sum(gb.^2))); % Normalize filter energy
G = conv2(I, gb, 'same'); % Convolve filter with image
if i == 1 && j == 1
Y = G;
else
Y = max(Y, G);
end
end
end
% Zero out the edges of the image
del = 8;
Y(1:del, :) = 0;
Y(:, 1:del) = 0;
Y(size(I, 1)-[0:del-1], :) = 0;
Y(:, size(I, 2)-[0:del-1]) = 0;
end
% MASK_FN - Generate a Gabor filter.
%
% This function creates a Gabor filter with specified size, width, and orientation.
%
% Syntax:
% gb = mask_fn(grammes, bw, theta, toPlot)
%
% Inputs:
% grammes - Size of the filter.
% bw - Filter width.
% theta - Filter orientation.
% toPlot - Flag to indicate whether to plot the filter (1 for yes, 0 for no).
%
% Outputs:
% gb - Generated Gabor filter.
function gb = mask_fn(grammes, bw, theta, toPlot)
sz = grammes;
if mod(sz, 2) == 0, sz = sz + 1; end
[x, y] = meshgrid(-fix(sz/2):1:fix(sz/2), fix(sz/2):-1:fix(-sz/2));
% Rotation
x_theta = x * cos(theta) + y * sin(theta);
y_theta = -x * sin(theta) + y * cos(theta);
% Create Gabor filter
for i = 1:length(x)
for j = 1:length(y)
d = abs(x_theta(i, j));
u = x_theta(i, j);
v = y_theta(i, j);
gb(i, j) = 0;
if d < bw
gb(i, j) = 1;
elseif d <= 3 * bw
gb(i, j) = -1;
end
end
end
[n1, n2] = find(gb == -1);
[p1, p2] = find(gb == 1);
nv_n1 = -length(p1) / length(n1);
for i = 1:length(n1)
gb(n1(i), n2(i)) = nv_n1;
end
gb = gb / sum(sum(gb.^2));
if toPlot == 1
mean(mean(gb));
end
end
% MASK_FN_SMOOTH - Generate a smooth Gabor filter.
%
% This function creates a smooth Gabor filter with specified size, width, and orientation.
%
% Syntax:
% gb = mask_fn_smooth(grammes, bw, theta, toPlot)
%
% Inputs:
% grammes - Size of the filter.
% bw - Filter width.
% theta - Filter orientation.
% toPlot - Flag to indicate whether to plot the filter (1 for yes, 0 for no).
%
% Outputs:
% gb - Generated smooth Gabor filter.
function gb = mask_fn_smooth(grammes, bw, theta, toPlot)
sz = grammes;
if mod(sz, 2) == 0, sz = sz + 1; end
[x, y] = meshgrid(-fix(sz/2):1:fix(sz/2), fix(sz/2):-1:fix(-sz/2));
% Rotation
x_theta = x * cos(theta) + y * sin(theta);
y_theta = -x * sin(theta) + y * cos(theta);
% Create smooth Gabor filter
for i = 1:length(x)
for j = 1:length(y)
d = abs(x_theta(i, j));
u = x_theta(i, j);
v = y_theta(i, j);
gb(i, j) = 0;
if d < bw
gb(i, j) = 1 - ((d / bw)^2);
elseif d <= 3 * bw
gb(i, j) = 0.5 * (-1 + (((d / bw) - 2)^2));
end
end
end
[u1, v1] = find(gb > 0);
[u2, v2] = find(gb < 0);
m1 = 0;
N1 = length(u1);
for i = 1:length(u1)
m1 = m1 + gb(u1(i), v1(i));
end
m1 = m1 / N1;
m2 = 0;
N2 = length(u2);
for i = 1:length(u2)
m2 = m2 + gb(u2(i), v2(i));
end
m2 = m2 / N2;
c = -m1 * N1 / (m2 * N2);
for i = 1:length(u2)
gb(u2(i), v2(i)) = c * gb(u2(i), v2(i));
end
gb = gb / sum(sum(gb.^2));
if toPlot == 1
mean(mean(gb));
end
end