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

조회 수: 1 (최근 30일)
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

채택된 답변

Harsha Vardhan
Harsha Vardhan 2023년 10월 18일
편집: Harsha Vardhan 2023년 10월 18일
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
Harsha Vardhan
Harsha Vardhan 2023년 10월 18일
편집: Harsha Vardhan 2023년 10월 18일
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.
Kartikeya
Kartikeya 2023년 10월 18일
@Harsha Vardhan Thanks If any doubts again i will ping in comment

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by