2012-11-14 107 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