how to know if image is rgb?

Technical Source
3 min readFeb 4, 2023

--

i want to set an if condition to know if the image i am reading is rgb image or not

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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()

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.