memyself
memyself

Reputation: 12618

how to scale bounding box coordinates?

I'm extracting the outline of blob the following way:

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

what I would like to do now, is to scale boundaries so that I can plot a smaller version of the boundaries inside the original boundaries. Is there an easy way to do this?

Here's an example on what the result should look like: black is the original bounding box, and red is the same bounding box, just scaled (but with same center as black box).

enter image description here

EDIT: I guess I can scale each point individually, but then I still have to recenter the coordinates. Is there a better way of doing this?

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

Upvotes: 2

Views: 3727

Answers (1)

solimanelefant
solimanelefant

Reputation: 546

Just creating a function which does this should be easy.

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

Or did you want to avoid something unoptimized for speed purposes?

Upvotes: 1

Related Questions