assigning color to binary image

15 ビュー (過去 30 日間)
Hassan
Hassan 2011 年 12 月 27 日
I have an image consisting of values 1 to 7. I want to show each value with specific color (green,blue,yellow,cyan,magenta,maroon, and sea green). I think I need to use colormap function but dont know how. Any suggestion please?
imshow(BW)
colormap(?)

採用された回答

Image Analyst
Image Analyst 2011 年 12 月 27 日
Hassan: Then, try this:
% Create sample data
m=7*rand(150);
imshow(m, []);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Construct new colormap with 8 rows and 3 columns
% Each row applies to one gray level.
% Create the 8x3 array any way you want.
% Here I'm using the built-in "jet" array
% set up to go over only 8 gray levels.
cmap = jet(8);
% Apply the colormap
colormap(cmap);
colorbar;
  4 件のコメント
Image Analyst
Image Analyst 2011 年 12 月 27 日
Just take the rgb values and divide by 255, something like
cmap=[0 255 0;... % Green
0 0 255;...
255 255 0;,... % Yellow
... etc.
] / 255;
You can figure out the rgb values for the colors like sea green and maroon by using a program such as Photoshop.
Hassan
Hassan 2011 年 12 月 27 日
thanks.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2011 年 12 月 27 日
That's not a binary image. It's probably a labeled image, which is a double. Look at this snippet of code that assigns a multitude of different colors to the regions:
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
imshow(coloredLabelsImage);
  2 件のコメント
Hassan
Hassan 2011 年 12 月 27 日
thanks for the comment. again, this code doesnt give once color to each label. instead, it gives on color to the connected regions.
Hassan
Hassan 2011 年 12 月 27 日
Actually Walter's code was the one I needed. the image consists of many regions (connected cells). some regions have value 1, some regions have value 2, ...and some have value 7. Sorry if I didn't describe it clearly.

サインインしてコメントする。


Sukuchha
Sukuchha 2011 年 12 月 27 日
first of all if your image is binary, you cant have values from 1 to 7.
if you have image with values 1 to 7, first you have label the connected components.
CC = bwconncomp(BW);
L = labelmatrix(CC);
RGB = label2rgb(L);
figure, imshow(RGB)taken from help file of label2rgb
see doc label2rgb
  4 件のコメント
Hassan
Hassan 2011 年 12 月 27 日
thanks Walter. It's the one I needed.
Image Analyst
Image Analyst 2011 年 12 月 27 日
Funny, same function as I used, label2rgb. But you're saying now that you don't have a labeled image and that you don't care about labels or connected regions. So that if you had 15 blobs with gray level 4, then all 15 of those should be the same color. And if you had 42 blobs with gray level 5, then all 42 of those should be the same color (but different than the color used for gray level 4). You can do that with just the regular colormap, like my answer below, without having to use label2rgb at all.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by