Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

changing a colormap on a single subplot

Asked by mat on 6 Jan 2013

i am sure this is very easy but it escapes me.. thanks for your kind note mat

1 Comment

per isakson on 6 Jan 2013

It is definitely not very easy

mat

Products

No products are associated with this question.

2 Answers

Answer by Image Analyst on 6 Jan 2013

See this: http://www.mathworks.com/support/tech-notes/1200/1215.html Or run my demo:

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;

0 Comments

Image Analyst
Answer by Walter Roberson on 6 Jan 2013

The FEX contribution "freezeColors" is useful for such cases.

0 Comments

Walter Roberson

Contact us