Can an RGB image be stored as 2D matrix without converting to greyscale image?

조회 수: 4 (최근 30일)
Anushka
Anushka 2015년 6월 29일
편집: DGM 2022년 4월 28일
Can an RGB image be stored as 2D matrix without converting to greyscale image in matlab?

답변 (2개)

Jan
Jan 2015년 6월 29일
You can reshape the array easily, but then it is not an image anymore in strict sense:
img = rand(640, 480, 3);
img2D = reshape(img, 640, []);
This can be reversed, but I do not see any benefit in this. It is the deeper meaning of 3D and 2D images to contain RGB values or a single channel only.
You can convert the values to uint32:
img = uint32(img(:,:,1) + img(:,:,2)*256 + img(:,:,3)*65536);
This is a 2D array also, and it contains even less memory than the double array, but more memory, when img was a uint8 array.

DGM
DGM 2022년 4월 28일
편집: DGM 2022년 4월 28일
I imagine another obvious interpretation of the question would be "can I store a color image in a 2D array?" with no stipulation that it's a losslessly reversible process. If that's the case, you could always reduce it to an indexed image.
% get an RGB image
rgbpict = imread('peppers.png');
% convert to an indexed image
[indpict cmap] = rgb2ind(rgbpict,256);
% display
imshow(indpict,cmap)
% how many channels does it have? just one.
size(indpict)
ans = 1×2
384 512
Unless the source image is a very simple synthetic image with relatively few colors, the process will irreversibly lose information. You can certainly convert the indexed image back to RGB with ind2rgb(), but don't expect to exactly recover the source image.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by