draw oriented rectangle around a blob

7 visualizaciones (últimos 30 días)
Tristan Mas
Tristan Mas el 26 de Mzo. de 2022
Comentada: Image Analyst el 27 de Mzo. de 2022
function y=cadre(x)
info = regionprops(x,'Boundingbox') ;
imshow(x)
hold on
for k = 1 : length(info)
BB = info(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','g','LineWidth',1) ;
end
I am using this code with a segmented image in input, and I have this result :
input output
I would like the rectangles to be the smallest and with the same orientation as the objects, to get the angles for exemple.
How can I modify my code ?

Respuesta aceptada

Scott MacKenzie
Scott MacKenzie el 27 de Mzo. de 2022
Editada: Scott MacKenzie el 27 de Mzo. de 2022
Something like this seems to work:
img = imread('testimage.jpg');
bw = imbinarize(im2gray(img),'adaptive','Sensitivity',0.45);
info = regionprops(bw, 'all');
imshow(img);
hold on;
minSize = 50; % ignore small/noise regions (adjust as necessary)
angles = [];
for k = 1:length(info)
% get region properties
w = info(k).MajorAxisLength;
h = info(k).MinorAxisLength;
x = info(k).Centroid(1) - w/2;
y = info(k).Centroid(2) - h/2;
theta = info(k).Orientation;
if h < minSize % skip the small/noise regions
continue;
end
angles = [angles theta];
ps = polyshape([x, x, x+w, x+w], [y, y+h, y+h, y]);
ps2 = rotate(ps, -theta, [x+w/2 y+h/2]);
plot(ps2, 'EdgeColor','r', 'FaceColor','none', 'LineWidth',2);
end
angles
angles = 1×2
67.7787 -44.8710
  1 comentario
Image Analyst
Image Analyst el 27 de Mzo. de 2022
OK but note that the boxes don't completely contain the blobs.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 26 de Mzo. de 2022

Walter Roberson
Walter Roberson el 26 de Mzo. de 2022
Editada: Walter Roberson el 26 de Mzo. de 2022
Note that if you are looking for the angles then regionprops() 'Orientation' will give you the angle of the major axes.

Categorías

Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by