2

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());
5
  • Don't use edge detection if you don't need it! If your image is already a straight line, just pass it to Hough (converted to gray) Commented Oct 14, 2016 at 14:57
  • I tried it before but it gives to me weirdly results. So I thought I need Canny algorithm. If I use code without Canny algorithm I get a lot of points. If I show at new image with detected lines, I see image with random lines. For example, I have image with 1 line, just for testing. Without Canny it generates hundreds of points... Commented Oct 14, 2016 at 16:50
  • Start with generating the intermediate outputs at each steps. Next, upload the input images, intermediate images as well, so other users are well aware of the problem. Second, write analytically. Dont try to summarize everything in one passage. Community will take that as a good sign that the question putter has put in enough work. Commented Oct 14, 2016 at 16:59
  • Thank you. Problem is that HoughLinesP finds out too much lines. But there is just one line... So I can't write some outputs at each steps. If I use Canny, HoughLinesP generates to me nice lines - but edges. Thank you, i'm really grateful. Commented Oct 14, 2016 at 17:20
  • Can you add the image you are using to detect a line? Commented Oct 17, 2016 at 1:10

1 Answer 1

5

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: enter image description here

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: enter image description here

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: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

If you downvote, please also take sometime to write what is it that can be improved about this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.