I want to extract the move boxes from a chess scoresheet.
I followed slide 15 of the paper. I use the code to implement the steps described in the paper.
def preprocess(img):
img_tmp = img
img_tmp = cv2.cvtColor(img_tmp, cv2.COLOR_BGR2GRAY)
otsu_threshold, img_tmp = cv2.threshold(img_tmp, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
kernel = np.ones((5,5), np.uint8)
img_tmp = cv2.erode(img_tmp, kernel, iterations = 5)
img_tmp = cv2.dilate(img_tmp, kernel, iterations = 5)
contours, hierarchy = cv2.findContours(image = img_tmp, mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_NONE)
return (img_tmp, contours)
I don't get the same results as described in the paper. Through image processing, I can't get an image containing only gridlines. Instead, I get a preprocessed scoresheet that still contains irrelevant information.
Garbage in garbage out so I also get an image with wrong contours.
I think the error lies in the binarization step. The foreground should be white and the background should be black. But I don't know how to change the parameters in cv2.threshold
to achieve the desired outcome.
Edit: I used the images from the HCS dataset. I get an improved image. The approach from @Bilal improves the image7 further. Still the results are not as good as described in the paper. So to improve the results further removing the handwritten text before the contour detection is a good idea. The paper says
"Then we use two long, thin kernels (one horizontal and one vertical) with sizes relative to input image dimensions, and morphological operations (erosion followed by dilation) with those kernels to generate an image containing only grid lines."
I did use erosion followed by dilation but the image still contains the moves. So there is a mistake in the code.
The article could be an interesting starting point. But is there a more simple approach to the problem?