-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskFilter.m
71 lines (61 loc) · 1.48 KB
/
maskFilter.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
clear;
close all;
clc;
h = 512;
w = 512;
image = imread("./inputs/input.jpg");
image = imresize(image, [w h]);
gray = im2gray(image);
noised = imnoise(gray, 'salt & pepper');
mask = ones(3);
sum = 0;
smoothed_avg = zeros(w - 2);
for row = 1:(h - 2)
for col = 1:(w - 2)
maskRow = 1;
for innerRow = row:(row + 2)
maskCol = 1;
for innerCol = col:(col + 2)
sum = sum + double(noised(innerRow, innerCol)) * mask(maskRow, maskCol);
maskCol = maskCol + 1;
end
maskRow = maskRow + 1;
end
avg = sum / 9;
avg = avg / 255;
smoothed_avg(row, col) = avg;
sum = 0;
end
end
smoothed_med = zeros(w - 2);
med_list = zeros(3);
for row = 1:(h - 2)
for col = 1:(w - 2)
maskRow = 1;
for innerRow = row:(row + 2)
maskCol = 1;
for innerCol = col:(col + 2)
med_list(maskRow, maskCol) = double(noised(innerRow, innerCol)) * mask(maskRow, maskCol);
maskCol = maskCol + 1;
end
maskRow = maskRow + 1;
end
med = median(med_list, "all");
med = med / 255;
smoothed_med(row, col) = med;
end
end
figure("Name", "Mask Filters");
tiledlayout(2, 2);
nexttile;
imshow(gray);
title("Gray");
nexttile;
imshow(noised);
title("Salt & Pepper noised");
nexttile;
imshow(smoothed_avg);
title("Avg Mask Filter");
nexttile;
imshow(smoothed_med);
title("Median Mask Filter");