From 67f55a3e88189c238e7929f29cef969e7634a368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Mon, 22 Apr 2019 19:17:40 +0200 Subject: [PATCH] Worked on card value detection --- shenzhen_solitaire/cv/card_finder.py | 87 ++++++++++++++++++++++++---- test/cv_helper.py | 28 +++++---- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/shenzhen_solitaire/cv/card_finder.py b/shenzhen_solitaire/cv/card_finder.py index 35cb382..b16abda 100644 --- a/shenzhen_solitaire/cv/card_finder.py +++ b/shenzhen_solitaire/cv/card_finder.py @@ -1,22 +1,22 @@ from typing import List, Tuple, Optional, Dict -import numpy -from .adjustment import Adjustment, get_square -from .. import board import enum import itertools +import cv2 +import numpy as np +from .adjustment import Adjustment, get_square -def _extract_squares(image: numpy.ndarray, +def _extract_squares(image: np.ndarray, squares: List[Tuple[int, int, int, - int]]) -> List[numpy.ndarray]: + int]]) -> List[np.ndarray]: return [image[square[1]:square[3], square[0]:square[2]].copy() for square in squares] -def get_field_squares(image: numpy.ndarray, - adjustment: Adjustment) -> List[numpy.ndarray]: +def get_field_squares(image: np.ndarray, + adjustment: Adjustment) -> List[np.ndarray]: squares = [] for ix in range(8): for iy in range(5): @@ -32,16 +32,79 @@ class Cardcolor(enum.Enum): Background = (178, 194, 193) -def simplify(image: numpy.ndarray) -> Dict[Cardcolor, int]: +GREYSCALE_COLOR = { + Cardcolor.Bai: 50, + Cardcolor.Black: 100, + Cardcolor.Red: 150, + Cardcolor.Green: 200, + Cardcolor.Background: 250} + + +def simplify(image: np.ndarray) -> Tuple[np.ndarray, Dict[Cardcolor, int]]: + result_image: np.ndarray = np.zeros( + (image.shape[0], image.shape[1]), np.uint8) result_dict: Dict[Cardcolor, int] = {c: 0 for c in Cardcolor} - for pixel in itertools.chain.from_iterable(image): + for pixel_x, pixel_y in itertools.product( + range(result_image.shape[0]), + range(result_image.shape[1])): + pixel = image[pixel_x, pixel_y] best_color: Optional[Tuple[Cardcolor, int]] = None for color in Cardcolor: mse = sum((x - y) ** 2 for x, y in zip(color.value, pixel)) if not best_color or best_color[1] > mse: best_color = (color, mse) assert best_color - for i in range(3): - pixel[i] = best_color[0].value[i] + result_image[pixel_x, pixel_y] = GREYSCALE_COLOR[best_color[0]] result_dict[best_color[0]] += 1 - return result_dict + return (result_image, result_dict) + + +def get_simplified_squares(image: np.ndarray, + adjustment: Adjustment) -> List[np.ndarray]: + squares = get_field_squares(image, adjustment) + for index, square in enumerate(squares): + squares[index], _ = simplify(square) + return squares + + +def _find_single_square(search_square: np.ndarray, + template_square: np.ndarray) -> Tuple[int, Tuple[int, int]]: + assert search_square.shape[0] <= template_square.shape[0] + assert search_square.shape[1] <= template_square.shape[1] + best_result: Optional[Tuple[int, Tuple[int, int]]] = None + for x, y in itertools.product( + range(template_square.shape[0], search_square.shape[0] - 1, -1), + range(template_square.shape[1], search_square.shape[1] - 1, -1)): + p = template_square[x - search_square.shape[0]:x, + y - search_square.shape[1]:y] - search_square + count = cv2.countNonZero(p) + if not best_result or count < best_result[0]: + best_result = ( + count, + (x - search_square.shape[0], + y - search_square.shape[1])) + return best_result + + +def find_square(search_square: np.ndarray, + squares: List[np.ndarray]) -> Tuple[np.ndarray, int]: + best_set = False + best_square = None + best_count = 0 + best_coord = None + for square in squares: + count, coord = _find_single_square(square, search_square) + if not best_set or count < best_count: + best_set = True + best_square = square + best_count = count + best_coord = coord + print(best_square[1]) + cv2.imshow("Window", best_square - + search_square[best_coord[0]:best_coord[0] + + best_square.shape[0], best_coord[1]:best_coord[1] + + best_square.shape[1]]) + while cv2.waitKey(0) != 27: + pass + cv2.destroyWindow("Window") + return (best_square, best_count) diff --git a/test/cv_helper.py b/test/cv_helper.py index 085de03..eab6f49 100644 --- a/test/cv_helper.py +++ b/test/cv_helper.py @@ -6,8 +6,6 @@ import cv2 import numpy import itertools -A = cv2.imread("Solitaire.png") - def pixelcount(image: numpy.ndarray) -> List[Tuple[Tuple[int, int, int], int]]: p: Dict[Tuple[int, int, int], int] = {(0, 0, 0): 0} @@ -31,14 +29,24 @@ def simplify(image: numpy.ndarray) -> None: def main() -> None: - adj = adjustment.adjust_field(A) - image_squares = card_finder.get_field_squares(A, adj) - for img in image_squares: - print(*pixelcount(img), sep='\n') - cv2.imshow("Window", img) - cv2.waitKey(0) - cv2.destroyWindow("Window") - print() + image = cv2.imread("Solitaire.png") + image2 = cv2.imread("Solitaire2.png") + image2 = cv2.resize(image2, (1000, 629)) + + adj = adjustment.adjust_field(image) + squares = card_finder.get_simplified_squares(image, adj) + print("Simplified") + + adj.x -= 2 + adj.y -= 2 + adj.w += 5 + adj.h += 5 + + image_squares = card_finder.get_field_squares(image2, adj) + for i in range(10,20): + image_squares[i], _ = card_finder.simplify(image_squares[i]) + print("Finding...") + found_image, certainty = card_finder.find_square(image_squares[i], squares) if __name__ == "__main__":