2

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

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

我现在想做的是缩放boundaries,以便我可以boundaries在原始内部绘制一个较小的版本boundaries。是否有捷径可寻?

下面是一个关于结果应该是什么样子的示例:黑色是原始边界框,红色是相同的边界框,只是缩放(但与黑框具有相同的中心)。

在此处输入图像描述

编辑: 我想我可以单独缩放每个点,但是我仍然需要重新定位坐标。有没有更好的方法来做到这一点?

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
4

1 回答 1

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

或者您是否想避免出于速度目的而未优化的东西?

于 2012-11-14T13:37:30.963 回答