changing a colormap on a single subplot

6 views (last 30 days)
mat
mat on 6 Jan 2013
i am sure this is very easy but it escapes me.. thanks for your kind note mat

Answers (2)

Image Analyst
Image Analyst on 6 Jan 2013
clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% The colormap can only be 256 rows long (otherwise it repeats - you can't assign them).
% So make each image only a portion of the 256 gray levels.
% Define a colormap that consists of 4 separate colormaps.
% So make each colormap section 256/4 = 64 rows long.
numberOfImages = 4;
cmap = [gray(256/numberOfImages);...
jet(256/numberOfImages);...
copper(256/numberOfImages);...
winter(256/numberOfImages)];
% Apply the colormap to the figure.
colormap(cmap);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% if there are 4 images, each needs to be displayed in 256/4 or 64 gray levels.
% Use imadjust() to map the original gray image into that range.
% Generate the first image.
subplot(2, 2, 1);
% Map into 0-63.
firstImage = imadjust(grayImage, [], [0 1/numberOfImages]);
% Display image mapped into the new intensity range.
image(firstImage);
title('Gray Colormap', 'FontSize', fontSize);
colorbar;
% Generate the other images such that
subplot(2, 2, 2);
% Map into 64-127.
secondImage = imadjust(grayImage, [], [1/numberOfImages 2/numberOfImages]);
% Display image mapped into the new intensity range.
image(secondImage);
title('Jet Colormap', 'FontSize', fontSize);
colorbar;
subplot(2, 2, 3);
% Map into 128-191.
thirdImage = imadjust(grayImage, [], [2/numberOfImages, 3/numberOfImages]);
% Display image mapped into the new intensity range.
image(thirdImage);
title('Hot Colormap', 'FontSize', fontSize);
colorbar;
subplot(2, 2, 4);
% Map into 192-255.
fourthImage = imadjust(grayImage, [], [3/numberOfImages, 4/numberOfImages]);
% Display image mapped into the new intensity range.
image(fourthImage);
title('Winter Colormap', 'FontSize', fontSize);
colormap(cmap)
colorbar;

Walter Roberson
Walter Roberson on 6 Jan 2013
The FEX contribution "freezeColors" is useful for such cases.

Community Treasure Hunt

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

Start Hunting!