Why does my code shows three outputs instead of one?

조회 수: 21 (최근 30일)
Made Kamasan
Made Kamasan 2020년 5월 28일
댓글: Najaf 2024년 3월 9일
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개)

Subhadeep Koley
Subhadeep Koley 2020년 5월 28일
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
Asad Ali
Asad Ali 2022년 5월 21일
very helpful, thanks alot brother
Najaf
Najaf 2024년 3월 9일
Thank you so mach.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by