2017-05-16 21 views
1

私は、MATLABを使って、ラベル付きイメージで見ることができる "objects"/"regions"を持つ構造体を作成しています。プロパティを抽出していますバウンディングボックスエリアregionpropsです。残念ながら(と私には驚いたことに)(領域を描くピクセルのリスト)境界を抽出できませんので、bwboundaryでそれらを作成しています。しかし、これは問題を提起する:が1でラベル付けされた領域に対応する、n番目の領域がラベルnに対応するN個の領域を有するNx1構造体を与える(例えば、obj(1))。bwboundariesとregionpropsをラベルマップで一緒に使用できますか?

obj_boundaries = bwboundaries(obj_labelmap, 'noholes');私は同じように働くことを期待しています。それはほとんどありませんが、ラベルの約80%にすぎません。いくつかのラベルを任意に混合するので、obj_boundaries(1)は5(または他のいずれか)でラベル付けされた領域に対応します。

問題は次のように示されます.1つのラベルがregionprops(BoundingBox)インデックスで、もう1つのラベルがobj_boundaries(boundary)インデックスです。

% get obj regions and boundaries from labelmap 
obj_all = regionprops(obj_labelmap,'BoundingBox','Area','PixelIdxList'); 
obj_boundaries = bwboundaries(obj_labelmap, 'noholes'); 

imshow(obj_labelmap ~= 0); hold on;  % background image 
colors=['b' 'g' 'r' 'c' 'm' 'y']; 
for k=1:length(obj_boundaries), 

    boundary = obj_boundaries{k};  % get boundary 
    cidx = mod(k,length(colors))+1;  % set color 
    plot(boundary(:,2), boundary(:,1),... 
     colors(cidx),'LineWidth',1); 

    % plot text for boundary 
    % randomize text position for better visibility 
    rndRow = ceil(length(boundary)/(mod(rand*k,7)+1)); 
    col = boundary(rndRow,2); row = boundary(rndRow,1); 
    h = text(col+1, row-1, [num2str(k)]);% '|' num2str(obj_labelmap(row,col))]); 
    set(h,'Color',colors(cidx),'FontSize',10,'FontWeight','bold'); 

    % plot bounding boxes 
    bb = obj_all(k).BoundingBox; 
    rectangle('Position', bb,... 
     'EdgeColor', [0.7 0.7 0.7], 'LineWidth', 1) 

    % plot text 
    col = bb(1) ; row = bb(2); 
    h = text(col+3, row+3, num2str(k)); 
    set(h,'Color',colors(cidx),'FontSize',10,'FontWeight','bold'); 
end 

differently assigned labels

構造体の中に境界をマージしようと、私は各境界や魚用のラベル値を抽出する必要があります:図は、私はhereをアップロードしlabelmapにこのコードを使用して生成されました正しいものを取得するか、対応する(理想的にソートされた)idxを格納して取得します。しかし、これは計算上非常に高価です。

% the order in which regionprops objects are stored corresponds to the labels 
    obj_all_idx = zeros(numel(obj_all), 1); 
    for k=1:length(obj_all) 
     pixelList = obj_all(k).PixelIdxList; 
     obj_all_idx(k) = obj_labelmap(pixelList(1)); 
    end; clear k; 

    % the order in which obj_boundaries are stored does NOT correspond to the labels 
    obj_boundaries_idx = zeros(numel(obj_boundaries), 1); 
    for k=1:length(obj_boundaries) 
     boundary = obj_boundaries{k}; 
     col = boundary(1,2); row = boundary(1,1); 
     obj_boundaries_idx(k) = obj_labelmap(row,col); 
    end; clear k; 

私の質問は:領域の境界、にBoundingBoxとエリアのプロパティをマージするための良い方法はありますか?私は何かを監督していますか?これらのプロパティを生成する別の方法はありますか?

答えて

1

bwboundariesを直接regionprops出力に適用すると、混乱することはありません。それは'Image'プロパティにそれを行うための最も単純だが、'PixelIdList'を使用することも可能である:

% generate binary image 
bw = imread('coins.png') > 100; 
% get region props 
props = regionprops(bw,{'Area','BoundingBox','Image','PixelIdxList'}); 
% split props to cells 
C = struct2cell(props).'; 
regions = C(:,3); 
bbox = C(:,2); 
% extract boundaries from 'Image' property 
B = cellfun(@(obj)bwboundaries(obj, 'noholes'),regions,'UniformOutput',0); 
% add bounding box offset to boundary coordinates 
B = cellfun(@(b,box) bsxfun(@plus,b{1},box([2 1]) - 0.5),B,bbox,'UniformOutput',0); 
% assign boundaries cell to props struct 
props(end).BWBoundary = []; 
[props(:).BWBoundary] = deal(B{:}); 
% plot 
imshow(bw); 
hold on; 
for ii = 1:numel(B) 
    plot(B{ii}(:,2),B{ii}(:,1),'LineWidth',3) 
    text(mean(B{ii}(:,2)),mean(B{ii}(:,1)),num2str(ii)); 
end 

enter image description here

関連する問題