How do I check if an image is RGB color or gray scale before execution?

84 ビュー (過去 30 日間)
Priyanka Roy
Priyanka Roy 2015 年 9 月 1 日
コメント済み: Image Analyst 2021 年 6 月 30 日
How do I check if an image is RGB (true color) or gray scale before execution of my code?
  1 件のコメント
Priyanka Roy
Priyanka Roy 2015 年 9 月 1 日
"If" condition satisfy for RGB image
if size(Image,3)==3

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

採用された回答

Image Analyst
Image Analyst 2015 年 9 月 1 日
See this snippet where I check if the number of color channels is 1 and then, if it's not, and I wanted a grayscale image instead of an RGB image, I convert it to gray scale:
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.
end
Of course you can call the image array anything you want, and check if numberOfColorChannels > 1 or if numberOfColorChannels == 3, or however you want to do it, and then take whatever actions you want to based on knowing how many color channels it has.
  2 件のコメント
Martin Makay
Martin Makay 2021 年 6 月 30 日
Your code helped me out big time, stay cool bro.
Image Analyst
Image Analyst 2021 年 6 月 30 日
@Martin Makay, thanks for the nice comment.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by