MATLAB: How to color code a black and white image by area

Image Processing Toolboximage segmentationregionprops

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

Best Answer

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);