旋转的多边形对象的宽度和高度-Matlab [英] Width and height of a rotated polyshape object - Matlab

查看:82
本文介绍了旋转的多边形对象的宽度和高度-Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此问题,我正在尝试计算宽度和物体的高度.我是通过将检查的对象转换为多边形并旋转来实现的.如何提取旋转的多边形对象的宽度和高度?有没有办法使用regionprop做到这一点,它会更有效吗?

Based on this question I am trying to calculate the width and height of an object. I did so by converting the examined object to a polyshape and rotated. How can I extract the width and height of the rotated polyshape object? Is there a way to do it using regionprop and will it be more efficient?

代码:

clc;
clear;
close all;

Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
[B,L] = bwboundaries(BW,'noholes');

imshow(Image);
hold on;

k=3;
stat = regionprops(BW,'Centroid','Orientation','MajorAxisLength');
b = B{k};
yBoundary = b(:,2);
xBoundary = b(:,1);
centroidObject = stat(k).Centroid;
xCentre = centroidObject(:,2);
yCentre = centroidObject(:,1);
plot(yCentre, xCentre, 'r*')

orientationDegree =  stat(k).Orientation
hlen = stat(k).MajorAxisLength/2;
cosOrient = cosd(stat(k).Orientation);
sinOrient = sind(stat(k).Orientation);
xcoords = xCentre + hlen * [cosOrient -cosOrient];
ycoords = yCentre + hlen * [-sinOrient sinOrient];

plot(yBoundary, xBoundary, 'r', 'linewidth', 3);
pgon = polyshape(yBoundary, xBoundary);  
polyRot = rotate(pgon,(90+orientationDegree),centroidObject);  
plot(polyRot);  

[xlim,ylim] = boundingbox(polyRot);
Height = xlim(2) - xlim(1); 
Width = ylim(2) - ylim(1);

推荐答案

我将使用最小费雷特直径计算所返回的角度来旋转多边形.通常,处于这种旋转状态的盒子是面积最小的盒子(例外情况很少见).方向"功能是根据最佳拟合椭圆计算得出的,不一定会产生一个小方框.

I would use the angle returned by the minimum Feret diameter calculation to rotate the polygon. Usually, the box at this rotation is the box with minimal area (exceptions seem to be very rare). The 'Orientation' feature is computed based on the best fit ellipse, and would not necessarily yield a small box.

除了旋转整个对象多边形外,还可以仅旋转凸包,凸包通常包含较少的点,因此效率更高.Feret计算已经使用了凸包,因此从 regionprops 请求它没有额外的成本.

Instead of rotating the full object polygon, you can also rotate only the convex hull, which typically contains fewer points and thus would be more efficient. The Feret computation already uses the convex hull, so there is no additional cost to requesting it from regionprops.

这是执行我所描述的代码:

This is code that does what I describe:

Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);

stat = regionprops(BW,'ConvexHull','MinFeretProperties');

% Compute Feret diameter perpendicular to the minimum diameter
for ii=1:numel(stat)
    phi = stat(ii).MinFeretAngle; % in degrees
    p = stat(ii).ConvexHull * [cosd(phi),-sind(phi); sind(phi),cosd(phi)];
    minRect = max(p) - min(p); % this is the size (width and height) of the minimal bounding box
    stat(ii).MinPerpFeretDiameter = minRect(2); % add height to the measurement structure
end

请注意,上面代码中的 minRect 的第一个值是对象的宽度(最小边界框的最小边),等效于 stat(ii).MinFeretDiameter .这两个值不相同,因为它们的计算方式不同,但是它们非常接近.minRect 的第二个值,保存为MinPerpFeretDiameter",是高度(或者更确切地说是最小边界框的最长边).

Note that the first value of minRect in the code above is the width of the object (smallest side of minimum bounding box), and equivalent to stat(ii).MinFeretDiameter. The two values are not identical because they are computed differently, but they are pretty close. The second value of minRect, which is saved as "MinPerpFeretDiameter", is the height (or rather the longest side of the minimum bounding box).

这篇关于旋转的多边形对象的宽度和高度-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