change Black and White - Image to Black & Red Image

조회 수: 7 (최근 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);
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
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
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
Lukas Non
Lukas Non 2012년 7월 9일
Hi, thanks for you reply. Unfortunately that did not work.
Ryan
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