My image is RGB image but when I used size function it showed me grayscale image.
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Akib Rahman
el 13 de Jun. de 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191229/image.jpeg)
My Matlab code is:
[rows, columns, numberOfColorChannels] = size('test.jpg');
Output is: numberOfColorChannels is "1"
6 comentarios
Stephen23
el 13 de Jun. de 2018
Editada: Stephen23
el 13 de Jun. de 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.
Respuesta aceptada
Stephen23
el 13 de Jun. de 2018
Editada: Stephen23
el 13 de Jun. de 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 comentarios
Stephen23
el 13 de Jun. de 2018
Editada: Stephen23
el 13 de Jun. de 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.
Más respuestas (1)
Image Analyst
el 13 de Jun. de 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 comentarios
Image Analyst
el 13 de Jun. de 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.
Ver también
Categorías
Más información sobre Explore and Edit Images with Image Viewer App en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!