Skip to main content

图像预处理

import cv2
from ascript.android import screen

AScript 内置 OpenCV 4.1.2.30,可直接使用 cv2 对屏幕截图进行预处理,提升识别准确率。

使用场景
  • OCR 识别前做二值化/去噪,提升复杂背景下的文字识别率
  • 找图前做灰度化/模糊处理,提升模板匹配准确度
  • 处理光线不均匀的截图(自适应二值化)
  • 去除干扰元素(腐蚀掉细小噪点、膨胀连接断裂文字)
  • 缩放/旋转图片后再进行识别

获取 OpenCV 图像

from ascript.android import screen

# 方式一:直接截屏为 cv2 格式(BGR)
img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)

# 方式二:Bitmap 转 cv2
bitmap = screen.capture()
img = screen.bitmap_to_cvimage(bitmap)

# 方式三:裁剪指定区域
img = screen.capture(100, 200, 500, 600, pixel_format=screen.FORMAT_CV_MAT)

常用预处理操作

以下为 OpenCV 原生函数,可直接在 AScript 中使用。

灰度化

import cv2
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

二值化

import cv2
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 固定阈值二值化
_, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

# 反向二值化(黑白互换)
_, binary_inv = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)

# 自适应二值化(光线不均匀时推荐)
adaptive = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

腐蚀 & 膨胀

import cv2
import numpy as np
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)
kernel = np.ones((5, 5), np.uint8)

# 腐蚀:去除小噪点,使前景变细
eroded = cv2.erode(img, kernel, iterations=1)

# 膨胀:填补空洞,使前景变粗
dilated = cv2.dilate(img, kernel, iterations=1)

开运算 & 闭运算

import cv2
import numpy as np
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)
kernel = np.ones((5, 5), np.uint8)

# 开运算(先腐蚀后膨胀):去除小噪点
opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

# 闭运算(先膨胀后腐蚀):填充小孔洞
closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

高斯模糊

import cv2
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)

# 去噪平滑
blurred = cv2.GaussianBlur(img, (5, 5), 0)

缩放 & 旋转

import cv2
from ascript.android import screen

img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)

# 缩放到 50%
resized = cv2.resize(img, None, fx=0.5, fy=0.5)

# 旋转 90 度
rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

预处理后用于识别

预处理后的 cv2 图像可直接传入各图色方法的 image 参数:

import cv2
from ascript.android import screen
from ascript.android.screen import Ocr, FindImages

# 截屏并预处理
img = screen.capture(pixel_format=screen.FORMAT_CV_MAT)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

# 用预处理后的图进行 OCR
result = Ocr.find("文字", image=binary)

# 用预处理后的图进行找图
result = FindImages.find("btn.png", image=img)

更多参考