这是一个很难回答的问题,我做了一些没有太大成功的想法。 我的主要目标是旋转任意像素对象,以最小化它的边界框'宽度。(或最大化高度,假设周长不变则应该相同)
因为这个目标不在SO问题的范围内,所以我确定了一个更简单的目标,它将导致解决方案:找到像素对象的几何中心。
为什么呢?因为如果我有这个中心,我将能够找到离它最远的点,然后旋转物体,使这些点垂直对齐。
我原本以为这就像计算边界框的中心一样简单。 Inkscape中的快速测试证明了错误:边界框的中心不是旋转不变的:
那么,我怎样才能找到真实的几何中心来计算物体极值并旋转它?以下是我试图实现的一些例子 - 请注意我使用PIXELS而不是矢量数据:
答案 0 :(得分:2)
谷歌如何计算质心但我宁愿直接接近你的主要目标:
计算OBB(定向边界框)
有更多的方法。有些人正在使用PCA或Eigen Vectors,我这样做:
可以应用于矢量和光栅输入。
旋转,使OBB主轴与垂直轴对齐
因此,您可以直接从 OBB 计算angle
,只需在较大的 OBB 侧向量上使用atan2
即可。并按90-angle
CCW 进行轮播(如果您的坐标系x+
向右移动,y+
上升,angle
从{{1}增加 CCW 方向的轴}。
答案 1 :(得分:2)
cv2.minAreaRect 可能正是您所寻求的。有关使用它的示例,请参阅here。
答案 2 :(得分:0)
为了找到宽度最小的边界框,我建议采取以下步骤:
Step1 :对于所有角度,用它的凸包替换对象,因为它们具有相同的边界框。
关于方框的观察:在最小宽度的方框中,左侧或右侧垂直方框线必须与船体的一个线段对齐 - 否则,您将通过旋转方式获得较小宽度的方框线。几度。因此,这限制了凸包线段的方向。
Step2 :对于所有这些方向,计算船体所有点的最小和最大旋转x值,给出宽度。
答案 3 :(得分:0)
Ralf Kleberhoff's answer is almost right, but it needs a twist.
Algorithm:
Proof: The circle whose center is the midpoint of the maximal diameter of the convex hull is the circle with minimal area encompassing the entire object. By contradiction, if there existed a smaller circle, its diameter's length would be smaller than the maximal diameter, which implies that at least one of the extrema of the maximal diameter would be outside of such a circle.