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

18 views (last 30 days)
Can an RGB image be stored as 2D matrix without converting to greyscale image in matlab?

Answers (2)

Jan
Jan on 29 Jun 2015
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 on 28 Apr 2022
Edited: DGM on 28 Apr 2022
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.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!