In [2]:
%matplotlib inline
import cv2
import numpy as np
import matplotlib.pyplot as plt
In [3]:
img = cv2.imread('page22.tif')
#img = cv2.cvtColor(img, cv2.cv.CV_BGR2RGB)
In [4]:
board = img[1045:2245, 1858:3058]
In [5]:
plt.imshow(board)
Out[5]:
In [6]:
def get_cell(x, y):
x_cell_size = 149
y_cell_size = 150
x *= x_cell_size
y *= y_cell_size
field = board[y:y+y_cell_size, x:x+x_cell_size]
plt.imshow(field)
return field
In [7]:
f = get_cell(0,0)
In [8]:
f = get_cell(7,7)
In [9]:
dark = get_cell(3,4)
In [13]:
black_pawn_on_dark = board[150:300, 0:150]
plt.imshow(black_pawn_on_dark)
Out[13]:
In [27]:
# Let's try to find all black pawns on dark squares...
_, w, h = black_pawn_on_dark.shape[::-1]
board_copy = board.copy()
res = cv2.matchTemplate(board, black_pawn_on_dark, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(board_copy, pt, (pt[0] + w, pt[1] + h), (0,200,0), 5)
plt.imshow(board_copy)
Out[27]:
In [26]:
zip(*loc[::-1])
Out[26]:
In [ ]: