how can i check that my image is rgb
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 25 Avr 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.
Réponse acceptée
Iman Ansari
le 17 Avr 2013
Modifié(e) : Iman Ansari
le 17 Avr 2013
Hi
if size(a,3)==3
2 commentaires
Image Analyst
le 17 Mar 2018
size() is a built in function that gives the lengths of the array in each of the dimensions. Who is "M"?
Plus de réponses (2)
Prashant Birajdar
le 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 commentaires
Walter Roberson
le 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
DGM
le 21 Avr 2022
Modifié(e) : DGM
le 27 Mai 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 nca] = chancount(rgba)
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);
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!