Ask Your Question
1

find if line and polygon are intersecting in java opencv

asked 2016-06-22 20:50:25 -0500

Indira gravatar image

updated 2017-12-26 10:56:00 -0500

I need help in opencv java. Is there any java api that finds if a line and polygon are intersecting?

It would be very helpful if anyone could answer this asap. thanks in Advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-06-22 22:38:20 -0500

all the helper functions to calculate intersection point of two lines are exist in min_enclosing_triangle.cpp

but unfortunately you should implement all of them in JAVA

to determine if a line and polygon are intersecting you could iterate all points of polygon as pairs and test if any point pairs (line) is intersecting with the line ( sorry for my poor english. feel free to ask if I did not express well )

image description

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

using namespace cv;
using namespace std;

#define EPSILON 1E-5

static bool almostEqual(double number1, double number2);
static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b);


//! Compute the distance between two points
/*! Compute the Euclidean distance between two points
*
* @Param a Point a
* @Param b Point b
*/
static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b)
{
    double xDiff = a.x - b.x;
    double yDiff = a.y - b.y;

    return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

//! Check if one point lies between two other points
/*!
* @Param point             Point lying possibly outside the line segment
* @Param lineSegmentStart  First point determining the line segment
* @Param lineSegmentEnd    Second point determining the line segment
*/
static bool isPointOnLineSegment(const cv::Point2f &point, const cv::Point2f &lineSegmentStart,
                                 const cv::Point2f &lineSegmentEnd)
{
    double d1 = distanceBtwPoints(point, lineSegmentStart);
    double d2 = distanceBtwPoints(point, lineSegmentEnd);
    double lineSegmentLength = distanceBtwPoints(lineSegmentStart, lineSegmentEnd);

    return (almostEqual(d1 + d2, lineSegmentLength));
}

//! 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 ...
(more)
edit flag offensive delete link more

Comments

see documentation

Parameters
src single-channel array.
sturkmen gravatar imagesturkmen ( 2016-06-24 16:14:37 -0500 )edit

Thank you. Solved it. but not based on contours. Using countNonZero. this time i drew poligon and line on blank image and found count.

Indira gravatar imageIndira ( 2016-06-24 17:18:27 -0500 )edit

you could post your solution as an answer.

sturkmen gravatar imagesturkmen ( 2016-06-25 18:00:21 -0500 )edit
1

answered 2016-06-27 07:54:36 -0500

Indira gravatar image
    // Draw white colored line on black pixel Image for the given slot
    Mat lineFrame=blackImage.clone();
    Imgproc.line(lineFrame, start, end, new Scalar(255,255,255), 4);  

    // Draws white colored detected contour of moving loco on black pixel image
    Mat contourFrame=blackImage.clone();
    Imgproc.drawContours(contourFrame, array, array.indexOf(matImage), new Scalar(255, 255, 255));   

    // BitWise_and of line and contour gives the intersection points
    Mat result= new Mat();
    Core.bitwise_and(lineFrame, contourFrame, result);

    // Counts num of non zero pixels (intersection points)
    int count=Core.countNonZero(result);
    if(count>0){
        System.out.println("Intersected "+count);
        return true;
    }
    else{
        return false;
    }
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-06-22 20:50:25 -0500

Seen: 1,189 times

Last updated: Jun 27 '16