简体   繁体   中英

How to detect line in image with OpenCV in Java

I have code from tutorial and I want to detect straight lines in image. I have this code, but for 1 line HoughLinesP generates hundreds of points instead 2 points (start point and end point - [x1,y1] and [x2, y2]). Even if I use empty image, I get 99 points... Thank you all very much.

    Mat src = Highgui.imread("C:/Users/Daniel/Desktop/line.png",Highgui.CV_LOAD_IMAGE_COLOR);
    Mat lines = new Mat();
    Mat grey = new Mat();

    Imgproc.cvtColor(m, grey, Imgproc.COLOR_BGR2GRAY);
    Imgproc.HoughLinesP(grey, lines, 1, Math.PI / 180, 50, 50, 10);

    for (int x = 0; x < lines.cols(); x++) 
      {
            double[] vec = lines.get(0, x);
            double x1 = vec[0], 
                   y1 = vec[1],
                   x2 = vec[2],
                   y2 = vec[3];
            Point start = new Point(x1, y1);
            Point end = new Point(x2, y2);

            Core.line(grey, start, end, new Scalar(100,100,100), 1);
      }

      //just show image
      new LoadImage("C:/Users/Daniel/Desktop/cdst.jpg",cdst);

      //print matrix of points of all lines - here I get a lot of points for 1 line
      System.out.println(lines.dump());

Although you have not put an example image and not described the question fully, I will try to answer it because I know what the problem might be (ofcourse I cannot tell for sure until I have full details in your question).

The problem is that you are trying to extract lines from an image which looks kindof like this: 在此处输入图片说明

The HoughLinesP expects a binary image (possibly an output from an edge detection algorithm), where the lines are represented in white (or 1 in a binary image) and background is represented in black(or 0). The problem is that your images possibly has the opposite representation, which is what makes the function give too many output lines. What you want instead is something like this: 在此处输入图片说明

Here is C++ code using HoughLinesP to get the equation of line.

#include <iostream>
#include <cv.h>
#include <highgui.h>



int main()
{
    cv::Mat inImage = cv::imread("savedPng.png");

    cv::Mat ginImage;
    cv::cvtColor(inImage, ginImage, CV_BGR2GRAY);

    cv::vector<cv::Vec4i> lines;
    cv::HoughLinesP(ginImage, lines, 1, CV_PI/180, 50, 50, 10 );

    for( size_t i = 0; i < lines.size(); i++ )
    {
        cv::Vec4i l = lines[i];
        std::cout << "( " << l[0]<< ", " << l[1]<< std::endl;
        std::cout << "( " << l[2]<< ", " << l[3] << std::endl;
        std::cout << "***********"<< std::endl;

    }
     return 1;
}

Here is the output:

( 103, 294
( 600, 41
***********
( 105, 294
( 601, 42
***********
( 102, 294
( 600, 40
***********
( 112, 292
( 601, 43
***********
( 105, 291
( 416, 133
***********
( 445, 123
( 601, 44
***********

The output image showing the detected line: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM