MATLAB Answers

Why does my code shows three outputs instead of one?

3 views (last 30 days)
Made Kamasan
Made Kamasan on 28 May 2020
Answered: Subhadeep Koley on 28 May 2020
Hey guys, I'm currently working on a median filter project, but for some reason it keeps showing three outputs (pictures), does anyone know whats wrong with my code? I tried tinkering with the code but nothing changed. Thanks in advance
pkg load image
clear;
close all;
clc;
I1=imread('dot.jpg');
figure(1);imshow(I1);
k=1;
I2=padarray(I1,[k k],'replicate');
[m n]=size(I1);
for i=2:(m-1)
for j=2:(n-1)
v=I1(i-1:i+1,j-1:j+1);
r=(sum(v(:)))/9;
%r=median((v(:)'));
c(i-1,j-1)=uint8(ceil(r));
endfor
endfor
figure(2),imshow(c);

  1 Comment

Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 28 May 2020
I suspect your dot.jpg is a RGB 3 channel image. When you are extracting the height and width of the image using the size function, if you do not specify the variable to hold the size of the 3rd dimension then the size of the second and the third dimension will get multiplied and stroed in n.
If you do not need the 3rd dimension size use "~" operator to ignor it. Refer the code below,
clear;
close all;
clc;
% Replace this image with your image
I1 = imread('kobi.png');
figure
imshow(I1);
k = 1;
I2 = padarray(I1, [k k], 'replicate');
% Using "~" to ignor third output
[m, n, ~] = size(I1);
% Pre allocating the variable "c" for speed
c = zeros(m, n);
for i = 2:(m-1)
for j = 2:(n-1)
v = I1(i-1:i+1, j-1:j+1);
r = (sum(v(:)))/9;
% r = median((v(:)'));
c(i-1, j-1) = uint8(ceil(r));
end
end
figure
imshow(c);
Hope this helps!

  0 Comments

Sign in to comment.

Categories


Translated by