图像处理基础函数
Mower 使用 OpenCV 处理图像,颜色通道顺序为 RGB。
坐标与区域
类型
arknights_mower/utils/typealias.py 中定义了坐标与区域的类型:
Coordinate = Tuple[int, int]
Scope = Tuple[Coordinate, Coordinate]
Coordinate 元组的两个元素分别是横坐标和纵坐标;Scope 元组的两个元素分别是区域左上角与右下角的坐标。
get_pos()
 arknights_mower/utils/solver 中 BaseSolver 类有静态方法 get_pos()。get_pos() 接受三个参数,第一个参数既可以是坐标,也可以是区域。当第一个参数是坐标时,get_pos() 直接返回此坐标;当第一个参数是区域时,根据第二、第三个参数 x_rate 和 y_rate 获取区域中指定比例的坐标。默认值 x_rate=0.5,y_rate=0.5,也就是获取区域中央的坐标。
当 get_pos() 接受的第一个参数为区域时,若 x_rate 或 y_rate 超过 1 或小于 0,可得到位于区域外的坐标。
va()、vs() 和 sa()
 arknights_mower/utils/vector.py 提供了坐标的加减法与区域的偏移函数。va() 将两个坐标相加,vs() 将两个坐标相减,sa() 进行区域偏移。
使用示例
from arknights_mower.utils.solver import BaseSolver
  pos1 = (50, 30)
pos2 = (80, 60)
scope = (pos1, pos2)
  BaseSolver.get_pos(scope)
  BaseSolver.get_pos(scope, x_rate=0.2, y_rate=0.8)
  from arknights_mower.utils.vector import va, vs, sa
  va(pos1, pos2)
  vs(pos1, pos2)
  pos3 = (90, 40)
sa(scope, pos3)
  加载图像
loadimg()
 arknights_mower/utils/image.py 中提供了用于读取图像的函数 loadimg()。loadimg() 接受两个参数,第一个参数 filename 是图片文件名,第二个参数 gray 为 True 时返回灰度图像,否则返回 RGB 顺序的彩色图像。gray 默认为 False。
loadimg() 使用 lru_cache 以避免反复加载相同的文件。可使用 loadimg.cache_clear() 清除 LRU 缓存。
loadres()
 Mower 用于识别的图像素材全部位于 arknights_mower/resources 以及热更新目录 tmp/hot_update 下。使用 loadimg() 加载这些图片时,需要拼接出完整的路径,较为繁琐。loadres() 函数可简化从上述位置加载图片的过程。它接受两个参数,第一个参数 res 是图片资源名,第二个参数 gray 与 loadimg() 中保持一致。
图片资源名的规则如下:
- 对于 arknights_mower/resources下的 png 文件,只需写出相对resources的路径,并省略扩展名;
- 以 @hot/表示热更新目录tmp/hot_update;
- 对于 jpg 文件,在资源名中需要保留 .jpg后缀。
| 资源名 | 文件名 | 
|---|---|
| ope_select_start | arknights_mower/resources/ope_select_start.png | 
| navigation/episode | arknights_mower/resources/navigation/episode.png | 
| @hot/dragon_boat_festival/banner | tmp/hot_update/dragon_boat_festival/banner.png | 
| @hot/hortus/terminal.jpg | tmp/hot_update/hortus/terminal.jpg | 
loadimg() 与 loadres() 加载的图像,可直接用 matplotlib 显示。
使用示例
from matplotlib import pyplot as plt
from arknights_mower.utils.image import loadimg, loadres
  sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/201/20240707154102.png")
plt.imshow(sc)
plt.show()
  sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/201/20240707154102.png", True)
plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
plt.show()
  res = loadres("ope_select_start")
plt.imshow(res)
plt.show()
  res = loadres("ope_select_start", True)
plt.imshow(res, cmap="gray", vmin=0, vmax=255)
plt.show()
  裁切图像
arknights_mower/utils/image.py 中提供 cropimg() 方法,接受两个参数:第一个参数 img 可以是灰度或彩色图像;第二个参数 scope 是裁切的区域。函数返回裁切后的图像。注意 cropimg() 返回的图像没有经过复制,如果要修改裁切后的图像,并且不想修改原图像,需要自己复制一次。
使用示例
from matplotlib import pyplot as plt
from arknights_mower.utils.image import cropimg, loadimg
  sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/201/20240709094558.png")
plt.imshow(sc)
plt.show()
  img = cropimg(sc, ((40, 60), (300, 200)))
plt.imshow(img)
plt.show()
  二值化
arknights_mower/utils/image.py 中 thres2() 方法用于将灰度图像二值化,接受两个参数:第一个参数 img 为灰度图像;第二个参数 thresh 为阈值。函数返回二值化后的图像。
使用示例
from matplotlib import pyplot as plt
from arknights_mower.utils.image import loadimg, thres2
  sc = loadimg("/home/zhao/Documents/mower-profile/screenshot/302/20240713110952.png", True)
plt.imshow(sc, cmap="gray", vmin=0, vmax=255)
plt.show()
  img = thres2(sc, 245)
plt.imshow(img, cmap="gray", vmin=0, vmax=255)
plt.show()