MATLAB: 3D to 2D and then back from 2D to 3D

digital image processingimage acquisitionimage processing

Hello,
I am trying this image conversion. I read an image using matlab's command data = double(imread('image.jpg')); which gives me a 3D image of size(data) = 60, 50 and 3. When I plot using imshow('data') it gives me the exact same figure I have. Then, I convert this into 2D by using: data_matrix_1 = data(:,:,1); data_matrix_2 = data(:,:,2); data_matrix_3 = data(:,:,3);
The problem is how can I reconstruct the figure from the 2D data. I used the following but it didn't work: recovered_data = cat(3, data_matrix_1, data_matrix_2, data_matrix_3); imshow(recovered_data)
Please help me solve this simple issue. Thank you.

Best Answer

Your original image is not 3D, it's a colour image where the third dimension is the amount of red, green and blue respectively.
You can extract colour channels the way you've done:
red = data(:, :, 1);
green = data(:, :, 2);
blue = data(:, :, 3);
And you can indeed reconstruct the colour image from the colour channels with
imgcolour = cat(3, red, green, blue);
If that doesn't work for you then the problem is somewhere. For a start you should define "it didn't work". Was an error message displayed? If yes what? If you expected another result, what result did you get and what result did you expect?