MATLAB Answers

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

73 views (last 30 days)
Akib Rahman
Akib Rahman on 13 Jun 2018
Edited: Stephen on 13 Jun 2018
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
Stephen
Stephen 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

Stephen
Stephen on 13 Jun 2018
Edited: Stephen 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
Stephen
Stephen 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
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

Community Treasure Hunt

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

Start Hunting!

Translated by