Worked on cv

This commit is contained in:
Lukas Wölfer
2019-04-22 15:26:12 +02:00
parent 3720e25a5a
commit 3077525efe
3 changed files with 59 additions and 31 deletions

View File

@@ -1,7 +1,9 @@
from typing import List, Tuple
from typing import List, Tuple, Optional, Dict
import numpy
from .adjustment import Adjustment, get_square
from .. import board
import enum
import itertools
def _extract_squares(image: numpy.ndarray,
@@ -20,3 +22,26 @@ def get_field_squares(image: numpy.ndarray,
for iy in range(5):
squares.append(get_square(adjustment, ix, iy))
return _extract_squares(image, squares)
class Cardcolor(enum.Enum):
Bai = (65, 65, 65)
Black = (0, 0, 0)
Red = (22, 48, 178)
Green = (76, 111, 19)
Background = (178, 194, 193)
def simplify(image: numpy.ndarray) -> Dict[Cardcolor, int]:
result_dict: Dict[Cardcolor, int] = {c: 0 for c in Cardcolor}
for pixel in itertools.chain.from_iterable(image):
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_dict[best_color[0]] += 1
return result_dict