This commit is contained in:
Lukas Wölfer
2019-06-24 21:23:42 +02:00
parent a067bd04b3
commit eea7c6c7ef
2 changed files with 31 additions and 38 deletions

View File

@@ -32,53 +32,46 @@ def _adjust_squares(
count_x: int, count_x: int,
count_y: int, count_y: int,
adjustment: Optional[Adjustment] = None) -> Adjustment: adjustment: Optional[Adjustment] = None) -> Adjustment:
if not adjustment: if not adjustment:
adjustment = Adjustment(0, 0, 0, 0, 0, 0) adjustment = Adjustment(0, 0, 0, 0, 0, 0)
def _adjustment_step(keycode: int) -> None:
assert adjustment is not None
x_keys = {81: -1, 83: +1, 104: -10, 115: +10}
y_keys = {82: -1, 84: +1, 116: -10, 110: +10}
w_keys = {97: -1, 117: +1}
h_keys = {111: -1, 101: +1}
dx_keys = {59: -1, 112: +1}
dy_keys = {44: -1, 46: +1}
if keycode in x_keys:
adjustment.x += x_keys[keycode]
elif keycode in y_keys:
adjustment.y += y_keys[keycode]
elif keycode in w_keys:
adjustment.w += w_keys[keycode]
elif keycode in h_keys:
adjustment.h += h_keys[keycode]
elif keycode in dx_keys:
adjustment.dx += dx_keys[keycode]
elif keycode in dy_keys:
adjustment.dy += dy_keys[keycode]
while True: while True:
working_image = image.copy() working_image = image.copy()
for index_x, index_y in itertools.product(range(count_x), range(count_y)): for index_x, index_y in itertools.product(
range(count_x), range(count_y)):
square = get_square(adjustment, index_x, index_y) square = get_square(adjustment, index_x, index_y)
cv2.rectangle(working_image, cv2.rectangle(working_image,
(square[0], square[1]), (square[0], square[1]),
(square[2], square[3]), (square[2], square[3]),
(0, 0, 0)) (0, 0, 0))
cv2.imshow('Window', working_image) cv2.imshow('Window', working_image)
k = cv2.waitKey(0) keycode = cv2.waitKey(0)
print(k) print(keycode)
if k == 27: if keycode == 27:
break break
elif k == 81: _adjustment_step(keycode)
adjustment.x -= 1
elif k == 83:
adjustment.x += 1
elif k == 82:
adjustment.y -= 1
elif k == 84:
adjustment.y += 1
elif k == 104:
adjustment.x -= 10
elif k == 115:
adjustment.x += 10
elif k == 116:
adjustment.y -= 10
elif k == 110:
adjustment.y += 10
elif k == 97:
adjustment.w -= 1
elif k == 111:
adjustment.h -= 1
elif k == 101:
adjustment.h += 1
elif k == 117:
adjustment.w += 1
elif k == 59:
adjustment.dx -= 1
elif k == 44:
adjustment.dy -= 1
elif k == 46:
adjustment.dy += 1
elif k == 112:
adjustment.dx += 1
cv2.destroyWindow('Window') cv2.destroyWindow('Window')
return adjustment return adjustment

View File

@@ -58,7 +58,7 @@ def simplify(image: np.ndarray) -> Tuple[np.ndarray, Dict[Cardcolor, int]]:
best_color: Optional[Tuple[Cardcolor, int]] = None best_color: Optional[Tuple[Cardcolor, int]] = None
for color in Cardcolor: for color in Cardcolor:
mse = sum((x - y) ** 2 for x, y in zip(color.value, pixel)) mse = sum((x - y) ** 2 for x, y in zip(color.value, pixel))
if not best_color or best_color[1] > mse: if not best_color or best_color[1] > mse: #pylint: disable=E1136
best_color = (color, mse) best_color = (color, mse)
assert best_color assert best_color
result_image[pixel_x, pixel_y] = GREYSCALE_COLOR[best_color[0]] result_image[pixel_x, pixel_y] = GREYSCALE_COLOR[best_color[0]]
@@ -78,7 +78,7 @@ def _find_single_square(search_square: np.ndarray,
template_square.shape[0]:margin_x, margin_y - template_square.shape[0]:margin_x, margin_y -
template_square.shape[1]:margin_y] template_square.shape[1]:margin_y]
count = cv2.countNonZero(search_region - template_square) count = cv2.countNonZero(search_region - template_square)
if not best_result or count < best_result[0]: if not best_result or count < best_result[0]: #pylint: disable=E1136
best_result = ( best_result = (
count, count,
(margin_x - template_square.shape[0], (margin_x - template_square.shape[0],