How can I distinguish between grayscale and color images?

15 ビュー (過去 30 日間)
Alvindra Pratama
Alvindra Pratama 2016 年 5 月 26 日
コメント済み: Alvindra Pratama 2016 年 5 月 26 日
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?

採用された回答

Image Analyst
Image Analyst 2016 年 5 月 26 日
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
  1 件のコメント
Alvindra Pratama
Alvindra Pratama 2016 年 5 月 26 日
Thank you this is work for me

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 5 月 26 日
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)

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by