いまの計算の軸に沿ったバウンディングボックスの楕円?
質問
場合に、主軸の楕円は、垂直または水平で、簡単に計算のバウンディングボックスが、どのようにつき、楕円の回転?
みんなのこれまでは計算のポイント周囲のmax/min xとyの値です。このように思わせるのが簡単になります。
がある関数の数学的意味での)を記述する楕円を任意の角度で、使ってみましたその誘導体の点を見いの斜面はゼロまたは未定義できないようです。
編集:めさんありがとうございます。軸に沿ったバウンディングボックス、すなわちすべきではない回転させる楕円が滞在を揃えて、x軸での変革のバウンディングボックスになります。
解決
きは受け付けていませんparametrized方程式、楕円の回転を任意の角度:
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)semimajor軸とsemiminor b軸、回転させて角度φ.
きの差別化と解決のための勾配=0:
0 = dx/dt = -a*sin(t)*cos(phi) - b*cos(t)*sin(phi)
=>
tan(t) = -b*tan(phi)/a [3]
なるべく多様なソリューションt(るい)、プラグイン後に[1]おmaxとmin 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の回転によるΠ/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としてポイント2次元空間です。
ここでは方程式x(t)=ellipse_equation(t)およびy(t)=ellipse_equation(t)取得し、血ので、問題を解決しています。以来、今は楕円と三角法に基づいてるのです。すべ終わる方程式のいずれかを取得します根によatan,またはasin.
ヒント:確認コードしてみでunrotated楕円:きのルーツを0,Pi/2,Pi-3-Pi/2になります。
それを各軸移動量(x,y).お得なツ(以下の場合楕円は退化などの半径はゼロ).Evalulateの位置のルーツ、すべての意します。
まだほとんどあります。の境界のボックスは、楕円は簡単にスキャンこれら四つのポイント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;
BrilianヨハンNilsson.私は転写コード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からの原点として式になります。
がh,k)は、センター、およびbのサイズを大き、短軸およびt変化につ。
から、対応することができるでしょう求めるt dx/dtやdy/dtが0になります。
この式の場合は楕円より 巣や偏心 (場合による軸の長さ、センターの角度。g.その答えによるuser1789690).
すなわち、場合の焦点は、(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ヨハンNilsson.
すOpenCV/C++および利用 cv::fitEllipse(..)
機能ですが、バウンディングrectのきます。ここにしたソリューションズ-リサーチの答え:
// 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した上で実施した利用いただけます:standardとexpress。私としては、この調査を実施しているのは、同じ伝えています。私は一日の検索アルゴリズムまたは一部のコードを検てのエリアを表示一部動作しなかった、とても嬉しかった最後のコードです。私は誰かがこの便利なのです。このコードを計算し、そのバウンディングボックスの回転させます。バウンディングボックス軸に揃っていない回転します。の身の楕円でした転させます。
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 rectポイントのための実施
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;
}