change Black and White - Image to Black & Red Image

20 views (last 30 days)
Hi all,
I have the following problem. I am displaying binary map information, stored in a 100x100 matrix with imshow. This works well. I am also plotting the error between two maps by substracting map A from map B. This leaves us with an new map C. I can plot that with imshow but I want to change the colors to Black & red instead of BW.
Does anyone know how to do that? Any help is appreciated.

Answers (2)

Image Analyst
Image Analyst on 9 Jul 2012
Edited: Image Analyst on 9 Jul 2012
See my demo, which handles negative values which will occur. -1 = red, 0 = black, and +1 = blue.
workspace; % Make sure the workspace panel is showing.
% Create two sample images.
fontSize = 22;
binaryImageA = false(200, 300);
binaryImageB = false(200, 300);
% Make two regions that overlap.
binaryImageA(60:140, 100:200) = true;
binaryImageB(100:150, 150:250) = true;
% Display the sample images.
subplot(2, 2, 1);
imshow(binaryImageA);
title('Binary Image A', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
subplot(2, 2, 2);
imshow(binaryImageB);
title('Binary Image B', 'FontSize', fontSize);
grid on;
% Subtract the two sample images
differenceImage = int32(binaryImageA) - int32(binaryImageB);
% Display the difference image.
figure;
imshow(differenceImage, []);
title('Difference Image: A - B', 'FontSize', fontSize);
grid on;
% Make colormap: red for negative, 0 for 0, blue for positive.
cmap = [1 0 0; 0 0 0; 0 0 1];
% Apply colormap
colormap(cmap);
colorbar;
  1 Comment
Lukas Non
Lukas Non on 9 Jul 2012
This works perfect. Thank you so much. I was going trough a lot of frustration. You are my hero!!

Sign in to comment.


Ryan
Ryan on 9 Jul 2012
You need to define the colormap:
imshow(BW,[0 0 0; 1 0 0]) % 0's are black and 1's are red
  2 Comments
Ryan
Ryan on 9 Jul 2012
Edited: Ryan on 9 Jul 2012
A general rule of thumb is always go with Image Analyst's answer on image related questions.
The most likely cause is that your matrix C has a -1 as IA pointed out at the beginning of his posts. This works for a logical, 0 & 1, BW image (at least for me). You'd need to specify a third color to handle the other number. I'm fairly sure color maps apply the colors in increasing numerical order so choose wisely.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by