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:
- if size(img,3) == 3, likely you are dealing with RGB, but there are uncommon exceptions
- if size(img,3) == 4 and not TIFF, likely you are dealing with RGBA
- 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.
- TIFF is complicated and it is better to query imfinfo or TIFF tags