Error using medfilt2 Expected input number 1, A, to be two-dimensional.

I have an error using medfilt2, I don't know how to fix it. Please help, here I am using Matlab version R2020a.
Error starting from line 3. Here are the details of the error that appears when running :
  1. Error using medfilt2. Expected input number 1, A, to be two-dimensional.
  2. Error in medfilt2>parse_inputs (line 107). validateattributes(a, ...
  3. Error in medfilt2 (line 49). [a, mn, padopt] = parse_inputs(args{:});
I=imread('peppers.png');
IN=imnoise(I,'salt & pepper',0.02);
J1=medfilt2(IN,[3 3]);
J2=medfilt2(IN,[5 5]);
figure,imshow(I);
figure,imshow(IN);
figure,imshow(J1);
figure,imshow(J2);
-Thank you in advance

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Jun. 2021

0 Stimmen

peppers png is a color image. rgb images are 3D arrays. medfilt2 cannot process 3D arrays.

2 Kommentare

how to handle it? do you have a two-dimensional image?
I still don't understand what kind of image to use
Your next step is imnoise, and that says... https://www.mathworks.com/help/images/ref/imnoise.html#d123e157610 that the input image for it must be grayscale.
So you have several choices:
  • convert the RGB image to grayscale and process the one plane
  • process the three color planes independently and combine the result
  • use medfilt3() instead of medfilt2() (this is probably not a good idea!)
  • convert the RGB image to some other color systems such as L*ab or HSV, and extract one of the representation panes and work with that
For example,
I = imread('peppers.png');
HSV = rgb2hsv(I);
imshow(HSV); title('true hsv');
H = HSV(:,:,1);
H11 = H; H11(:,:,2:3) = 1;
imshow(H11); title('hsv with full S & V')
I11 = hsv2rgb(H11);
imshow(I11); title('RGB reconstructed from H + full S & V')
IN = imnoise(H,'salt & pepper',0.02);
J1 = medfilt2(IN, [3 3]);
HSV1 = J1; HSV1(:,:,2:3) = 1;
imshow(HSV1); title('noised H with full S & V')
I1 = hsv2rgb(HSV1);
imshow(I1); title('RGB reconstructed from noised H + full S & V')
imshowpair(I11, I1, 'diff')
title('no noise vs noise')

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by