I am working with a JPEG image, if it results in a 3D matrix (225x225x3), you can convert it to 2D. If the image is grayscale, it will become a 2D matrix. If it contains color information, it will remain a 3D matrix. Could someone please share the code or steps to be followed.

 채택된 답변

Matt J
Matt J 2023년 10월 16일

0 개 추천

댓글 수: 8

Kartikeya
Kartikeya 2023년 10월 16일
Actually i am getting 3d Matrix only even though changed to greycolour . I need to get 2d matrix
Name Size Bytes Class Attributes
I 225x225 50625 uint8
RGB 225x225x3 151875 uint8
a 225x225 405000 double
i 225x225x3 1215000 double
Matt J
Matt J 2023년 10월 16일
We can't see what you did to produce these results. Please run the operations here in the forum from start to finish.
RGB = imread('photo_2023-10-16_18-46-00.jpg');
I = rgb2gray(RGB);
imshow(I);
@Kartikeya, I is a 2D array, see below -
RGB = imread('photo_2023-10-16_18-46-00.jpg');
I = rgb2gray(RGB);
whos
Name Size Bytes Class Attributes I 225x225 50625 uint8 RGB 225x225x3 151875 uint8 ans 1x54 108 char cmdout 1x33 66 char
imshow(I);
Kartikeya
Kartikeya 2023년 10월 16일
편집: Kartikeya 2023년 10월 16일
Is it possible to get RGB 225x225x3 as 225x225x2 because 225 is row and columns and 3 is a 3d matrix i need code for If the image is grayscale, it will become a 2D matrix. If it contains color information, it will remain a 3D matrix in loop cycle.
Dyuman Joshi
Dyuman Joshi 2023년 10월 16일
No, you can't have RBG as 225x225x2.
"i need code for If the image is grayscale, it will become a 2D matrix. If it contains color information, it will remain a 3D matrix in loop cycle. "
You are misunderstanding. Grayscale images are by definition 2D matrices, and RGB images are by definition 3D matrices. You don't need to convert them.
Kartikeya
Kartikeya 2023년 10월 16일
Thanks
Kartikeya
Kartikeya 2023년 10월 16일
May i know what is meant by A, I, RGB in the names

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

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 10월 16일

1 개 추천

Try these two code snippets. The first one gives you a grayscale image, regardless of what's in the file.
originalImage = imread(fullFileName);
% 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)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the red channel.
grayImage = originalImage(:, :, 1); % 2 for green, 3 for blue.
% OR ALTERNATIVELY use the rgb2gray() function to do a weighted average of all 3 color channels.
grayImage = rgb2gray(originalImage);
else
% It's already gray scale. Just put it into our grayImage variable.
grayImage = originalImage;
end
The second one gives you an RGB image regardless of what's in the file.
originalImage = imread(fullFileName);
% 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)
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
If it's indexed, we'll need some additional code to use ind2rgb to apply the colormap.

댓글 수: 3

Kartikeya
Kartikeya 2023년 10월 16일
Thanks for code its helpful for me
@Image Analyst Not getting channel 1 in an Face images even though in Grey-colour based on your code. Can you please help me. I am storing every image in jpeg format.
originalImage = imread('greyface1.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 = 183
columns = 275
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
Image Analyst
Image Analyst 2023년 10월 20일
Not sure what you want. You're using the code snippet to make an image an RGB image, but your image is already an RGB image. See? It says numberOfColorChannels = 3 so that means it's already color, and so there is nothing really to do to make it color. I just put it into another, more secriptively named variable. So why should you get gray scale?

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

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2023년 10월 16일

댓글:

2023년 10월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by