如果椭圆的长轴是垂直或水平的,那么计算边界框很容易,但是当椭圆旋转时呢?

到目前为止我能想到的唯一方法是计算周长周围的所有点并找到最大/最小 x 和 y 值。看来应该有更简单的方法。

如果有一个函数(在数学意义上)描述任意角度的椭圆,那么我可以使用它的导数来找到斜率为零或未定义的点,但我似乎找不到一个。

编辑:为了澄清,我需要轴对齐的边界框,即它不应随椭圆旋转,而应与 x 轴保持对齐,因此变换边界框将不起作用。

有帮助吗?

解决方案

您可以尝试对以任意角度旋转的椭圆使用参数化方程:

x = h + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)  [1]
y = k + b*sin(t)*cos(phi) + a*cos(t)*sin(phi)  [2]

...其中椭圆具有中心 (h,k) 长半轴 a 和短半轴 b,并旋转角度 phi。

然后您可以微分并求解梯度 = 0:

0 = dx/dt = -a*sin(t)*cos(phi) - b*cos(t)*sin(phi)

=>

tan(t) = -b*tan(phi)/a   [3]

这将为您提供许多 t 的解决方案(您感兴趣的其中两个),将其重新插入 [1] 以获得您的最大值和最小值 x。

重复[2]:

0 = dy/dt = b*cos(t)*cos(phi) - a*sin(t)*sin(phi)

=>

tan(t) = b*cot(phi)/a  [4]

让我们尝试一个例子:

考虑 (0,0) 处的椭圆,a=2,b=1,旋转 PI/4:

[1] =>

x = 2*cos(t)*cos(PI/4) - sin(t)*sin(PI/4)

[3] =>

tan(t) = -tan(PI/4)/2 = -1/2

=>

t = -0.4636 + n*PI

我们感兴趣的是 t = -0.4636 和 t = -3.6052

所以我们得到:

x = 2*cos(-0.4636)*cos(PI/4) - sin(-0.4636)*sin(PI/4) = 1.5811

x = 2*cos(-3.6052)*cos(PI/4) - sin(-3.6052)*sin(PI/4) = -1.5811

其他提示

我发现了一个简单的公式 http://www.iquilezles.org/www/articles/ellipses/ellipses.htm (并忽略 z 轴)。

我大致是这样实现的:

num ux = ellipse.r1 * cos(ellipse.phi);
num uy = ellipse.r1 * sin(ellipse.phi);
num vx = ellipse.r2 * cos(ellipse.phi+PI/2);
num vy = ellipse.r2 * sin(ellipse.phi+PI/2);

num bbox_halfwidth = sqrt(ux*ux + vx*vx);
num bbox_halfheight = sqrt(uy*uy + vy*vy); 

Point bbox_ul_corner = new Point(ellipse.center.x - bbox_halfwidth, 
                                 ellipse.center.y - bbox_halfheight);

Point bbox_br_corner = new Point(ellipse.center.x + bbox_halfwidth, 
                                 ellipse.center.y + bbox_halfheight);

这相对简单,但有点难以解释,因为您没有向我们提供表示椭圆的方式。有很多方法可以做到..

无论如何,一般原则是这样的:您无法直接计算轴对齐的边界框。但是,您可以将 x 和 y 中椭圆的极值计算为二维空间中的点。

为此,采用方程 x(t) = ellipse_equation(t) 和 y(t) = ellipse_equation(t) 就足够了。得到它的一阶导数并求解它的根。因为我们处理的是基于三角学的椭圆,所以很简单。您最终应该得到一个方程,该方程可以通过 atan、acos 或 asin 求根。

暗示:要检查您的代码,请尝试使用未旋转的椭圆:您应该在 0、Pi/2、Pi 和 3*Pi/2 处求根。

对每个轴(x 和 y)执行此操作。你最多会得到四个根(如果你的椭圆是退化的,则更少,例如其中一个半径为零)。评估根部的位置,即可得到椭圆的所有极值点。

现在你就快到了。获取椭圆的边界框就像扫描这四个点的 xmin、xmax、ymin 和 ymax 一样简单。

