How can i check if an image is RGB or grayscale or binary?
50 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cahaya
am 18 Aug. 2012
Kommentiert: Stephen23
am 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..
3 Kommentare
Sky Dutta
am 27 Jul. 2018
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"?
Akzeptierte Antwort
Image Analyst
am 18 Aug. 2012
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 Kommentare
Stephen23
am 15 Jun. 2018
Bearbeitet: Stephen23
am 15 Jun. 2018
@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
am 15 Jun. 2018
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.
Weitere Antworten (2)
Noor Ul Islam
am 27 Dez. 2013
% 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
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!