How to color code a black and white image by area?

11 views (last 30 days)
Hi Everyone,
I have a black and white image that I've done some morphological operations on and then run regionprops to get information about region areas and perimeters.
My question is how would I go about creating an image that has these regions color coded by the area found using region props? I've searched all over the internet and can't seem to find any leads.
Thanks for your help

Accepted Answer

Walter Roberson
Walter Roberson on 5 Aug 2015
Edited: Walter Roberson on 5 Aug 2015
labelBW = bwlabel(YourBWImage);
reginfo = regionprops(labelBW, 'Area');
all_area = [reginfo.Area];
rel_area = all_area(:) ./ max(all_area);
Now that we have relative areas, we need to color code. We could easily convert to grayscale but that would not be very colorfull. So for lack of better instructions as to how to code the colors, code the relative area as being the hue. Do not just take the raw relative area as that would range 0 to 1 and hue 1 is the same as hue 0 (it wraps around). To prevent color confusion, we limit the upper value of the hue
hue = rel_area * 0.85;
hsvtab = [hue, ones(size(hue,1),2)];
rgbtab = hsv2rgb(hsvtab);
Now that we have an RGB color table we can display the image and activate the colormap
image(labelBW);
colormap(rgbtab);
  1 Comment
Bennett Lambert
Bennett Lambert on 5 Aug 2015
This worked perfect! I just added:
map = colormap;
map(1,:) = [0,0,0];
colormap(map)
to get them to display on a black background and it's exactly what I was looking for.
Thanks!

Sign in to comment.

More Answers (1)

Bennett Lambert
Bennett Lambert on 5 Aug 2015
Thanks so much for taking the time to answer!

Community Treasure Hunt

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

Start Hunting!