Ask Your Question
0

How to find intersection point and the distance for ray line segment intersection in OpenCV? [closed]

asked 2018-12-21 23:49:56 -0600

syaifulnizamyahya gravatar image

updated 2018-12-21 23:50:34 -0600

I have 4 lines segment, A, B, C and D. Each line is represented as two points. Eg. line A is represented as point A1 and point A2.

image description

What I want is

  1. point X, which is the point where line A ray intersect with line B
  2. distance between X and A1 or A2(either one is fine).

When testing for intersection, line A ray should not

  1. intersect with line segment D
  2. intersect with line segment C

How do I do this?

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by LBerger
close date 2018-12-22 02:51:18.236868

Comments

2
berak gravatar imageberak ( 2018-12-22 01:50:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-12-22 02:55:10 -0600

there is some (internal) helper functions in min_enclosing_triangle.cpp

image description

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

#define EPSILON 1E-5

//! Return the maximum of the provided numbers
static double maximum(double number1, double number2, double number3) {
    return std::max(std::max(number1, number2), number3);
}

//! Check if the two numbers are equal (almost)
/*!
* The expression for determining if two real numbers are equal is:
* if (Abs(x - y) <= EPSILON * Max(1.0f, Abs(x), Abs(y))).
*
* @Param number1 First number
* @Param number2 Second number
*/
static bool almostEqual(double number1, double number2) {
    return (std::abs(number1 - number2) <= (EPSILON * maximum(1.0, std::abs(number1), std::abs(number2))));
}

//! Determine the intersection point of two lines, if this point exists
/*! Two lines intersect if they are not parallel (Parallel lines intersect at
* +/- infinity, but we do not consider this case here).
*
* The lines are specified by a pair of points each. If they intersect, then
* the function returns true, else it returns false.
*
* Lines can be specified in the following form:
*      A1x + B1x = C1
*      A2x + B2x = C2
*
* If det (= A1*B2 - A2*B1) == 0, then lines are parallel
*                                else they intersect
*
* If they intersect, then let us denote the intersection point with P(x, y) where:
*      x = (C1*B2 - C2*B1) / (det)
*      y = (C2*A1 - C1*A2) / (det)
*
* @Param a1 First point for determining the first line
* @Param b1 Second point for determining the first line
* @Param a2 First point for determining the second line
* @Param b2 Second point for determining the second line
* @Param intersection The intersection point, if this point exists
*/
static bool lineIntersection(const cv::Point2f &a1, const cv::Point2f &b1, const cv::Point2f &a2,
    const cv::Point2f &b2, cv::Point2f &intersection) {
    double A1 = b1.y - a1.y;
    double B1 = a1.x - b1.x;
    double C1 = (a1.x * A1) + (a1.y * B1);

    double A2 = b2.y - a2.y;
    double B2 = a2.x - b2.x;
    double C2 = (a2.x * A2) + (a2.y * B2);

    double det = (A1 * B2) - (A2 * B1);

    if (!almostEqual(det, 0)) {
        intersection.x = static_cast<float>(((C1 * B2) - (C2 * B1)) / (det));
        intersection.y = static_cast<float>(((C2 * A1) - (C1 * A2)) / (det));

        return true;
    }

    return false;
}


int main(int argc, char** argv)
{
    Mat img(400, 400, CV_8UC3, Scalar(127, 127, 127));

    Point2f p1(100,130), p2(150,200), p3(300,100), p4(200,300), intersection_point;


    line(img, p1, p2, Scalar(0, 255, 0), 2);
    line(img, p3, p4, Scalar(0, 255, 0), 2);

    lineIntersection(p1, p2, p3, p4, intersection_point);

    circle(img, intersection_point, 2, Scalar(0, 0, 255), 2);

    circle(img, p1, 2, Scalar(255, 0, 0), 2);
    circle(img, p2, 2, Scalar(255, 0, 0), 2);
    circle(img, p3, 2, Scalar(255, 0, 0), 2);
    circle(img, p4, 2, Scalar(255, 0, 0), 2);

    imshow("Line Intersection Demo", img);
    waitKey(0);

    return 0;
}
edit flag offensive delete link more

Comments

nice find ;)

berak gravatar imageberak ( 2018-12-22 02:59:41 -0600 )edit
1

Now wait for a new question : how to know if the intersection belong to a segment? and duplicate post

LBerger gravatar imageLBerger ( 2018-12-22 03:07:47 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-21 23:49:56 -0600

Seen: 2,278 times

Last updated: Dec 22 '18