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

52 ビュー (過去 30 日間)
Cahaya
Cahaya 2012 年 8 月 18 日
コメント済み: Stephen23 2018 年 7 月 27 日
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..
  3 件のコメント
Sky Dutta
Sky Dutta 2018 年 7 月 27 日
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"?
Stephen23
Stephen23 2018 年 7 月 27 日
@Sky Dutta: read the comments to the accepted answer.

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

採用された回答

Image Analyst
Image Analyst 2012 年 8 月 18 日
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);
  5 件のコメント
Stephen23
Stephen23 2018 年 6 月 15 日
編集済み: Stephen23 2018 年 6 月 15 日
@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).
Walter Roberson
Walter Roberson 2018 年 6 月 15 日
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.

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 8 月 18 日
Use rgb2gray() to convert the image to grayscale.

Noor Ul Islam
Noor Ul Islam 2013 年 12 月 27 日
% first Read image
img=imread('imag.jpg');
% 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
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
%Note: each R,G and B component is 2D matrix... hope this is what u required

Community Treasure Hunt

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

Start Hunting!

Translated by