簡體   English   中英

如何在OpenCV中最好計算光線線段的交點? 並獲得其交點和距原點的距離?

[英]How to calculate ray-line segment intersection preferably in OpenCV? And get its intersection points and distance from origin?

我有4條線段,A,B,C和D。每條線表示為兩個點。 例如。 線A表示為點A1和點A2。

在此處輸入圖片說明

我想要的是

  1. X點,A線與B線相交的點
  2. X和A1之間的距離(原點)

測試相交時,線A射線不應

  1. 與線段D相交
  2. 與線段C相交

我該怎么做呢?

終於讓它可以在OpenCV C ++上運行。 基於此https://stackoverflow.com/a/32146853/457030

// return the distance of ray origin to intersection point
double GetRayToLineSegmentIntersection(Point2f rayOrigin, Point2f rayDirection, Point2f point1, Point2f point2)
{
    Point2f v1 = rayOrigin - point1;
    Point2f v2 = point2 - point1;
    Point2f v3 = Point2f(-rayDirection.y, rayDirection.x);

    float dot = v2.dot(v3);
    if (abs(dot) < 0.000001)
        return -1.0f;

    float t1 = v2.cross(v1) / dot;
    float t2 = v1.dot(v3) / dot;

    if (t1 >= 0.0 && (t2 >= 0.0 && t2 <= 1.0))
        return t1;

    return -1.0f;
}

// use this to normalize rayDirection
Point2f NormalizeVector(Point2f pt)
{
    float length = sqrt(pt.x*pt.x + pt.y*pt.y);
    pt = pt / length;
    return pt;
}

// gets the intersection point
Point2f GetRayIntersectionPoint(Point2f origin, Point2f vector, double distance)
{
    Point2f pt;

    pt.x = origin.x + vector.x * distance;
    pt.y = origin.y + vector.y * distance;

    return pt;
}

應該自我解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM