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?