My image is RGB image but when I used size function it showed me grayscale image.

16 views (last 30 days)
I have an image. When I open it using windows photo viewer it showed RGB image but when I use size function of Matlab for finding color channel it showed me "1". That's mean it is the grayscale image. The image also attached.
My Matlab code is:
[rows, columns, numberOfColorChannels] = size('test.jpg');
Output is: numberOfColorChannels is "1"
  6 Comments
Stephen23
Stephen23 on 13 Jun 2018
Edited: Stephen23 on 13 Jun 2018
@Akib Rahman: The link you gave shows how to imread the image into MATLAB and then use size in the image array. In contrast you don't load any data from any file into MATLAB (no imread, no load, nothing at all that actually gets the image file into MATLAB memory), and then instead of testing the size of a variable (which you don't have anyway) you test the size of a 1xN char vector, which might as well be 'Hello World' for all the difference it would make:
>> [~,~,P] = size('Hello World')
P = 1
Why do I get the same value as you did? Is it because I have an image the same size as you... or because I measured the size of an 1xNx1x1x1x... char vector? Your code has nothing to do with any image (unlike the code on the link you gave).
In any case, a simple method to check the image type is to use imfinfo, which does not require reading the image into memory.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 13 Jun 2018
Edited: Stephen23 on 13 Jun 2018
There is no problem, you just did not actually read the image into MATLAB. It works perfectly for me:
>> [img,map] = imread('test.jpg');
>> size(img)
ans =
96 200 3
Or alternatively:
>> S = imfinfo('test.jpg');
>> S.ColorType
ans = truecolor
Nope, not a grayscale image at all.
  3 Comments
Stephen23
Stephen23 on 13 Jun 2018
Edited: Stephen23 on 13 Jun 2018
@Akib Rahman: your file is not a grayscale JPEG image. It has three color channels, which indicates that it is stored as RGB. And when I test it, it shows that it is RGB (truecolor):
>> S = imfinfo('HSIimage2.jpg');
>> S.ColorType
ans =
truecolor
And to test whether this works or not, I converted your original image to grayscale (I used Irfanview, the option to save as a grayscale file is offered in the "save options" dialog box), saved it, and tested my new image file, it shows this:
>> S = imfinfo('HSIimage3.jpg');
>> S.ColorType
ans =
grayscale
Your file is not saved as a grayscale JPEG.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Jun 2018
You're calling size() on the filename string, not the RGB image. You need to call imread() to read the image into a variable:
rgbImage = imread('test.jpg');
[rows, columns, numberOfColorChannels] = size(rgbImage)
like I did in my demo where you pulled this code from (but altered it).
  2 Comments
Akib Rahman
Akib Rahman on 13 Jun 2018
I run your code for 2 images. First image is already given in the question. The output is: rows = 96; columns = 200; numberOfColorChannels = 3
The second image is
The output is: rows = 661; columns = 604; numberOfColorChannels = 3
For both images, numberOfColorChannels is 3. But the second image is grayscale
Image Analyst
Image Analyst on 13 Jun 2018
They're RGB images but all color channels are the same. R=G, R=B, and G=B. So it appears as gray scale. If you don't want that, then you need to take a closer look at how the images were saved originally and why they probably got converted to gray scale BEFORE saving to a JPG file.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!