How do I check if an image is RGB color or gray scale before execution?
41 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Priyanka Roy
am 1 Sep. 2015
Kommentiert: Image Analyst
am 30 Jun. 2021
How do I check if an image is RGB (true color) or gray scale before execution of my code?
Akzeptierte Antwort
Image Analyst
am 1 Sep. 2015
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!