生息演算地图匹配地点

In [1]:

from matplotlib import pyplot as plt
from arknights_mower.utils.matcher import Matcher
from arknights_mower.utils.image import loadimg, loadres

In [2]:

sc = loadimg("/home/zhao/Documents/Desktop/ra_map.png", True)
plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
plt.show()
2024-07-16 17:47:39,468 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/Desktop/ra_map.png

No description has been provided for this image

In [3]:

res = loadres("ra/map/资源区_射程以内", True)
plt.imshow(res, cmap="gray", vmin=0, vmax=255)
plt.show()
2024-07-16 17:47:39,726 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/arknights-mower/arknights_mower/resources/ra/map/资源区_射程以内.png

No description has been provided for this image

In [4]:

matcher = Matcher(sc)
2024-07-16 17:47:39,863 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/matcher.py:77 - __init__ - Matcher init: shape ((900, 1370))

In [5]:

matcher.match(res, draw=True)

No description has been provided for this image

2024-07-16 17:47:40,242 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/matcher.py:202 - score - transform matrix: [[1.0012251734016322, -0.005298719241620122, 667.1395609672389], [0.0004466367175678509, 1.00531283833583, 454.08088001521105]]

No description has been provided for this image

No description has been provided for this image

2024-07-16 17:47:40,604 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/matcher.py:113 - match - match success: ([[667, 454], [925, 582]], (0.5030120481927711, 0.8988326848249028, 0.875, 0.8695953607668526))

Out[5]:

[[667, 454], [925, 582]]

In [6]:

import cv2
from arknights_mower.utils.vector import va

In [7]:

result = cv2.matchTemplate(sc, res, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
print(max_val)
h, w = res.shape
scope = max_loc, va(max_loc, (w, h))
print(scope)
img = cv2.cvtColor(sc, cv2.COLOR_GRAY2RGB)
cv2.rectangle(img, scope[0], scope[1], (0, 255, 0), 3)
plt.imshow(img)
plt.show()
0.9816110134124756
((667, 454), (924, 582))

No description has been provided for this image

In [8]:

from arknights_mower.utils.log import logger
logger.setLevel("INFO")

In [9]:

%timeit matcher = Matcher(sc)
3.91 ms ± 240 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [10]:

%timeit matcher.match(res)
4.38 ms ± 34.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [11]:

%%timeit
result = cv2.matchTemplate(sc, res, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
30.7 ms ± 1.71 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)