필터 지우기
필터 지우기

How can I distinguish between grayscale and color images?

조회 수: 4 (최근 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개)

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 CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by