看看這是鼓舞人心的足夠你 -
%// 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)')
輸出 -
你的意思是填充邊界或顏色代碼的整個形狀?如果它只是邊界,那麼對於小的形狀,它幾乎是我想的整個形狀。 – Divakar
整個形狀。 –