如何缩放边界框坐标?

时间:2012-11-14 13:01:07

标签: matlab

我正在通过以下方式提取blob的轮廓:

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

我现在要做的是缩放boundaries,以便我可以在原始boundaries内绘制较小版本的boundaries。有一个简单的方法吗?

以下是结果应如下所示的示例:黑色是原始边界框,红色是相同的边界框,只是缩放(但中心与黑框相同)。

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

1 个答案:

答案 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

或者您是否希望避免因速度目的而未经优化的内容?