필터 지우기
필터 지우기

Fake colorbar for image

조회 수: 7 (최근 30일)
KAE
KAE 2018년 5월 8일
편집: KAE 2018년 5월 8일
I would like to be able to show an image like the example below, but instead make a fake color scale that is not related to the image values. The scale would ideally look like a colorbar, but I would choose the number of colors in the scale, the text label of each color, and its RGB value. My example below might have 4 color patches: 'Red Pepper' [194 24 25], 'Green Pepper' [157 153 47], 'Purple Cloth' [134 94 157], and 'White Garlic' [255 255 255]. Or it could have a different number of color patches. I do not want to do any image processing on the image, just annotate it. Is this possible?
figure;
I = imread('peppers.png'); % Read in example image
imshow(I); % Show image
colorbar; % Not what we want, since it is a continuous color map which is automatically labelled 0, 0.2, 0.4, ...1
% Instead we want a color scale with with the color patch RGB values and text labels supplied by the user

채택된 답변

Image Analyst
Image Analyst 2018년 5월 8일
Do you mean like this:
% Read in the color demo image.
[rgbImage, colorMap] = imread('peppers.png');
imshow(rgbImage, colorMap);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
drawnow;
% 'Red Pepper' [194 24 25], 'Green Pepper' [114 24 55], 'Purple Cloth' [117 74 26], and 'White Garlic' [255 255 255].
cMap = [194 24 25;114 24 55; 117 74 26; 255 255 255]/255
colormap(cMap);
colorbar('Ticks',32:64:255,...
'TickLabels',{'Red Pepper','Green Pepper','Purple Cloth','White Garlic'},...
'FontSize', 20);
I think your color definitions are strange though. Purple and green seem switched.
  댓글 수: 1
KAE
KAE 2018년 5월 8일
편집: KAE 2018년 5월 8일
Yes, my mistake. Now corrected in the example, sorry!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2018년 5월 8일
As long as you're not using the colormap to show anything, you can use the method below. If you need to use the colormap, you can use ind2rgb.
I = imread('peppers.png'); % Read in example image
imshow(I); % Show image
h=colorbar;
cmap=[194 24 25;114 24 55;117 74 26;255 255 255];
cmap=cmap/255;
TickLabels={'Red Pepper','Green Pepper','Purple Cloth','White Garlic'};
Ticks=linspace(0,1,numel(TickLabels)+1);Ticks=Ticks(2:end)-diff(Ticks)/2;
colormap(cmap)
h.TickLabels=TickLabels;
h.Ticks=Ticks;
  댓글 수: 1
KAE
KAE 2018년 5월 8일
This works great, as does the other answer. I wasn't sure what to do so I accepted the one with the screenshot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by