Error in computing median filter and histogram equalization on an image. How can i solve it?

1 回表示 (過去 30 日間)
Abdulmu'min Musa
Abdulmu'min Musa 2020 年 3 月 29 日
編集済み: DGM 2024 年 12 月 13 日
I am trying to perform median filter on an image using simple median filter code like this
a = imread('1a.jpg');
figure; imshow (a)
b = imnoise(a,'salt & pepper',0.02);
figure; imshow (b)
c = medfilt2(b);
figure; imshow (c)
but i am getting this error:
Error using medfilt2
Expected input number 1, A, to be two-dimensional.
Error in medfilt2>parse_inputs (line 107)
validateattributes(a, ...
Error in medfilt2 (line 48)
[a, mn, padopt] = parse_inputs(varargin{:});
Error in medianFilter (line 11)
c = medfilt2(b);
Also when i tried to perform histogram equalization also using this code
a = imread('1a.jpg');
img_eq = histeq(a);
figure; imshow(img_eq);
I also get this error:
Error using histeq
Expected input number 1, I, to be two-dimensional.
Error in histeq (line 69)
validateattributes(a,{'uint8','uint16','double','int16','single'}, ...
Error in Histogramequalization (line 13)
img_eq = histeq(a);
How can i go about it pls.
Here are the two types of images i used.

回答 (1 件)

DGM
DGM 2024 年 12 月 11 日
編集済み: DGM 2024 年 12 月 13 日
The image is a JPG, and most JPGs are RGB.
fname = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/280425/1a.jpg';
inpict = imread(fname); % RGB, uint8
size(inpict) % this is not a single-channel image
ans = 1×3
181 146 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
We can also look at metadata if that's not certain enough
S = imfinfo(fname);
S.ColorType % 24b RGB
ans = 'truecolor'
S.NumberOfSamples % 3 channels
ans = 3
S.BitDepth % 8b x 3channels = 24b
ans = 24
If the image is nominally gray and is to be treated as gray, then just convert it using im2gray() (or rgb2gray() in older versions)
% the simple modern way
% im2gray() will convert RGB inputs and pass gray inputs
graypict1 = im2gray(inpict);
% but rgb2gray() will throw an error if it receives anything but a 3-ch image,
% so you have to do its input handling for it externally.
% if you're not going to use anything older than R2020b, just use im2gray().
if size(inpict,3) ~= 1
graypict2 = rgb2gray(inpict);
end
See also:

Community Treasure Hunt

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

Start Hunting!

Translated by