change Black and White - Image to Black & Red Image

5 ビュー (過去 30 日間)
Lukas Non
Lukas Non 2012 年 7 月 9 日
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.

回答 (2 件)

Image Analyst
Image Analyst 2012 年 7 月 9 日
編集済み: Image Analyst 2012 年 7 月 9 日
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);
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);
title('Binary Image B', 'FontSize', fontSize);
grid on;
% Subtract the two sample images
differenceImage = int32(binaryImageA) - int32(binaryImageB);
% Display the difference image.
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
  1 件のコメント
Lukas Non
Lukas Non 2012 年 7 月 9 日
This works perfect. Thank you so much. I was going trough a lot of frustration. You are my hero!!


Ryan 2012 年 7 月 9 日
You need to define the colormap:
imshow(BW,[0 0 0; 1 0 0]) % 0's are black and 1's are red
  2 件のコメント
Ryan 2012 年 7 月 9 日
編集済み: Ryan 2012 年 7 月 9 日
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.


Community Treasure Hunt

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

Start Hunting!

Translated by