I have some shapes on an image that i've attempted to label according to their Area using a solution provided to me:
stats = regionprops(BW,'Area')
stats2 = regionprops(BW,'Centroid')
figure,imshow(BW)
for k = 1:numel(stats)
xy = stats2(k).Centroid
if (stats(k).Area>TH)
text(xy(1),xy(2),'L') %// Large Shape
else
text(xy(1),xy(2),'S') %// Small Shape
end
end
But it turns out the shapes are too small for the letters ( which would be too small even if I changed the font), I'm wondering if there's a way to do the thresholding to produce a colour code i.e change the filling of the shapes based on their area?
See if this is inspiring enough for you -
%// Input image. This one is chosen as it is available in MATLAB image library
img = imread('coins.png');
%// Convert to binary image
BW = im2bw(img,0.4); %// 0.4 as binary thresehold worked for this specific image
%// Get area and pixel-list stats
stats = regionprops(BW,'Area');
stats2 = regionprops(BW,'PixelIdxList');
s1 = struct2array(stats);
[v1,v2,v3] = unique(s1);
num_colors = numel(v1);
%// Pixel values per channel for creating color codes
pix_per_ch = linspace(0,255,ceil(power(num_colors,1/3)));
%// Unique 3 color codes
all_color_codes = allcomb(pix_per_ch,pix_per_ch,pix_per_ch);
%// allcomb is a MATLAB File-exchange tool avaiialble at -
%// http://www.mathworks.in/matlabcentral/fileexchange/10064-allcomb
%// Unique 3 color codes for the number of shapes available
color_codes = all_color_codes(randi(size(all_color_codes,1),num_colors,1),:);
%// Sort these uniques colors based on their grayscale intensities
[~,ind]=sort(rgb2gray(uint8(permute(color_codes,[1 3 2]))));
sorted_color_codes = color_codes(ind,:);
%// Pre-allocate for the ouput image
out = uint8(255.*BW(:,:,ones(1,3)));
%// Assign each shape a unique color based on their areas
for k = 1:numel(stats)
ind1 = stats2(k).PixelIdxList;
indx = bsxfun(@plus,ind1,[0:2].*size(img,1)*size(img,2));
color_code = sorted_color_codes(v3(k),:);
color_code_ext = color_code(ones(1,numel(ind1)),:);
out(indx) = color_code_ext;
end
%// Display input, output results
figure,
subplot(211),imshow(img),
title('Input Image')
subplot(212),imshow(out),
title('Output Image (Brighter colors represent larger shapes)')
Output -