2

I have an image that is a simple picture and I want to extract the end points of the lines. However, there are other lines that do overlap, so, my lines have 'gaps' in them. Binary Image of 10 lines

I am trying to use HoughLinesP to extract the parameterization of these ten lines and though the visual result is reasonable, it still gives me 43 individual lines. HoughLinesP representation

I have tried smoothing the lines, skeleton representation of the lines, redrawing the lines after each of those, I'm working with countours right now. I have adjusted my parameters (line length, max gap, threshold, etc...) and I can not get this to reduce to ten lines. In my current code, I subtract the first image from itself to make a new black space to draw my HoughLines on, maybe not the most efficient, but its effective. Here is my code:

    import numpy as np
    import cv2

    img = cv2.imread('masked.png')

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray,50,150,apertureSize = 3)

    minLineLength = 100

    img2 = cv2.subtract(img, img)

    lines = cv2.HoughLinesP(gray,1,np.pi/180,10,minLineLength,maxLineGap=100)

    print(len(lines))

    for n in range(len(lines)):
        for x1,y1,x2,y2 in lines[n]:
            cv2.line(img2,(x1,y1),(x2,y2),(0,255,0),1,4)

    cv2.imshow('result.png', img2)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

Is there another approach that would allow me to fill in these gaps and pull out ten equations of lines? I'm using Python, OpenCV and Numpy right now.

Joseph
  • 301
  • 3
  • 13
  • 1
    Yes, absolutely! You can simply use slope-intercept form `y=mx+b` to separate the lines. All of the lines should have the same slope `m` but they each have different intercepts `b`. You have two endpoints so you can calculate `m`, then plug in one of the endpoints you have to get `b` for every line. Then if the lines have a similar `b` value, you know they're the same overall line, so you can average them together. Better yet since you have 40 lines with the same slopes, you can average all their slopes together and find individual intercepts. – alkasm Jul 04 '17 at 18:11
  • I didnt think about converting from polar to cartesian. Great idea! Ill try this tonight – Joseph Jul 04 '17 at 18:33
  • Not to put too much weight on it but these *are* in Cartesian form, not polar. `HoughLinesP` simply gives lines defined by two endpoints in Cartesian coordinates. – alkasm Jul 04 '17 at 18:40
  • Ill accept that. It a set of two points, though i think it uses rho and theta to get them. ... but it is irrelevant to my need – Joseph Jul 04 '17 at 18:42
  • For clarity, the `rho, theta` form is not *polar* but *Hesse normal form* for the Hough Transform. In polar, `rho, theta` defines a point; in Hesse normal form, `rho, theta` represents a line. In any case, `HoughLines` returns lines in Hesse normal form, where `HoughLinesP` returns lines in Cartesian endpoint pairs. You did say that you tried skeletonizing and similar but did you ever apply morphological operations to simply connect the lines before running the transform? – alkasm Jul 04 '17 at 18:46
  • Just dilation and erosion, I didnt think much else looked promising – Joseph Jul 04 '17 at 18:51
  • Well dilation with an identity matrix will connect diagonal lines at the 45 degree angle yours are at. Just a thought, it really isn't necessary with `HoughLinesP` though since you can use the `maxLineGap` parameter. – alkasm Jul 04 '17 at 18:53
  • Yea and i expect to grow the algorithm to handle a wide variety of angles. – Joseph Jul 04 '17 at 18:55
  • And thank you for bringing my attention to the Hesse normal form. – Joseph Jul 04 '17 at 18:56
  • Last point I want to make is that if you're using slope-intercept form, you will have an undefined slope for vertical lines. If you stick with `HoughLines` instead, then you can separate the lines based on their `rho` distance and average their angles `theta`. It is a little slower than `HoughLinesP` though so you could stick with `HoughLinesP` and then convert them. Either way, there's a bunch of ways to deal with this and I think you've got an idea of them :) – alkasm Jul 04 '17 at 18:59
  • Fantastic. .. i just didn't consider working with them p directly. Thank you – Joseph Jul 04 '17 at 19:04

0 Answers0