MATLAB Answers


How can i check if an image is RGB or grayscale or binary?

Asked by Cahaya
on 18 Aug 2012
Latest activity Commented on by Stephen Cobeldick on 27 Jul 2018
I'm a new in matlab, need answer to this question.. And, may i change the size of matrix of RGB image to two-dimensional matrix? I wanna try edge detection, i load an grayscale image but the image matrix show an image have three-dimensional matrix.. Any solutions?? Thank you for your response..


%% You can check if an image is RGB or grayscale or binary? by using imagemodel1 function.... as shown below
|Im = Imread ( xyz..... ); handle = image(Im); imgmodel = imagemodel(handle); str = getImageType(imgmodel)
How can i get to know that the image is rgb? I mean I need a program which tell me that "This image is Colored image or this is a binary image"?
@Sky Dutta: read the comments to the accepted answer.

Sign in to comment.


No tags entered yet.

3 Answers

Answer by Image Analyst
on 18 Aug 2012
 Accepted Answer

You can convert to grayscale using a combination of all color channels:
grayImage = rgb2gray(rgbImage);
Or you can extract one of the color channels:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
You can get the size (dimensions) like this:
[rows columns numberOfColorChannels] = size(yourImageArray);


Stephen Thanks for your reply. But, how can I be sure C is a grayscale image and D is a binary image? That's mean, is there any function or anything for checking image type? Thanks in advanced.
@Akib Rahman: images are just numbers, and numbers really don't tell us anything about how numbers should be interpreted. Humans attribute meaning to those numbers by their context. The image type (RGB, grayscale, binary) can be stored as meta-data (like image files do, e.g. in JPEG: "truecolor", "grayscale", etc.) or implied from the array class (but this is not always reliable, see below).
Fundamentally a binary image is just an image with only two values, so you could check if the entire image only have two values. But what happens if I used uint8 and my photo just happens to have exactly two colors in it?
Consider an image that only contains black pixels: there is no way to know from the values alone if the image is intended to be binary or grayscale. The data type could be used (e.g. a logical / one-bit-per-pixel-class can only be used to encode a binary image), but consider if I use uint8 to encode a binary image and then send it to you: in this case you have no way to know if my image is binary or grayscale or even indexed. These would all be exactly the same size. There is no way to "know" what the image type is, unless I tell you or give you supplementary information (e.g. a map, the bit level per pixel, etc).
ndims(A) ==2 && islogical(A) --> true if binary image
ndims(A) == 2 && length(unique(A(:))) == 1 --> true if grayscale or indexed or bi-level but all the same color
ndims(A) == 2 && length(unique(A(:))) == 2 --> true if grayscale or indexed or bi-level with two different values
ndims(A) > 2 --> cannot be grayscale or indexed or binary
ndims(A) == 3 && size(A,3) == 3 && size(unique(A, 'rows'),1) == 1 --> rgb or hsv with all the same color
ndims(A) == 3 size(A,3) == 3 && size(unique(A, 'rows'),1) == 2 --> bilevel rgb or bilevel hsv
Here, bi-level refers to images that have two distinct colors. They might be binary images, but they might not be (for example, red letters on blue background). It is common to encode binary images as the values 0 and 1, but it is also common to encode binary images as the values 0 and 255.

Sign in to comment.

Answer by Walter Roberson
on 18 Aug 2012

Use rgb2gray() to convert the image to grayscale.


Sign in to comment.

Answer by Noor Ul Islam on 27 Dec 2013

% first Read image
% Check img whether it rgb or grayscale
[r c d]=size(img)
% if image is 3D above command will give an error; solution=remove o/p variable d, then it is %perfect
% to convert to grayscale
% use inbuilt command
img1=rgb2gray(img); %now you get 2D image
% to manipulate the true colors RGB; split image into R,G,B components as follow
%Note: each R,G and B component is 2D matrix... hope this is what u required


Sign in to comment.