def ShowSolution(images, puzzle, solution, frame, box):
cell_size = np.array([box.w / 4, box.h / 4])
for piece_type, piece, i, j in solution:
top_left_loc = np.array([box.x, box.y]) + (np.array([j, i]) -
np.array([1, 1])) * cell_size
color = pieces.Colors[piece_type]
piece_img = np.zeros_like(frame)
for square in itertools.product(range(2), range(2)):
if piece[square] == board.SquareType.AIR:
continue
loc = top_left_loc + np.array(square[::-1]) * cell_size
piece_img = cv2.rectangle(piece_img, tuple(loc), tuple(loc + cell_size),
color, -2)
if piece[square] in images:
image = cv2.resize(images[piece[square]], tuple(cell_size))
blend = np.zeros_like(piece_img)
blend[loc[1]:loc[1] + cell_size[1], loc[0]:loc[0] + cell_size[
0]] = image
piece_img = cv2.addWeighted(piece_img, 1.0, blend, 1.0, 0)
piece_gray = cv2.cvtColor(piece_img, cv2.COLOR_RGB2GRAY)
_, piece_gray = cv2.threshold(piece_gray, 10, 255, cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(piece_gray, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
piece_img = cv2.drawContours(piece_img, contours, -1, (255, 255, 255), 3)
frame = cv2.addWeighted(frame, 1.0, piece_img, 0.7, 0)
cv2.imshow("Planes", frame)
评论列表
文章目录