im2gray not working

I am trying to read my image and to make it as an array. I want to use im2gray command but it is not working. However the same code with same image is working in matlab online and other systems. I have image processing software insatlled in the system. Please help me with this.

2 comentarios

KSSV
KSSV el 8 de Jun. de 2022
The error says im1 (image) is of class uint8 and im2gray is not working on that.
Try:
im2 = im2gray(double(im1)) ;
What version of MATLAB you are using? Actually im2gray should work for uint8 as well.
Preeti Sharma
Preeti Sharma el 11 de Jun. de 2022
I am using version 2020a. I tried and mat2gray worked perfectly. im2gray for the same image worked in version 2020b.

Iniciar sesión para comentar.

Respuestas (2)

DGM
DGM el 8 de Jun. de 2022
Editada: DGM el 8 de Jun. de 2022

0 votos

im2gray() was introduced in R2020b. You're using R2020a.
Depending on what you need, you might be able to do it with rgb2gray() and some array size checks.
switch size(inpict,3)
case 3
outpict = rgb2gray(inpict);
case 1
outpict = inpict;
otherwise
% i'm assuming you don't want to support IA/RGBA images
error('a wild error message appears')
end
If you know all your images are RGB and aren't worried about running into ones that aren't, you can just use rgb2gray().

4 comentarios

Stephen23
Stephen23 el 8 de Jun. de 2022
Note that a single page may be an indexed image, so case 1 is latent buggy.
DGM
DGM el 8 de Jun. de 2022
Editada: DGM el 8 de Jun. de 2022
In the same way, any 3-channel image may be HSV, LAB, or any other format, so every function in MATLAB/IPT that handles RGB images is latent buggy too.
FWIW, im2gray() makes no attempt to discern whether the input is grayscale or indexed. It accepts any image so long as it has:
  • ndims<3
  • ndims==3 and has 3 channels
So im2gray() has the exact same problem.
Of course, if you accept that all these tools are flawed in that they're susceptible to ambiguities that need to be resolved at a higher level, you could always just use MIMT mono() and get support for I/IA/RGB/RGBA inputs and other conversions as well.
[A,~,alph] = imread('sources/standardmods/cmanrgba.png');
A = joinalpha(A,alph); % an RGBA image
B = mono(A,'y601'); % an IA image
Now you can be susceptible to mistakenly being fed CMYK images too!
Stephen23
Stephen23 el 8 de Jun. de 2022
Editada: Stephen23 el 8 de Jun. de 2022
"so every function in MATLAB/IPT that handles RGB images is latent buggy too."
Of course, the user must know what image type they have. My comment was more for the reader.
Ultimately an array of numbers is just that: any possible meaning it has can only be resolved at a higher level, as you very neatly put it.
DGM
DGM el 8 de Jun. de 2022
Apologies if I interpreted your comment as a message to me and not the reader/OP.
Either way, that blog post from Steve is good.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 8 de Jun. de 2022

0 votos

im2 = im1; % Initialize.
if ndims(im2) == 3
% If it's color, convert to gray scale.
im2 = rgb2gray(im2);
end

2 comentarios

DGM
DGM el 8 de Jun. de 2022
rgb2gray() accepts only mxnx3 arrays, but not all 3D images have 3 channels.
Image Analyst
Image Analyst el 8 de Jun. de 2022
@DGM correct. It assumes that the image im2 is a gray scale or RGB image. If im2 is a hyperspectral image or a volumetric image (like from CT MRI) then one should not use rgb2gray() or im2gray(). If you do an error will likely be thrown to alert you of that.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 8 de Jun. de 2022

Comentada:

el 11 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by