顺便说一句 - 如果您在找到椭圆方程时遇到问题:尝试将其简化为这样的情况:您有一个轴对齐的椭圆,有一个中心、两个半径和一个绕中心的旋转角度。

如果这样做,方程将变为:

  // the ellipse unrotated:
  temp_x (t) = radius.x * cos(t);
  temp_y (t) = radius.y = sin(t);

  // the ellipse with rotation applied:
  x(t) = temp_x(t) * cos(angle) - temp_y(t) * sin(angle) + center.x;
  y(t) = temp_x(t) * sin(angle) + temp_y(t) * cos(angle) + center.y;

布里利安·约翰·尼尔森。我已将您的代码转录为 c# - ellipseAngle 现在以度为单位:

private static RectangleF EllipseBoundingBox(int ellipseCenterX, int ellipseCenterY, int ellipseRadiusX, int ellipseRadiusY, double ellipseAngle)
{
    double angle = ellipseAngle * Math.PI / 180;
    double a = ellipseRadiusX * Math.Cos(angle);
    double b = ellipseRadiusY * Math.Sin(angle);
    double c = ellipseRadiusX * Math.Sin(angle);
    double d = ellipseRadiusY * Math.Cos(angle);
    double width = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2)) * 2;
    double height = Math.Sqrt(Math.Pow(c, 2) + Math.Pow(d, 2)) * 2;
    var x= ellipseCenterX - width * 0.5;
    var y= ellipseCenterY + height * 0.5;
    return new Rectangle((int)x, (int)y, (int)width, (int)height);
}

我认为最有用的公式是这个。从原点旋转 phi 角度的省略号具有以下方程:

alt text

alt text

其中 (h,k) 是中心,a 和 b 是长轴和短轴的大小,t 从 -pi 到 pi 变化。

由此,您应该能够推导出 t dx/dt 或 dy/dt 变为 0 的情况。

如果椭圆由其给出,则以下是该情况的公式 焦点和偏心率 (对于由轴长度、中心和角度给出的情况,请参见 e.G。用户1789690的回答)。

即,如果焦点为 (x0, y0) 和 (x1, y1),偏心率为 e,则

bbox_halfwidth  = sqrt(k2*dx2 + (k2-1)*dy2)/2
bbox_halfheight = sqrt((k2-1)*dx2 + k2*dy2)/2

在哪里

dx = x1-x0
dy = y1-y0
dx2 = dx*dx
dy2 = dy*dy
k2 = 1.0/(e*e)

我从 user1789690 和 Johan Nilsson 的答案中得出了公式。

如果您使用 OpenCV/C++ 并使用 cv::fitEllipse(..) 函数,您可能需要椭圆的边界矩形。在这里我使用迈克的答案提出了一个解决方案:

// tau = 2 * pi, see tau manifest
const double TAU = 2 * std::acos(-1);

cv::Rect calcEllipseBoundingBox(const cv::RotatedRect &anEllipse)
{
    if (std::fmod(std::abs(anEllipse.angle), 90.0) <= 0.01) {
        return anEllipse.boundingRect();
    }

    double phi   = anEllipse.angle * TAU / 360;
    double major = anEllipse.size.width  / 2.0;
    double minor = anEllipse.size.height / 2.0;

    if (minor > major) {
        std::swap(minor, major);
        phi += TAU / 4;
    }

    double cosPhi = std::cos(phi), sinPhi = std::sin(phi);
    double tanPhi = sinPhi / cosPhi;

    double tx = std::atan(-minor * tanPhi / major);
    cv::Vec2d eqx{ major * cosPhi, - minor * sinPhi };
    double x1 = eqx.dot({ std::cos(tx),           std::sin(tx)           });
    double x2 = eqx.dot({ std::cos(tx + TAU / 2), std::sin(tx + TAU / 2) });

    double ty = std::atan(minor / (major * tanPhi));
    cv::Vec2d eqy{ major * sinPhi, minor * cosPhi };
    double y1 = eqy.dot({ std::cos(ty),           std::sin(ty)           });
    double y2 = eqy.dot({ std::cos(ty + TAU / 2), std::sin(ty + TAU / 2) });

    cv::Rect_<float> bb{
        cv::Point2f(std::min(x1, x2), std::min(y1, y2)),
        cv::Point2f(std::max(x1, x2), std::max(y1, y2))
    };

    return bb + anEllipse.center;
}

