When I use imread
in MATLAB and read an image, how would I know if it is RGB, gray scale or single programmatically?
I1 = imread('sample_image.jpg');
How can I know what type I1
is before any conversion?
The PNG, JPEG, and GIF formats are most often used to display images on the Internet. Some of these graphic formats are listed and briefly described below, separated into the two main families of graphics: raster and vector.
You can use imfinfo
to retrieve information about an image file before you load it:
info = imfinfo('sample_image.jpg');
info.ColorType % e.g. 'grayscale', 'truecolor', 'indexed'
info.BitDepth % e.g. 8, 16
You can also look at the help section on imread to see what the output class will be for different file types. The problem comes in determining the difference between a grayscale image, and an indexed colour file - these will have the same size and class. In this case you need to check ColorType
beforehand and load the colormap in when you read the image:
[I, map] = imread(filename)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With