2

Already using HoughLinesP for detecting lines in the image, but the issue is it detects only straight, clean lines and misses out rest

here's the code

int threshold = 80;
double minLineSize = 100;
double lineGap = 10;
double theta=Math.PI / 180;

Imgproc.HoughLinesP(edges, lines, 1, theta, threshold, minLineSize, lineGap);

when it is applied on the below image

https://i.stack.imgur.com/2HFFY.png

The lines identified can be seen as below

https://i.stack.imgur.com/loc4k.png

As you can see the actual lines(Document boundary) I wanted to detect are being skipped.

Steps I followed

  • Changed to grayscale
  • Applied canny edge detection
  • findContour
  • If contour with appropriate area is not found, trying to find large lines & build the boundary manually
  • Issue i'm facing is that the houghlinesP is not detecting required lines and hence I'm unable to design my document boundary

Please suggest any other method to detect lines, and since I will use this code as a generalized one, I will not be able to modify the threshold & other values once I have configured.

I have also referred many other similar questions in stackoverflow but most of them suggest to use HoughLines but with different parameter values.

One of the question I found similar to my issue, but no answer.

Detecting incomplete rectangles (missing corners/ short endges) in OpenCV

Dark White lines are the ones detecting by HoughLinesP method

Also find below other sample images where the lines are being detected wrongly

https://i.stack.imgur.com/fWjti.png

https://i.stack.imgur.com/1vaut.png

https://i.stack.imgur.com/KXosQ.png

Mohammed Irfan
  • 131
  • 1
  • 11
  • You are almost certainly plotting your lines wrong; you can tell as most have slopes opposing the correct slope. Can you post the code you use to plot them? Note that the output of `HoughLinesP()` gives multiple lines with the form `x1, y1, x2, y2` and not `x1, x2, y1, y2`. Further you can tell by your images that there are many line segments detected at the boundaries of the documents; you should be able to make these one contiguous line by increasing your `lineGap` (which is the maximum allowable pixel gap for two detected lines to actually be included as one line). – alkasm Jun 05 '17 at 11:24
  • @AlexanderReynolds below is my code for printing the lines `for (int k=0; k < lines.rows(); k++) { for (int l=0; l < lines.cols(); l++) { double[] vec = lines.get(k, l); double x1 = vec[0], y1 = vec[3], x2 = vec[2], y2 = vec[1]; Imgproc.line(edges, new Point(x1,y1), new Point(x2,y2), new Scalar(255,0,0), 3); } }` – Mohammed Irfan Jun 05 '17 at 11:29
  • Yeah, that's your problem. The points are stored in the order `x1, y1, x2, y2` but you're accessing them as if they're in the order `x1, y2, x2, y1`. You need to swap them around. `double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3];` – alkasm Jun 05 '17 at 11:31
  • use LSD (line segment detector) from openCV 3 on the grayscale image.. – Micka Jun 05 '17 at 11:40
  • Thank you, I modified & tested. Reason why I swapped the indices is because for some image it was mapping wrong indices. And I was not aware how the vector will be returned. Now, If I just increase the gap, will it identify curved large splitted line as a single line – Mohammed Irfan Jun 05 '17 at 11:42
  • @MohammedIrfan no, it will not. Can you edit your post to include what one of the original images look like before Canny? Is there a contrasting background behind the pages or not much? The answer on [this thread](https://stackoverflow.com/questions/44047819/python-opencv-increase-brightness-but-overflow/44049669#44049669) may at least help you remove lines generated by the inside of the document, though, and with that you may not need to use Hough at all (or you can simply make the min line length shorter so that you at least get multiple short segments along the curvy boundaries). – alkasm Jun 05 '17 at 11:46
  • @AlexanderReynolds Yes, it contains contrasting background to the object I want to identify & hence the reason I was trying to find out only lines if contours are not found, but with your tweak i'm able to identify large lines & even I increased the linegap to merge line segments. How can I upvote your answer – Mohammed Irfan Jun 06 '17 at 12:30

0 Answers0