Convert a matrix of doubles to an RGB image, and then back to a matrix of doubles (ideally matching the original)?
28 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
Prachi Kulkarni
2021 年 9 月 22 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Modify Image Colors についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!