How to find the vertex of a square by hough line transform?

60 Views Asked by At

enter image description here

The image on the left is the original image, and the right is the image detected through houghlineP. What I want is to find the coordinates of each vertex of Sudoku through the information on the right, as shown in the figure below.

I don't know what else to do to detect vertices after houghlineP.

enter image description here

def hough_line_segments(src) :
    src = cv2.resize(src, (800, 800))
    edge = cv2.Canny(src, 75, 200)
    lines = cv2.HoughLinesP(edge, rho=5, theta=math.pi / 180, threshold=250, minLineLength=30, maxLineGap=30)
    dst = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR)
    
    red_lines = np.zeros([src.shape[0], src.shape[1], 3], dtype=np.uint8)
    print(lines)
    gradients = []
    if lines is not None:
        for i in range(lines.shape[0]):
            pt1 = (lines[i][0][0], lines[i][0][1])
            pt2 = (lines[i][0][2], lines[i][0][3])
            gradients.append(calculate_slope(pt1[0], pt1[1], pt2[0], pt2[1]))
            # cv2.line(dst, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)
            cv2.line(red_lines, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)
        group1, group2 = divide_numbers_by_average(gradients)
        for i in range(lines.shape[0]):
            if gradients in group1:
                cv2.line(dst, pt1, pt2, (0, 255, 0), 2, cv2.LINE_AA)  # green line
            elif gradients in group2:
                cv2.line(dst, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)  # red line
    cv2.imshow('src', src)
    cv2.imshow('dst', dst)
    cv2.imshow('red_line', red_lines)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

src = cv2.imread('../img/board6.png', cv2.IMREAD_GRAYSCALE)

hough_line_segments(src)

0

There are 0 best solutions below