-
Notifications
You must be signed in to change notification settings - Fork 3
/
erosion.m
28 lines (27 loc) · 1.1 KB
/
erosion.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
function eroded_image = erosion(image, se)
[rows, cols] = size(image);
[se_rows, se_cols] = size(se);
se_row_center = ceil(se_rows/2);
se_col_center = ceil(se_cols/2);
eroded_image = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
% Check if the current pixel is part of the image
if image(i, j) == 1
% Get the coordinates of the neighborhood
row_start = i - se_row_center + 1;
row_end = i + se_row_center - 1;
col_start = j - se_col_center + 1;
col_end = j + se_col_center - 1;
% Check if the neighborhood is inside the image
if (row_start > 0) && (row_end <= rows) && (col_start > 0) && (col_end <= cols)
neighborhood = image(row_start:row_end, col_start:col_end);
% Check if all pixels in the neighborhood match the structuring element
if all(neighborhood(se == 1))
eroded_image(i, j) = 1;
end
end
end
end
end
end