0

I have an image of a goban (example below). I want to detect the grid of the playing area to create an (x, y) coordinate system of the board. To start I am trying to just detect the lines on the board:

def find_lines(filename):
    gray = cv2.imread(str(filename), cv2.IMREAD_GRAYSCALE)

    kernel_size = 5
    blur = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)

    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur, low_threshold, high_threshold)
    rho = 1  # distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 15  # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 50  # minimum number of pixels making up a line
    max_line_gap = 20  # maximum gap in pixels between connectable line segments
    lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)

    line_image = np.zeros((*gray.shape, 3))
    for line in lines:
        for x1,y1,x2,y2 in line:
            cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
    return line_image


def main(filename):
    lines = find_lines(filename)
    cv2.imwrite('lines.jpg'), lines)

if __name__ == '__main__':
    main(sys.argv[1])

This gives me a good image of the outline of the board, but I want to get just the grid. Part of the difficulty is that the grid lines have a width and HoughLinesP() is giving me the outline of these lines. Also, it is giving me multiple segments along each grid line when I want the (x, y) coordinates at the top and bottom or left and right of each grid line. The lines variable in find_lines() has dimensions (1411, 1, 4), but I only want the 38 grid lines. What can I do from here to get just the endpoints of the grid lines?

enter image description here

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    "*This gives me a good image of the outline of the board*", if I were you, I would use `cv2.PerspectiveTransform(source_coordinates, destination_coordinates)` and crop the board in a square shape then you can resize it. – Bilal Oct 17 '22 at 04:09
  • @Bilal This might be an XY problem. Part of my motivation to find the grid in the image is to determine the `source_coordinates` for a perspective transformation. Do you have an alternative suggestion for doing that? – Code-Apprentice Oct 17 '22 at 04:59
  • @Bilal Oh, your suggested dupe might be the solution here. I'll take a look at that. – Code-Apprentice Oct 17 '22 at 05:01
  • 1
    @Bilal The code from the accepted answer appears to give the results I want. Thanks. Now to move to the harder version of this problem when there are stones on the board. – Code-Apprentice Oct 17 '22 at 05:09
  • ...and a partial board – Code-Apprentice Oct 17 '22 at 05:15
  • "*partial board*", [HSV Segmentaion](https://stackoverflow.com/a/59906154) is useful to detect the wooden board and then the borders, and if you can do so you can apply the former approach again. – Bilal Oct 17 '22 at 06:17
  • @Bilal Thanks for the link. I'll check it out after I figure out some other things with a complete board. – Code-Apprentice Oct 17 '22 at 13:32

0 Answers0