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!