how can i check that my image is rgb

39 views (last 30 days)
amir
amir on 17 Apr 2013
Edited: DGM on 27 May 2022
i need to use rgb2gray func. but first i need to check that my image is rgb or grayscale..like this :
if XXXXXX
c1=rgb2gray(a);
c=imadjust(c1);
else
c=imadjust(a);
subplot(2,3,1),imshow(a),title('original');
subplot(2,3,2),imshow(c),title('option 1');
end
what should i use insted of XXXXXX ?
  2 Comments
Yashwanth Nandyala
Yashwanth Nandyala on 25 Apr 2019
if length(unique(im)) > 255
im = rgb2gray(im);
end
Walter Roberson
Walter Roberson on 25 Apr 2019
That test could easily fail for a uint8 RGB image: all it would take would be for one intensity level to be unused. For example,
im = repmat(imread('cameraman.tif'), 1, 1, 3);
is an RGB image, but length(unique(im)) would be 247
That test could easily fail for a 16 bit grayscale image that is represented as double: the number of unique intensities could be greater than 255, and yet it would still be a grayscale image for which rgb2gray() would fail.

Sign in to comment.

Accepted Answer

Iman Ansari
Iman Ansari on 17 Apr 2013
Edited: Iman Ansari on 17 Apr 2013
Hi
if size(a,3)==3
  2 Comments
Shefali  Singh
Shefali Singh on 17 Mar 2018
What is this size? M not getting what u wanna imply?
Image Analyst
Image Analyst on 17 Mar 2018
size() is a built in function that gives the lengths of the array in each of the dimensions. Who is "M"?

Sign in to comment.

More Answers (2)

Prashant Birajdar
Prashant Birajdar on 15 Sep 2015
Hi,
Color images have 3 channels (R, G, B), so:
size(your_Image, 3) = 3
For grayscale:
size(your_Image, 3) = 1
  2 Comments
Vish
Vish on 12 Apr 2016
what about difference between gray and binary how to identify that???
Walter Roberson
Walter Roberson on 22 Nov 2016
Binary images have multiple representations
if islogical(img)
it is binary
elseif isinteger(img)
if all(ismember(img(:), [0 intmax(class(img))]))
bilevel integer image 0 and the maximum for the integer class
elseif all(ismember(img(:), [0 1]))
bilevel integer image 0 and 1
elseif length(unique(img)) <= 2
bilevel integer image of some other integer values
else
integer image that is not bilevel
end
else ~isnumeric(img)
error, data is not logical or integer or numeric
elseif all(ismember(img(:), [0 1]))
bilevel single or double 0 and 1
elseif length(unique(img)) <= 2
bilevel single or double some other numeric values
else
single or double that is not bilevel
end
Which if these you bother to implement would depend upon your definition of "binary".
The shortest of these would probably be
if length(unique(img)) <= 2
which would test for bilevel for all the data types. But remember that bilevel could be "chance"; for example, dark grey on light grey might not be deliberately bilevel. Even if the values are all 0 or 1 (single or double) or all 0 or intmax (integer data) you cannot be sure that it is "deliberately" binary.
One test that can be made is to use imfinfo and examine the file's bitdepth property. If the bitdepth is 1 then the image is intentionally binary. You can get an idea of which image file formats support binary images at https://www.mathworks.com/help/matlab/ref/imwrite.html#input_argument_fmt

Sign in to comment.


DGM
DGM on 21 Apr 2022
Edited: DGM on 27 May 2022
Everyone neglects images with alpha. I doubt anyone really cares, since nothing in MATLAB/IPT naturally produces RGBA images or supports such a workflow. Still, it's a point I'm going to make, since it is something that I use.
From here forward, I'll ignore the fact that a 3-channel image might be HSV or any number of things other than RGB.
Just checking
isrgb = size(a,3)==3
will only tell you if it's RGB, but not RGBA -- despite the fact that the two cases may need to be handled (in part) the same way due to the fact that they both have three color channels.
MIMT chancount() (attached) is very simple and returns the number of color channels and alpha channels that an image has. It assumes that valid channel arrangements are I/IA/RGB/RGBA/RGBAAA, so don't feed it a CMYK array and expect it to know the difference. It makes no bold assumptions based on class or data range like the old isgray()/isind() functions did.
rgb = rand(10,10,3);
rgba = rand(10,10,4);
[ncc nca] = chancount(rgb)
ncc = 3
nca = 0
[ncc nca] = chancount(rgba)
ncc = 3
nca = 1
At that point, you can do whatever you need to do with the information.
This is a bit off topic, but if you're looking at using chancount in an RGBA workflow and you're thinking that you'd then need to check nca to see if there's alpha content and then split it off before you handle the color content separately, you don't really have to do it using chancount(). Just use MIMT splitalpha() (attached) to do the whole thing with less fuss. Similarly, MIMT joinalpha() allows you to merge alpha content with an existing image without needing to check the channel arrangement of the image.
% say you already have an RGBA image in your workspace
workingimage = imread('peppers.png');
workingimage = joinalpha(workingimage,repmat(linspace(0,255,512),[384 1]));
% blindly split off any alpha if present
[workingimage alpha] = splitalpha(workingimage);
% do something with a function that can't handle IA/RGBA
workingimage = rgb2hsv(workingimage);
workingimage(:,:,2) = workingimage(:,:,2)*0.5; % desaturate it
workingimage = im2uint8(hsv2rgb(workingimage));
% if there was alpha, put it back
% if there was no alpha, change nothing
workingimage = joinalpha(workingimage,alpha);

Tags

Community Treasure Hunt

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

Start Hunting!