Determine if a Grayscale Image is in Matlab

I am writing a function that can display an image and perform certain smoothing tasks. At the very beginning of my function, I convert the image to grayscale using pic = rgb2gray(pic);

I hope that the function will allow you to make any image (even if it already has shades of gray). In Matlab, if I pass him a grayscale image, it is currently erroneous because it cannot convert it (which is obvious).

Is there a built-in function or an easy way to test the image and determine its color format?

I read on google something about the isRGB and isGrayscale functions, but they were removed from later versions of Matlab ...

I think something like this would be great if it had a built-in function.

  if (pic == RGB) do . . . elseif (pic == GrayScale) do . . . else do . . . 

If not, maybe I could write a function that takes pixel x,y and checks its value?

if (p(x,y) == .... or something else? I'm not sure ... Thoughts?

+6
source share
3 answers

Looks like what @Milo suggested, but with a different function. Use ndims :

 ndims(pic) 

returns the number of dimensions in the pic image. The number of measurements in the array is always greater than or equal to 2, and in the RGB image this will be >2 . The dependent sizes of the singleton are ignored (the size of a singleton is any size for which size(A,dim) = 1 )

+5
source

Color images have 3 channels (R, G, B), therefore:

 size(pic, 3) = 3 

For shades of gray:

 size(pic, 3) = 1 
+11
source

e = imfinfo ('yourimage.someextension');

f.ColorType

this will return you a ColorType image that you can check programmatically.

+1
source

All Articles