Azzera filtri
Azzera filtri

How can I distinguish between grayscale and color images?

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Image Analyst
Image Analyst il 26 Mag 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

Più risposte (1)

Walter Roberson
Walter Roberson il 26 Mag 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)

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by