diff --git a/_includes/cropimg.html b/_includes/cropimg.html index 1b25753e..3ff4839b 100644 --- a/_includes/cropimg.html +++ b/_includes/cropimg.html @@ -4,7 +4,6 @@
from matplotlib import pyplot as plt
-
 from arknights_mower.utils.image import cropimg, loadimg
 
diff --git a/_includes/feature-matching.html b/_includes/feature-matching.html new file mode 100644 index 00000000..c766b681 --- /dev/null +++ b/_includes/feature-matching.html @@ -0,0 +1,286 @@ +
+
+
In [1]:
+
+
+
import cv2
+from matplotlib import pyplot as plt
+from arknights_mower.utils.image import loadimg
+from arknights_mower.utils.matcher import ORB, flann
+
+
+
+
+
+
+
+
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 = ORB.detectAndCompute(res, None)
+img = cv2.drawKeypoints(res, kp1, None, (0, 255, 0), flags=0)
+plt.imshow(img)
+plt.show()
+
+
+
+
+
+
+
+
+
+
2024-07-13 19:59:06,194 - 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 = ORB.detectAndCompute(sc, None)
+img = cv2.drawKeypoints(sc, kp2, None, (0, 255, 0), flags=0)
+plt.imshow(img)
+plt.show()
+
+
+
+
+
+
+
+
+
+
2024-07-13 19:59:06,644 - 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_DISTANCE_LIMIT = 0.7
+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()
+
+
+
+
+
+
+
+
+
+
[367, 434]
+
+
+
+
+
+
+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 = ORB.detectAndCompute(sc, None)
+img = cv2.drawKeypoints(sc, kp2, None, (0, 255, 0), flags=0)
+plt.imshow(img)
+plt.show()
+
+
+
+
+
+
+
+
+
+
2024-07-13 19:59:08,194 - 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_DISTANCE_LIMIT = 0.7
+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 +
+
+
+
+
diff --git a/_includes/feature-point.html b/_includes/feature-point.html new file mode 100644 index 00000000..0402b2e0 --- /dev/null +++ b/_includes/feature-point.html @@ -0,0 +1,138 @@ +
+
+
In [1]:
+
+
+
import cv2
+from matplotlib import pyplot as plt
+from arknights_mower.utils.image import loadimg
+from arknights_mower.utils.matcher import ORB
+
+
+
+
+
+
+
+
In [2]:
+
+
+
sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/201/20240713031749.png", True)
+plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
+plt.show()
+
+
+
+
+
+
+
+
+
+
2024-07-13 15:35:42,109 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/mower-profile/screenshot/201/20240713031749.png
+
+
+
+
+
+
+No description has been provided for this image +
+
+
+
+
+
+
+
In [3]:
+
+
+
kp, des = ORB.detectAndCompute(sc, None)
+print(len(kp))
+img = cv2.drawKeypoints(sc, kp, None, (0, 255, 0), flags=0)
+plt.imshow(img)
+plt.show()
+
+
+
+
+
+
+
+
+
+
32303
+
+
+
+
+
+
+No description has been provided for this image +
+
+
+
+
+
+
+
In [4]:
+
+
+
sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/103/20240713152407.png", True)
+plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
+plt.show()
+
+
+
+
+
+
+
+
+
+
2024-07-13 15:35:43,652 - DEBUG - /home/zhao/Documents/arknights-mower/arknights_mower/utils/image.py:44 - loadimg - /home/zhao/Documents/mower-profile/screenshot/103/20240713152407.png
+
+
+
+
+
+
+No description has been provided for this image +
+
+
+
+
+
+
+
In [5]:
+
+
+
kp, des = ORB.detectAndCompute(sc, None)
+print(len(kp))
+img = cv2.drawKeypoints(sc, kp, None, (0, 255, 0), flags=0)
+plt.imshow(img)
+plt.show()
+
+
+
+
+
+
+
+
+
+
8041
+
+
+
+
+
+
+No description has been provided for this image +
+
+
+
+
diff --git a/_includes/loadimg.html b/_includes/loadimg.html index d82e4eab..544817bf 100644 --- a/_includes/loadimg.html +++ b/_includes/loadimg.html @@ -4,7 +4,6 @@
from matplotlib import pyplot as plt
-
 from arknights_mower.utils.image import loadimg, loadres
 
diff --git a/_includes/thres2.html b/_includes/thres2.html index 1e6c68ed..a608be4a 100644 --- a/_includes/thres2.html +++ b/_includes/thres2.html @@ -4,7 +4,6 @@
from matplotlib import pyplot as plt
-
 from arknights_mower.utils.image import loadimg, thres2
 
diff --git a/dev/README.md b/dev/README.md index e6f8b85d..a6f7b3cf 100644 --- a/dev/README.md +++ b/dev/README.md @@ -8,3 +8,4 @@ sort: 6 2. [版本与分支](./branch) 3. [文档](./doc) 4. [图像处理基础函数](./image) +5. [特征匹配](./feature-matching) diff --git a/dev/branch.md b/dev/branch.md index 2ee97f5f..591fb6d9 100644 --- a/dev/branch.md +++ b/dev/branch.md @@ -27,7 +27,7 @@ Mower 使用 GitHub Actions 自动为 Windows 平台打包。打包过程中, 当提交信息是“发版”时,CI 不会修改版本号;反之,CI 会把 commit id 的前 7 位附加到版本号后面,使用加号分隔。 -- 测试版使用滚动更新,版本号格式为 `YYYY.MM+`,其中 `YYYY` 为四位年份,`MM` 为两位月份,`` 为 commit id 的前 7 位。测试版代码的 `__version__` 中只写 `YYYY.MM` 的部分,由 CI 自动添加 commit id。 +- 测试版使用滚动更新,版本号格式为 `YYYY.MM+`,其中 `YYYY` 为四位年份,`MM` 为两位月份,`` 为 commit id 的前 7 位。测试版代码的 `__version__` 中只写 `YYYY.MM` 的部分。如果从源码运行 mower,在启动时利用 `arknights_mower/utils/git_rev` 获得 commit id;在打包时,由 CI 自动添加 commit id。 - 稳定版使用定点更新,版本号格式为 `YYYY.MM.X`,其中 `X` 为小版本号。创建分支时 `X` 为 1,以后每次发版时,需手动增加 `X`,并将提交信息设为“发版”。 ## 日志页背景 diff --git a/dev/feature-matching.md b/dev/feature-matching.md new file mode 100644 index 00000000..afbace4b --- /dev/null +++ b/dev/feature-matching.md @@ -0,0 +1,27 @@ +--- +sort: 5 +--- + +# 特征匹配 + +特征匹配是 mower 中最方便的识别方法。在截图中匹配目标图像时,首先提取截图与目标图像的特征点,然后找出匹配的特征点对,利用匹配结果在截图中定位目标图像。 + +## 特征与特征点 + +可以直接使用 `arknights_mower.utils.matcher` 中的 `ORB` 提取特征点。 + +一般而言,在图像中,拐角、复杂的图案与纹理(包括文字)处可以提取到较多的特征点;在空白处,很难或无法提取到特征点。从复杂的图像中可以提取到更多的特征点。 + +
+使用示例 +{% include feature-point.html %} +
+ +## 特征点的匹配 + +可以直接使用 `arknights_mower.utils.matcher` 中的 `flann` 对特征点进行匹配。 + +
+使用示例 +{% include feature-matching.html %} +