Why does my code shows three outputs instead of one?

22 ビュー (過去 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 件のコメント
madhan ravi
madhan ravi 2020 年 5 月 28 日
How to check your code without the image?

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

回答 (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