Pedro Marques
Pedro Marques

Reputation: 175

Represent white spots by rectangles

I have this binary image with white spots: Binary image I want to represent each white spot by a rectangle with the same size of spot and if possible with same orientation. There are any function that do that? I can detect each spot using RP: enter image description here

Upvotes: 0

Views: 84

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60554

I would compute the smallest Feret diameter (shortest projection) with corresponding angle, and the perpendicular projection. That usually corresponds to the smallest bounding box.

See here for MATLAB code on computing Feret diameters: http://www.crisluengo.net/archives/408

Upvotes: 1

m7913d
m7913d

Reputation: 11064

You can try to use regionprops as follows:

I = imread('tHZhy.png');
stats = regionprops(I, 'centroid', 'Orientation', 'MajorAxisLength','MinorAxisLength',  'BoundingBox');
figure
imshow(I)
hold on
for i=1:length(stats)
    xc = stats(i).Centroid;
    ma = stats(i).MajorAxisLength/2;
    mi = stats(i).MinorAxisLength/2;
    theta = -deg2rad(stats(i).Orientation);
    dx = [-ma -mi; ma -mi; ma mi; -ma mi; -ma -mi];
    R = [cos(theta) -sin(theta); sin(theta) cos(theta)]; % rotation matrix
    x = xc + dx*R';
    plot(xc(1), xc(2), 'g*');
    plot(x(:, 1), x(:, 2), 'g');
end

Note that the result is not perfect, especially for rather square objects. The reason therefore is that the major dimension is the greatest when it is directed along a diagonal. enter image description here

Upvotes: 0

Related Questions