-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprob3.m
62 lines (45 loc) · 1.38 KB
/
prob3.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
clear;
% Importing gray scale image
imgg = imread('3.JPG');
img = imgg(:,:,1);
[m, n] = size(img);
sImg = img;
%sImg1 = img;
% Filter definitation
w = [1 2 1 2 4 2 1 2 1];
%w = [1 1 1 1 1 1 1 1 1];
weight = sum(w);
%Filtering the original Image
for i = 1:m
for j = 1:n
z = [getPixel(img, i-1, j-1, m,n), getPixel(img, i-1, j, m,n), getPixel(img, i-1, j+1, m,n), getPixel(img, i, j-1, m,n), getPixel(img, i, j, m,n), getPixel(img, i, j+1, m,n), getPixel(img, i+1, j-1, m,n), getPixel(img, i+1, j, m,n), getPixel(img, i+1, j+1, m,n)];
sImg(i, j) = round(sum(int8(w) .* int8(z)) / weight);
end
end
%Filtering the original Image
for i = 1:m
for j = 1:n
z = [getPixel(img, i-1, j-1, m,n), getPixel(img, i-1, j, m,n), getPixel(img, i-1, j+1, m,n), getPixel(img, i, j-1, m,n), getPixel(img, i, j, m,n), getPixel(img, i, j+1, m,n), getPixel(img, i+1, j-1, m,n), getPixel(img, i+1, j, m,n), getPixel(img, i+1, j+1, m,n)];
sImg1(i, j) = median(uint8(z));
end
end
%Printing the Image and the histogram
figure,
subplot(2,2,1),
imshow(img);
title('Original Image');
subplot(2,2,2),
histogram(img);
title('Original Image');
subplot(2,2,3),
imshow(sImg);
title('Smoothen Image');
subplot(2,2,4),
imshow(sImg1);
title('Smoothen Image using Median filter');
function pixl = getPixel(img, x, y, m, n)
if x < 1 || y < 1 || x >= m || y >= n
pixl = 0;
else
pixl = img(x, y);
end