My code reads an RGB image, processing it to produce floating-point results. How can I write these values as an image, maintaining these floating-point values, in Matlab 7.6.0 (R2008a)?
It is actually possible to fit 64 bits of pixel data (i.e. the number of bits needed to represent a double-precision floating-point value) into certain image formats, specifically a PNG. And you can even recover the data exactly. The key is to encode everything as uint16
values and use a Truecolor RGB image (3 color planes of 16 bit data) along with an alpha transparency map (another 16 bits). Here's the encoding step:
data = rand(4); % Just some sample data, a small 4-by-4 matrix
hexData = num2hex(data(:)); % Get the 16 digit hex codes for each pixel
C = mat2cell(hexData, numel(data), [4 4 4 4]); % Split the hex codes into 4 groups of 4
C = cellfun(@(c) {uint16(hex2dec(c))}, C); % Convert each hex value into a 16 bit integer
colorData = reshape([C{1:3}], [size(data) 3]); % 4-by-4-by-3 uint16 color data
alphaData = reshape(C{4}, size(data)); % 4-by-4 uint16 alpha data
imwrite(colorData, 'double_data.png', 'Alpha', alphaData); % Save image
You now have a valid image file, although it'll probably looks like random garbage if you view it. Now, you can decode it like so:
[imColor, ~, imAlpha] = imread('double_data.png'); % Load image
imSize = size(imColor); % Get image size
imHex = [dec2hex(imColor(:, :, 1)) ... % Convert each uint16 to hex and concatenate
dec2hex(imColor(:, :, 2)) ...
dec2hex(imColor(:, :, 3)) ...
dec2hex(imAlpha)];
imdata = reshape(hex2num(imHex), imSize(1:2)); % Reproduce data
And an equality check:
>> isequal(imdata, data)
ans =
logical
1 % it's the same!