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

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

 Réponse acceptée

Harsha Vardhan
Harsha Vardhan le 18 Oct 2023
Modifié(e) : Harsha Vardhan le 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 commentaires

@Harsha Vardhan Can you please share the full code from starting to ending then i can understand in easy manner.
@Harsha Vardhan But i want that its was gray in colour and if it is in colour image automically it should convert and print channel 1. If you have any code please share
Harsha Vardhan
Harsha Vardhan le 18 Oct 2023
Modifié(e) : Harsha Vardhan le 18 Oct 2023
@Kartikeya The image you attached 'greyface2.jpg' is a RGB image and not a greyscale image. As explained in the abvove answer, since the R,G and B values are close to each other at each pixel, the final mixed color appears grey.
Please explain what do you want to achieve with your code so that I can help you further.
@Harsha Vardhan I need to achieve its in gray colour and print of 2d matrix. If you have any grey image please share i want run with my code.
You can use 'im2gray' function to convert both RGB images as well as grey scale images to grey scale images without any verification.
originalImage = imread('greyface2.jpg');
grayImage = im2gray(originalImage);
originalImage = imread('greyface2.jpg');
grayImage = im2gray(originalImage);
'grayImage' contains a 2d matrix.
Thanks for Helping . I have done till this and now suppose it is R,G,B print its colour in channel 1, 2, 3 etc. And below i am sharing an image can you please tell how to identify whether it is 2d matrix or not.
Code :-
originalImage = imread('ali.jpg');
grayImage = rgb2gray(originalImage);
imshow(grayImage)
Harsha Vardhan
Harsha Vardhan le 18 Oct 2023
Modifié(e) : Harsha Vardhan le 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.
@Harsha Vardhan Thanks If any doubts again i will ping in comment

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by