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

8 次查看(过去 30 天)
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