How can I distinguish between grayscale and color images?

15 Ansichten (letzte 30 Tage)
I have a uigetfile function to take a picture from a folder in the directory D. What I would like to take is a grayscale image. What I want is when I choose a color image it will display a warning message like "an image you selected is not a grayscale image". How can I make what I have said above?

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Mai 2016
Try this:
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
uiwait(warndlg('Converting color image to gray scale by taking green channel'));
end

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 26 Mai 2016
For a single image (not a stack of images), ndim() of the array is 3 for RGB ("truecolor") images. When ndim() of the array is 2, then the array might be grayscale or it might be pseudocolor .
If class() of the array is double and max(abs()) of the array is greater than 1.0 then it must be pseudocolor (or it might be a data array such as a dicom image.) If class() of the array is double and max(abs()) of the array is no more than 1.0 then it must be grayscale. If class() of the array is uint8 or uint16 then it might be either grayscale or pseudocolor.
By default, pseudocolor and grayscale images both display the same, as if they were pseudocolor. To get a grayscale image to display as gray, you use
colormap(gray)

Kategorien

Mehr zu Read, Write, and Modify Image finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by