Convert a matrix of doubles to an RGB image, and then back to a matrix of doubles (ideally matching the original)?

28 views (last 30 days)
Can someone offer any advice on how one could take a matrix of doubles, convert it to an RGB image, and then BACK to the original matrix of doubles? Here's my stab at it:
Matrix of doubles
mat = 100*peaks(100); minV = min(mat(:)); mat = mat - minV; % Make the min=0 just because.
figure; imagesc(mat)
title('Matrix of Doubles')
colorbar
matMin = min(mat(:)); matMax = max(mat(:));
Double to RGB
matRGB = double2rgb(mat,jet); % This function is on the FEX...
figure; imagesc(matRGB)
colorbar
title('RGB Image')
RGB back to Double, by way of grayscale?
matGray = im2gray(matRGB);
figure; imagesc(matGray)
colorbar
title('Grayscale')
matDoub = rescale(matGray,matMin,matMax); % Rescale the grayscale matrix to recover the original?
figure; imagesc(matDoub)
colorbar
title('Back to Double?')
Test of difference between original matrix and the recovered one.
delta = matDoub - mat;
sum(delta(:)) % Nope!

Accepted Answer

Prachi Kulkarni
Prachi Kulkarni on 22 Sep 2021
Hi,
The double2rgb file exchange function in your code is converting the matrix of doubles to an RGB image with jet colormap. The im2gray function that you have used to convert the RGB image to back to grayscale is not meant for mapping jet colormap to grayscale colormap. Hence the large error.
Following is a modified version of your code with some additional lines to map the jet colormap to grayscale colormap.
mat = 100*peaks(100); minV = min(mat(:)); mat = mat - minV; % Make the min=0 just because.
figure; imagesc(mat)
title('Matrix of Doubles')
colorbar
matMin = min(mat(:)); matMax = max(mat(:));
matRGB = double2rgb(mat,jet); % This function is on the FEX...
figure; imagesc(matRGB)
colorbar
title('RGB Image')
% ------------------------------------------------------------------------------------------------
% Add these lines to your code
matGray = zeros(size(matRGB));
mapJet = jet;
mapGray = gray;
for i=1:1:100
for j=1:1:100
mapColor = logical(prod(squeeze(matRGB(i,j,:))'==mapJet,2));
matGray(i,j,:) = mapGray(mapColor,:);
end
end
% ------------------------------------------------------------------------------------------------
matGray = im2gray(matGray);
figure; imagesc(matGray)
colorbar
title('Grayscale')
matDoub = rescale(matGray,matMin,matMax); % Rescale the grayscale matrix to recover the original?
figure; imagesc(matDoub)
colorbar
title('Back to Double?')
delta = matDoub - mat;
sum(delta(:)) % Nope!
The conversion back to a matrix of doubles will not be perfect due to quantization losses. However the accuracy of conversion can be measured with the immse function. This function measures the mean squared error between images.
immse(matDoub,mat)
Modifying the code as shown above, the mean square error drops from 3.0496e+05 to 11.0156
Please contact the author of the file exchange submission for double2rgb for more details.

More Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!