2
votes

I have a series on .png images, which were created by Matlab running 'image' on series on mxn matrices. In doing so, it applied the default colormap (jet). I now need to get the data back from the images (the data itself was lost). As I understand it, the image function takes my mxn matrix, and a colormap, and uses it to generate an mxnx3 tensor (of RGB values). Is it possible (aside from just searching through the colormap like a lookup table), to get Matlab to go from the image, and, based on the colormap used to generate it, reconstruct the data.

This can certainly be done, since the mapping from the original data to the RGB values is linear, based on the colormap, but I can only think of using the colormap as a lookup table. Is there a sort of reverse-image function?

Thanks.

3

3 Answers

3
votes

Data is lost in the conversion from an arbitrary m-by-n matrix to an "image":
Matlab shifts and scales your data to range [0..255] and quantizes it to 256 discrete levels.
Even if you are willing to neglect the quantization error, there are still the unknown shift and scale factors you need to recover in order to convert the mapped color image to the original matrix.

You can use rgb2ind to convert the image to the quantized [0..255] values:

 [x map] = rgb2ind( img, 256 ); 

But you cannot convert x to your original data since you do not know the proper scale and shift.

0
votes

You can do that lookup quite easily using the second output of ismember and some reshapes:

[~,x] = ismember(reshape(Img,[],3), jet, 'rows');
reshape(x, size(Img(:,:,1)))
0
votes

I am not aware of a tool that does exactly what you do. Some data is definetly lost Png has at most 48bit per pixel (practically you have much less as you are restrained to the colormap) while double as Matlab uses has 64bits. But it seems to me if you use default jet colormap wou will just have 64 levels.

Practically I think you can get usable results. Jet is supposed to be a HSV colormap. So maybe rgb2hsv will give you a mappable hue value. Using the interpolate functions it should be possible to calculate back. But that would basically be a lookup.