java, I would like to create a program that detects intersections from multiple straight lines and recognizes parts where more intersections are detected.

What i am doing now

Currently, the captured image is Canny processed and Hough transformed,
A straight line was detected.
Below is the code.

public class HoughClass {
    static {
        System.loadLibrary (Core.NATIVE_LIBRARY_NAME);
    public static void main (String [] args) {
        final String path_in = "wall02.jpg";
        final String path_grayout = "wall02_gray.png";
        final String path_cannyout = "wall02_canny.png";
        final String path_houghout = "wall02_hough.png";
        final String path_houghPout = "wall02_houghP.png";
        Mat src = new Mat ();// SourceMat
        Mat cdst = new Mat ();// CannyDestinationMat
        Mat cdstP = new Mat ();// CannyColorDestinationMat
        Mat gray = new Mat ();// GrayMat
        Mat canny = new Mat ();// CannyEdgeMat
        Mat lines = new Mat ();// HoughlineMat
        Mat linesP = new Mat ();// HoughlinePMat
        src = Imgcodecs.imread (path_in);// read image
        // Check Input-Image
        if (src.empty ()) {
            System.out.println ("Error opening image!");
            System.exit (-1);
        // ToGray
        Imgproc.cvtColor (src, gray, Imgproc.COLOR_RGB2GRAY);
        // Edge detection
        Imgproc.Canny (gray, canny, 10, 50);
        // Copy edges to the images that will display the results in BGR
        Imgproc.cvtColor (canny, cdst, Imgproc.COLOR_GRAY2BGR);
        cdstP = cdst.clone ();
        // Standard Hough Line Transform
        Imgproc.HoughLines (canny, lines, 1, Math.PI/360, 150);// runs the actual detection
        // Draw the lines
        for (int x = 0;x<lines.rows ();x ++) {
            double rho = lines.get (x, 0) [0],
                   theta = lines.get (x, 0) [1];
            double a = Math.cos (theta), b = Math.sin (theta);
            double x0 = a * rho, y0 = b * rho;
            Point pt1 = new Point (Math.round (x0 + 1000 * (-b)), Math.round (y0 + 1000 * (a))));
            Point pt2 = new Point (Math.round (x0-1000 * (-b)), Math.round (y0-1000 * (a))));
            Imgproc.line (cdst, pt1, pt2, new Scalar (0, 0, 255), 1, Imgproc.LINE_AA, 0);
        // Probabilistic Line Transform
        Imgproc.HoughLinesP (canny, linesP, 1, Math.PI/360, 50, 60, 10);// runs the actual detection
        // Draw the lines
        for (int x = 0;x<linesP.rows ();x ++) {
                double [] l = linesP.get (x, 0);
                Imgproc.line (cdstP, new Point (l [0], l [1]), new Point (l [2], l [3]), new Scalar (0, 0, 255), 1, Imgproc.LINE_AA, 0);
        Imgcodecs.imwrite (path_in, src);
        Imgcodecs.imwrite (path_grayout, gray);
        Imgcodecs.imwrite (path_cannyout, canny);
        Imgcodecs.imwrite (path_houghout, cdst);
        Imgcodecs.imwrite (path_houghPout, cdstP);
Execution results

Top: Gray image, Bottom: Hough transform


Multiple lines were detected, but no intersection point has been detected.
I checked and found that there was an article that asked for the intersection of two straight lines.
What should I do for multiple straight lines?
Please professor.

  • Answer # 1

    The approach seems to be different depending on the purpose of the process,
    Imagine that it might be troublesome if multiple lines appear like the red line in the image (because it depends on the process, it seems to directly affect "weight"), so
    I think it is necessary to make a judgment such as "I don't think about the intersection of these two lines because they are close to this line."
    (It may be possible to devise a method that suppresses multiple occurrences from the beginning to some extent by reducing the resolution of r in the Hough space.)

    ... is a way to find the location where multiple lines cross, but for example ...

    When the intersection of two lines is calculated in a brute force manner that others have answered, but ignoring pairs of lines that are "close to each other" [ Count the number of lines that are within the threshold and are not close to either of the two lines for which the intersection was calculated, and the result is the intersection with the largest number.

    I think a method like this can be considered.

  • Answer # 2


    What should I do for multiple lines?

    I think it can be obtained with an array of straight lines, so I think that there is no choice but to find the intersection point for each combination.

    If you write it roughly,


    for (int x = 0;x

    is a double loop,

    for (int x = 0;x