Trouble with appropriate houghlines parametrization?

Viewed 70

I am trying to identify a set of lines in my figure, but I am having trouble trying to figure out what appropriate parameters I should choose for the hough transform.

img = 255 - cv2.imread('isolate.png', 0)
blank = np.zeros(img.shape) + 255
dilation = cv2.dilate(img, np.ones((2,2)), iterations = 1)
processed = cv2.bitwise_not(dilation)
cv2.imwrite('lol.png', processed)
# cv2.imwrite('process.png',dilation)
lines = cv2.HoughLinesP(processed,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 180,maxLineGap = 1)
for line in lines:
    # import pdb; pdb.set_trace()
    x1, y1, x2, y2 = line[0]
    cv2.line(processed, (x1, y1), (x2, y2), (255, 0, 0), 1)
cv2.imwrite("result.png", processed)

The image that is passed into HoughLinesP looks like this - enter image description here The image that I get after drawing is this - enter image description here

1 Answers

Do not invert the image after reading it:

img = cv2.imread(test_image_filepath, cv2.IMREAD_GRAYSCALE)

But invert the processed. Since the source image is a gray scale image you have to transform the image to BGR before drawing the blue lines on the image:

processed = cv2.cvtColor(255-processed, cv2.COLOR_GRAY2BGR)

All together:

img = cv2.imread('isolate.png', cv2.IMREAD_GRAYSCALE)

dilation = cv2.dilate(img, np.ones((2,2)), iterations = 1)
processed = cv2.bitwise_not(dilation)

lines = cv2.HoughLinesP(processed,
    rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 180,maxLineGap = 1)

processed = cv2.cvtColor(255-processed, cv2.COLOR_GRAY2BGR)
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(processed, (x1, y1), (x2, y2), (255, 0, 0), 1)
Related