该代码基于上面 user1789690 贡献的代码,但在 Delphi 中实现。我已经对此进行了测试,据我所知它工作得很好。我花了一整天的时间寻找一个算法或一些代码,测试了一些不起作用的东西,我很高兴终于找到了上面的代码。我希望有人觉得这很有用。此代码将计算旋转椭圆的边界框。边界框与椭圆轴对齐且不旋转。半径是椭圆旋转之前的半径。

type

  TSingleRect = record
    X:      Single;
    Y:      Single;
    Width:  Single;
    Height: Single;
  end;

function GetBoundingBoxForRotatedEllipse(EllipseCenterX, EllipseCenterY, EllipseRadiusX,  EllipseRadiusY, EllipseAngle: Single): TSingleRect;
var
  a: Single;
  b: Single;
  c: Single;
  d: Single;
begin
  a := EllipseRadiusX * Cos(EllipseAngle);
  b := EllipseRadiusY * Sin(EllipseAngle);
  c := EllipseRadiusX * Sin(EllipseAngle);
  d := EllipseRadiusY * Cos(EllipseAngle);
  Result.Width  := Hypot(a, b) * 2;
  Result.Height := Hypot(c, d) * 2;
  Result.X      := EllipseCenterX - Result.Width * 0.5;
  Result.Y      := EllipseCenterY - Result.Height * 0.5;
end;

这是我的函数,用于找到任意方向的椭圆的紧配合矩形

我有 opencv 矩形和实施点:

cg——椭圆的中心

尺寸 - 椭圆的长轴、短轴

角度 - 椭圆的方向

cv::Rect ellipse_bounding_box(const cv::Point2f &cg, const cv::Size2f &size, const float angle) {

    float a = size.width / 2;
    float b = size.height / 2;
    cv::Point pts[4];

    float phi = angle * (CV_PI / 180);
    float tan_angle = tan(phi);
    float t = atan((-b*tan_angle) / a);
    float x = cg.x + a*cos(t)*cos(phi) - b*sin(t)*sin(phi);
    float y = cg.y + b*sin(t)*cos(phi) + a*cos(t)*sin(phi);
    pts[0] = cv::Point(cvRound(x), cvRound(y));

    t = atan((b*(1 / tan(phi))) / a);
    x = cg.x + a*cos(t)*cos(phi) - b*sin(t)*sin(phi);
    y = cg.y + b*sin(t)*cos(phi) + a*cos(t)*sin(phi);
    pts[1] = cv::Point(cvRound(x), cvRound(y));

    phi += CV_PI;
    tan_angle = tan(phi);
    t = atan((-b*tan_angle) / a);
    x = cg.x + a*cos(t)*cos(phi) - b*sin(t)*sin(phi);
    y = cg.y + b*sin(t)*cos(phi) + a*cos(t)*sin(phi);
    pts[2] = cv::Point(cvRound(x), cvRound(y));

    t = atan((b*(1 / tan(phi))) / a);
    x = cg.x + a*cos(t)*cos(phi) - b*sin(t)*sin(phi);
    y = cg.y + b*sin(t)*cos(phi) + a*cos(t)*sin(phi);
    pts[3] = cv::Point(cvRound(x), cvRound(y));

    long left = 0xfffffff, top = 0xfffffff, right = 0, bottom = 0;
    for (int i = 0; i < 4; i++) {
        left = left < pts[i].x ? left : pts[i].x;
        top = top < pts[i].y ? top : pts[i].y;
        right = right > pts[i].x ? right : pts[i].x;
        bottom = bottom > pts[i].y ? bottom : pts[i].y;
    }
    cv::Rect fit_rect(left, top, (right - left) + 1, (bottom - top) + 1);
    return fit_rect;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top