Is this problem relate to the image file? The image file is attached with this code. Thank you.
close all;
clear all;
clc;
%read image
I=imread('Left 002.jpg');
%Add salt and pepper noise
J = imnoise(I,'salt & pepper',0.01);
[m,n] = size(J);
output = zeros(m,n);
output = uint8(output);
for i = 1:m
for j = 1:n %intensity of pixel in the noisy image is given as noisy(i,j)
% here we define max and minimum values x and y coordinates of any
% pixel can take
xmin = max(1,i-1); % minimum x coordinate has to be greater than or equal to 1
xmax = min(m,i+1);
ymin = max(1,j-1);
ymax = max(n,j+1);
% the neigberhood matrix will than be
temp = J(xmin:xmax, ymin:ymax);
%now the new intensity of pixel at (i,j) will be median of this
%matrix
output(i,j) = median(temp(:));
end
end
figure(1);
set(gcf, 'Position', get(0,'ScreenSize'));
subplot(131),imshow(I),title('Original Image');
subplot(132),imshow(J),title('Noisy Image');
subplot(134),imshow(output),title('Median Filter');

3 件のコメント

Rik
Rik 2019 年 9 月 19 日
Is there a reason why you aren't using medfilt2? Just by reading your unformatted code I don't notice anything strange.
Nur Hafizah Mohd Nazari
Nur Hafizah Mohd Nazari 2019 年 9 月 19 日
編集済み: Matt J 2019 年 9 月 19 日
I already try using medfilt2 and got errors too.
Error in medfilt2 (line 49)
[a, mn, padopt] = parse_inputs(args{:});
Error in median_filter (line 6)
K = medfilt2(J);
I = imread('Left 002.jpg');
figure, imshow(I)
J = imnoise(I, 'salt & pepper', 0.02);
K = medfilt2(J);
imshowpair(J,K,'montage')
Matt J
Matt J 2019 年 9 月 19 日
I = rgb2gray(imread('Left 002.jpg'));

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2019 年 9 月 19 日
編集済み: Matt J 2019 年 9 月 19 日

1 投票

No, it is because of this line,
ymax = max(n,j+1); %should be min
But I agree with Rik that medfilt2 makes much more sense here.

2 件のコメント

Rik
Rik 2019 年 9 月 19 日
Good catch.
If medfilt2 is not possible (because you don't happen to have the IPT), you should consider composing a 3D matrix and use median(__,3) on that.
Nur Hafizah Mohd Nazari
Nur Hafizah Mohd Nazari 2019 年 9 月 21 日
Thanks for help me out. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by