필터 지우기
필터 지우기

how can i check that my image is rgb

조회 수: 16 (최근 30일)
amir
amir 2013년 4월 17일
편집: DGM 2022년 5월 27일
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
Yashwanth Nandyala
Yashwanth Nandyala 2019년 4월 25일
if length(unique(im)) > 255
im = rgb2gray(im);
end
Walter Roberson
Walter Roberson 2019년 4월 25일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Iman Ansari
Iman Ansari 2013년 4월 17일
편집: Iman Ansari 2013년 4월 17일
Hi
if size(a,3)==3
  댓글 수: 2
Shefali  Singh
Shefali Singh 2018년 3월 17일
What is this size? M not getting what u wanna imply?
Image Analyst
Image Analyst 2018년 3월 17일
size() is a built in function that gives the lengths of the array in each of the dimensions. Who is "M"?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Prashant Birajdar
Prashant Birajdar 2015년 9월 15일
Hi,
Color images have 3 channels (R, G, B), so:
size(your_Image, 3) = 3
For grayscale:
size(your_Image, 3) = 1
  댓글 수: 2
Vish
Vish 2016년 4월 12일
what about difference between gray and binary how to identify that???
Walter Roberson
Walter Roberson 2016년 11월 22일
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
DGM 2022년 4월 21일
편집: DGM 2022년 5월 27일
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);

태그

Community Treasure Hunt

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

Start Hunting!

Translated by