2012-11-14 95 views
0

我提取斑點的輪廓方式如下:如何縮放邊界框座標?

bw = im2bw(image, threshold); 
boundaries = bwboundaries(bw); 
plot(boundaries(:, 2), boundaries(:, 1), 'k', 'LineWidth', 2); 

什麼,我想現在要做的,就是規模boundaries這樣我就可以繪製原boundariesboundaries的縮小版。是否有捷徑可尋?

下面是關於結果應該是什麼樣子的示例:黑色是原始邊界框,紅色是相同的邊界框,只是縮放(但與黑色框的中心相同)。

enter image description here

編輯: 我想我可以單獨採用逐點,但後來我還是回到中心座標。有沒有更好的方法來做到這一點?

scale = 0.7 
nbr_points = size(b, 1); 
b_min = nan(nbr_points, 2); 
for k = 1 : nbr_points 
    b_min(k, :) = ([scale 0; 0 scale] * b(k, 1:2)')'; 
end 
+0

重新調整每個點到中心的距離可能會更舒服嗎?如下:從矩陣中扣除中心,重新調整中心。 –

回答

1

只是創建一個這樣做的函數應該很容易。

function scaledB = scaleBoundaries(B,scaleFactor) 
% B is a cell array of boundaries. The output is a cell array 
% containing the scaled boundaries, with the same center of mass 
% as the input boundaries. 

%%  
for k = 1:length(B) 
    scaledB{k} = B{k} .* scaleFactor; 
    com = mean(B{k}); % Take the center of mass of each boundary 
    sCom = mean(scaledB{k}); % Take the center of mass of each scaled boundary 
    difference = com-sCom; % Difference between the centers of mass 
    % Recenter the scaled boundaries, by adding the difference in the 
    % centers of mass to the scaled boundaries: 
    scaledB{k}(:,1) = scaledB{k}(:,1) + difference(1); 
    scaledB{k}(:,2) = scaledB{k}(:,2) + difference(2); 
end 
end 

或者您是否想要避免出於速度目的未優化的內容?

+0

太棒了!你能解釋一下'com'&'sCom'部分嗎? – memyself

+0

對不起,它應該工作。 com部分只是質量中心,爲了重新定義邊界,您需要這些。 – solimanelefant