Why does my code shows three outputs instead of one?

29 visualizaciones (últimos 30 días)
Made Kamasan
Made Kamasan el 28 de Mayo de 2020
Comentada: Najaf el 9 de Mzo. de 2024 a las 15:14
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);

Respuestas (1)

Subhadeep Koley
Subhadeep Koley el 28 de Mayo de 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!
  2 comentarios
Asad Ali
Asad Ali el 21 de Mayo de 2022
very helpful, thanks alot brother
Najaf
Najaf el 9 de Mzo. de 2024 a las 15:14
Thank you so mach.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by