2014-07-16 100 views
1

我有,我已經嘗試標籤的圖像上的一些形狀根據使用提供給我一個解決方案的地區獨特的顏色:灌裝斑點(形狀)基於區域

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 

但事實證明,形狀對於字母來說太小(即使我改變了字體,形狀也會太小),我想知道是否有一種方法可以進行閾值生成顏色代碼,即根據區域改變填充形狀?

+0

你的意思是填充邊界或顏色代碼的整個形狀?如果它只是邊界,那麼對於小的形狀,它幾乎是我想的整個形狀。 – Divakar

+0

整個形狀。 –

回答

4

看看這是鼓舞人心的足夠你 -

%// 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)') 

輸出 -

enter image description here

+0

堅韌不拔。希望我有另一個+1來給出答案本身:) – beaker

+0

@beaker確實是一個工作的一部分,但後來我不介意圖像處理的東西,所以它平衡了:)讚賞順利! ;) – Divakar