特征点的匹配

In [1]:

import cv2
from matplotlib import pyplot as plt
from arknights_mower.utils.image import loadimg
from arknights_mower.utils.matcher import (
    GOOD_DISTANCE_LIMIT,
    flann,
    keypoints,
    keypoints_scale_invariant,
)

In [2]:

res = loadimg("/home/zhao/Documents/hot_update/hortus/terminal.jpg", True)
plt.imshow(res, cmap="gray", vmin=0, vmax=255)
plt.show()
kp1, des1 = keypoints_scale_invariant(res)
img = cv2.drawKeypoints(res, kp1, None, (0, 255, 0), flags=0)
plt.imshow(img)
plt.show()
2024-07-15 12:07:34,522 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/hot_update/hortus/terminal.jpg

No description has been provided for this image

No description has been provided for this image

In [3]:

sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/501/20240705031952.png", True)
plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
plt.show()
kp2, des2 = keypoints(sc)
img = cv2.drawKeypoints(sc, kp2, None, (0, 255, 0), flags=0)
plt.imshow(img)
plt.show()
2024-07-15 12:07:35,018 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/mower-profile/screenshot/501/20240705031952.png

No description has been provided for this image

No description has been provided for this image

In [4]:

matches = flann.knnMatch(des1, des2, k=2)
good = []
for pair in matches:
    if (len_pair := len(pair)) == 2:
        x, y = pair
        if x.distance < GOOD_DISTANCE_LIMIT * y.distance:
            good.append(x)
    elif len_pair == 1:
        good.append(pair[0])
good = sorted(good, key=lambda x: x.distance)
debug_img = cv2.drawMatches(
    res,
    kp1,
    sc,
    kp2,
    good[:50],
    None,
    (0, 255, 0),
    flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS,
)
plt.imshow(debug_img)
plt.show()

No description has been provided for this image

In [5]:

debug_img = cv2.cvtColor(sc, cv2.COLOR_GRAY2RGB)
center = list(map(int, kp2[good[0].trainIdx].pt))
print(center)
cv2.circle(debug_img, center, 20, (0, 255, 0), 5)
plt.imshow(debug_img)
plt.show()
[463, 273]

No description has been provided for this image

In [6]:

sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/501/20240705071219.png", True)
plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
plt.show()
kp2, des2 = keypoints(sc)
img = cv2.drawKeypoints(sc, kp2, None, (0, 255, 0), flags=0)
plt.imshow(img)
plt.show()
2024-07-15 12:07:36,328 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/mower-profile/screenshot/501/20240705071219.png

No description has been provided for this image

No description has been provided for this image

In [7]:

matches = flann.knnMatch(des1, des2, k=2)
good = []
for pair in matches:
    if (len_pair := len(pair)) == 2:
        x, y = pair
        if x.distance < GOOD_DISTANCE_LIMIT * y.distance:
            good.append(x)
    elif len_pair == 1:
        good.append(pair[0])
good = sorted(good, key=lambda x: x.distance)
debug_img = cv2.drawMatches(
    res,
    kp1,
    sc,
    kp2,
    good[:50],
    None,
    (0, 255, 0),
    flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS,
)
plt.imshow(debug_img)
plt.show()

No description has been provided for this image

In [8]:

debug_img = cv2.cvtColor(sc, cv2.COLOR_GRAY2RGB)
center = list(map(int, kp2[good[0].trainIdx].pt))
print(center)
cv2.circle(debug_img, center, 20, (0, 255, 0), 5)
plt.imshow(debug_img)
plt.show()
[1173, 209]

No description has been provided for this image