Clear Filters
Clear Filters

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

3 views (last 30 days)
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.

Accepted Answer

Guillaume on 15 Nov 2014
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?
Guillaume on 16 Nov 2014
Edited: Guillaume on 16 Nov 2014
When you read a jpeg image, intensities come back either in the 0-255 (as uint8), 0-4096 (as uint16), or 0-65535 (as uint16). The most common is uint8 and it's most likely what you have.
An image of type double in matlab has intensities in the range 0-1 only. If you just use plain imshow on a uint8 image, it uses the range 0-255 to display. On a double image, it uses the range 0-1 to display it.
Because you've converted your image to double, but not adjusted the intensity range, when you use imshow it's going to display all the intensities above 1 (i.e. most of the intensities) as 1 == max intensity.
You have two options:
1. pass the intensity range of your image to imshow:
imshow(data, [0 255]);
2. rescale the intensities when you convert to double
data = double(imread('muzammil.jpg') / 255);
%or if you have the image processing toolbox:
data im2double(imread('muzammil.jpg');
edit: Actually, as of r2014b, im2double no longer requires the image processing toolbox
Muzammil Behzad
Muzammil Behzad on 16 Nov 2014
Thanks im2double worked. Thanks a lot :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Nov 2014
Try casting to uint8 for display
recovered_data = uint8(cat(3, data_matrix_1, data_matrix_2, data_matrix_3));
If the data is not in the range 0-1 (which it's most likely not) then imshow won't work with double images. Casting to uint8 is a simple solution.
Muzammil Behzad
Muzammil Behzad on 16 Nov 2014
Well uint8 didn't work but using im2double instead of double worked. Thanks.
Image Analyst
Image Analyst on 16 Nov 2014
It definitely should work unless you left out some code. What is the range of the data? What does
whos data
show? If you started with uint8, just converted to double and then used cat() and uint8() it WILL work, guaranteed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!