首页 > 解决方案 > OpenCV: Merging fitted shapes

问题描述

If I'm using OpenCV (Python) and fit two shapes, like so:

a = cv2.fitEllipse(contours)
b = cv2.minAreaRect(contours)

Both a and b are represented as Box2D objects, which look something like:

center: (x, y)
size: (width, height)
rotation: angle

a and b are often going to be fairly similar, but not exact due to different fit methods. How can I find the shape (ellipse) that is the "average" of a and b? That is, the ellipse that is the best fit between a and b?

标签: pythonopencvcomputer-vision

解决方案


“平均”的定义可能不明确,并为您提供不同的解决方案。如果这对您来说并不重要,因为您只想以任何方式平均形状,下面是如何平均两个矩形的简单解决方案:

表示两个矩形(a1, a2, a3, a4)(b1, b2, b3, b4)。假设我们要找到矩形(x1, x2, x3, x4)最小化

<code>sum |xi - ai|^2 + sum |xi - bi|^2</code>

一个矩形被参数化为

<code>x_i = R q_i + c</code>

其中R是 2D 旋转矩阵,是q_i矩形大小未知+-w,+-h的矩阵之一。是未知的矩形中心。whp

整个公式形成一个二次规划问题,具有未知数w, h, R, p和二次约束R的是一个旋转矩阵。


推荐阅读