Save indexed image as RGB
Show older comments
I want to display two pseudocolored monochrome images with the same colormap, even though the images have different data ranges. For example one image has the range of values 0-2800 and the other might be like 0-1000. However I want the one with the smaller data range to be displayed using the colormap for the larger range. So far, this is no problem, as I can call clim(), and as you can see from the top row of my visualization below. Everything in the top row is fine.
The problem comes about when I want to save exactly what you see in the upper right image as an RGB image. I've been working on this over several days trying many different ways of using rgb2ind(), exportgraphics(), getframe(), etc. and nothing produces an RGB with the same array size (rows and columns) and with the same displayed colors. I've simplified my actual problem to the simple code below to demonstrate the problem. What I want is the upper right image saved as an RGB image with the full, original resolution (not the resolution of a screenshot) and with the same colors you see in the displayed indexed image. Can anyone figure it out?
% Create an image in the range 0-2800.
data = zeros(700, 200);
[rows, columns] = size(data);
rowValues = linspace(2800, 0, rows);
for row = 1 : rows
data(row, :) = rowValues(row);
end
% Display it with the HSV colormap.
s1 = subplot(2, 2, 1);
cmap = hsv(100); % 100 distinct colors
imshow(data, [], 'ColorMap', cmap, 'Parent', s1);
impixelinfo;
title('Data range 0-2800')
axis('on', 'image');
colorbar(s1)
% clim([20, 1000]);
% Make second image with values from 150 to 750
% The color range should just be in the blue because we're using the same colormap.
s2 = subplot(2, 2, 2);
data2 = zeros(rows, 200);
[rows2, columns2] = size(data2);
% Specify a range for the new data.
minValue = 150;
maxValue = 750;
rowValues = linspace(maxValue, minValue, rows2);
for row = 1 : rows2
data2(row, :) = rowValues(row);
end
imshow(data2, [], 'ColorMap', cmap, 'Parent', s2);
impixelinfo;
caption = sprintf('Data range %.1f to %.1f', minValue, maxValue);
title(caption)
axis('on', 'image');
colorbar(s2)
% If we don't use clim() then the full colormap will go from 150-750.
% If we want it to look like the values from the colormap used for the 2800
% image, we need to use clim to make the colormap go from 0-2800
% instead of from 150 to 750.
clim([0, 2800])
% Now we want to convert it to an RGB image and save to disk for use later.
% First try exportgraphics, however it's a screenshot and not the full resolution.
% exportgraphics(s2, '0000 Screenshot.png') % Not the full resolution!
% Well that didn't work so try ind2rgb.
rgbImage = ind2rgb(data2, cmap); % Doesn't look the same!
% Display the RGB image in the third column
s3 = subplot(2, 2, 3);
imshow(rgbImage);
axis('on', 'image');
title('RGB Image using rgb2ind')
% That didn't work either as you can see from the displayed image.
% More futile attempts by scaling the data.
iData = uint8(rescale(data2, 0, 255)); % Scale data to 0-255
[cmapRows, cmapColumns] = size(cmap);
% Find the indexes in the colormap where the value would be for the min value.
index1 = floor(minValue/2800 * cmapRows)
% Find the indexes in the colormap where the value would be for the max value.
index2 = floor(maxValue/2800 * cmapRows)
% Try to convert it with this section of the full colormap.
rgbImage = ind2rgb(data2, cmap(index1:index2, :));
s4 = subplot(2, 2, 4);
imshow(rgbImage);
axis('on', 'image');
title('RGB Image using scaling')
% Well, the above totally failed.
% Try getframe(), but it only produces a screenshot,
% not at the full resolution of the original data.
% axesData = getframe(s2); % Low resolution screenshot.
% rgbImage = axesData.cdata; % Get the image from the structure.
% s2 = subplot(2, 2, 3);
% imshow(rgbImage);
% axis('on', 'image');
% title('RGB Image')
Accepted Answer
More Answers (0)
Categories
Find more on Red in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

