Clear Filters
Clear Filters

Not getting channel 1 in an Face images even though in Grey-colour

1 view (last 30 days)
originalImage = imread('greyface2.jpg');
% To get a 2-D grayscale image from the file, converting from RGB
% to gray scale if it's not already grayscale.
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(originalImage)
rows = 264
columns = 191
numberOfColorChannels = 3
if numberOfColorChannels > 1
% It's already RGB. Just put it into our rgbImage variable.
rgbImage = originalImage;
else
% It's not really RGB like we expected - it's grayscale (or indexed). We'll assume it's grayscale here.
fprintf('It is not really RGB like we expected - it is grayscale.\n');
% Concatenate the gray scale image to be all 3 color channels.
rgbImage = cat(3, originalImage, originalImage, originalImage);
end

Accepted Answer

Harsha Vardhan
Harsha Vardhan on 18 Oct 2023
Edited: Harsha Vardhan on 18 Oct 2023
Hi,
I understand that you want to check if an image is a gray scale image.
In many cases, gray colored images are stored as RGB images. When the R, G and B values of a pixel are either equal or very close to each other, that pixel gets the appearance of grayscale. In such cases, it must not inferred that the image is a gray scale image. Such an image can still contain 3 channels of R, G and B.
When you load such an image in MATLAB using imread, it will still read it as a 3-channel RGB image, because that's how the image is stored. That's why you're seeing numberOfColorChannels as 3.
To understand further, please execute the below code and observe the output.
originalImage(1,1,:)
1×1×3 uint8 array
ans(:,:,1) =
197
ans(:,:,2) =
198
ans(:,:,3) =
200
It can be seen from the above output that the first pixel - originalImage(1,1,:) has 3 channels associated with it. And the values of each channel are very close to each other. This gives an impression of greyscale inspite of having 3 channels.
If you want to ensure you're working with a true single-channel grayscale image in MATLAB, you can convert it using:
grayImage = im2gray(originalImage);
This answer has more information about the values returned by the 'size' function in the context of images - https://www.mathworks.com/matlabcentral/answers/81089-finding-the-dimensions-of-an-image#answer_939490
Hope this helps in resolving your query!
  9 Comments
Harsha Vardhan
Harsha Vardhan on 18 Oct 2023
Edited: Harsha Vardhan on 18 Oct 2023
In the screenshot you attached, the colunm 'size' displays the size of the matrix. The 'grayImage'has 365x265 in the 'size' column. It means it can be identified as a 2-dimensional matrix since it has only rows X columns.

Sign in to comment.

More Answers (0)

Categories

Find more on Modify Image Colors 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!