# Why does my code shows three outputs instead of one?

3 views (last 30 days)
Made Kamasan 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
clear;
close all;
clc;
figure(1);imshow(I1);
k=1;
[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

madhan ravi on 28 May 2020
How to check your code without the image?

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
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!