how to know if image is rgb?

조회 수: 37 (최근 30일)
Mahmoud Hassan
Mahmoud Hassan 2019년 4월 21일
편집: Stephen23 2019년 4월 22일
i want to set an if condition to know if the image i am reading is rgb image or not

답변 (1개)

Walter Roberson
Walter Roberson 2019년 4월 21일
편집: Walter Roberson 2019년 4월 22일
For some kinds of image files, it is not possible to be sure.
You can try using imfinfo() on the image file, and looking for the 'ColorType' of 'truecolor' -- this can be used to distinguish some of the more obscure formats that permit 3 independent channels that are not RGB.
In practice, usually you can just test whether size(img,3) is 3. But that is not completely accurate
  • if ndims(img) is 2 then it is not RGB
  • if ndims(img) is 4 then it is not a single RGB image, but it might be a sequence of RGB images or volume of RGB images. For example GIF reads as rows x cols x 1 x frames (GIF is always pseudocolor but each frame might have its own colormap)
  • if ndims(img) is 5 and the image is not DICOM or TIFF, or if ndims is greater than 5 for any file format, then you need to research the file format and it probably is not RGB
  • if size(img,3) is 1 or 2 then it is not RGB. 1 is common for grayscale and pseudocolor images. 2 is not common but I did encounter a pseudocolor + alpha PNG file
  • if size(img,3) is 3 and it is an image file format you can read with imread(), other than TIFF, then it is RGB
  • if size(img,3) is 3 and it is DICOM and ndims is 3 then it might be RGB or it might be 3 grayscale slices or 3 timepoints of ultrasound
  • if size(img,3) is 3 and it is DICOM and ndims is 4 then it is RGB that might be multiple slices or ultrasound timepoints
  • if size(img,3) is 3 and it is TIFF, you cannot be sure it is RGB unless you check the TIFF tags or check imfinfo ColorType: it could be grayscale+2 other, or pseudocolor+2 other, or multispectral that happened to have 3 channels. But it probably is RGB
  • if size(img,3) is 4 and it is not PNG or TIFF or BMP or JP2 or DICOM, then it is not RGB and is a weird file (or some situation I have missed)
  • if size(img,3) is 4 and it is PNG or BMP or JP2, then it is probably RGBA rather than RGB. Note: some PNG files code alpha as the 4th channel, but other PNG files code alpha as a separate image that is returned as the third output of imread()
  • if size(img,3) is 4 or more and it is DICOM, it is not RGB and is probably grayscale slices or possibly ultrasound
  • if size(img,3) is 4 and it is TIFF, then it might be RGBA or it might be CMYK or it might be RGB+other, and it is best to check imfinfo ColorType and Transparency or the TIFF tags
  • if size(img,3) is 5 and it is TIFF, then it might be RGBA+other or it might be RGB+2 other (such as two IR channels) or it might be CYMK+other (not common but not impossible), or it might be multispectral, and it is best to check imfinfo ColorType and Transparency or the TIFF tags. TIFF RGB+2 IR is used for remote sensing
  • if size(img,3) is 6 or more and it is TIFF, it is multispectral that might or might not happen to have RGB layers
  • TIFF might happen to have multiple GEOTIFF layers -- but those are more likely to show up as sequences of files in the TIFF directory
I have probably missed some possibilities.
To summarize:
  1. if size(img,3) == 3, likely you are dealing with RGB, but there are uncommon exceptions
  2. if size(img,3) == 4 and not TIFF, likely you are dealing with RGBA
  3. size(img,3) == 4 for TIFF and not meaning RGBA really does happen, especially for CMYK, but RGBA is a distinct possibility to. This is case that needs to be checked in more detail.
  4. TIFF is complicated and it is better to query imfinfo or TIFF tags
  댓글 수: 2
Stephen23
Stephen23 2019년 4월 22일
편집: Stephen23 2019년 4월 22일
Note that the original question does not mention the word "file" anywhere, only "image", nor is any specific image file format mentioned: this "image" might be saved in a .mat file, or some other proprietary/custom file format, which means that it could be stored in any colorspace.
For example, images are often encoded in HSL, L*a*b*, etc. colorspaces for the purpose of interpolation, color matching, feature identification, etc. Many of these colorspaces also have three (or sometimes four) pages along the third dimension. Which means that it is impossible to tell if an image (as opposed to the specific file formats that you list) is RGB just by looking at its values.
Walter Roberson
Walter Roberson 2019년 4월 22일
The question talks about an image that they are reading. You usually do not talk about "reading" .mat files: you usually talk about "loading" them.
HSL, L*a*b*, etc., colorspaces are not supported to be stored by any file format that imread() can process, with the exception of TIFF. They are also not supported by DICOM. TIFF can store arbitrary data arrays that you might just happen to know are HSL, L*a*b* or whatever, or there might be private tags for that purpose. PNG in theory can support blobs of data up to 1 Gb (I think it was, might be 2 Gb) without interpretation, but imread() cannot read such blobs. They can be stored with multibandread() / multibandwrite(), which is pretty close to pure binary files.

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

카테고리

Help CenterFile Exchange에서 DICOM Format에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by