How can I distinguish between grayscale and color images?
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alvindra Pratama
le 26 Mai 2016
Commenté : Alvindra Pratama
le 26 Mai 2016
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?
0 commentaires
Réponse acceptée
Image Analyst
le 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
Plus de réponses (1)
Walter Roberson
le 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)
0 commentaires
Voir également
Catégories
En savoir plus sur Read, Write, and Modify Image dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!