fix(core): 修复模板匹配中的 threshold 值被错误地传入到了 hist_match 函数

This commit is contained in:
XcantloadX 2025-01-14 19:01:02 +08:00
parent 159a626a98
commit fdf7dfce07
7 changed files with 27 additions and 8 deletions

View File

@ -383,7 +383,7 @@ class Context:
def __init__(self):
# HACK: 暂时写死
from adbutils import adb
adb.connect('127.0.0.1:16384')
adb.connect('127.0.0.1:5555')
self.__device = AdbDevice(adb.device_list()[0])
# self.__device = None
self.__ocr = ContextOcr(self)

View File

@ -190,7 +190,7 @@ def template_match(
img1 = cv2.bitwise_and(img1, img1, mask=mask)
img2 = cv2.bitwise_and(img2, img2, mask=mask)
if not hist_match(img1, img2, (0, 0, w, h), threshold):
if not hist_match(img1, img2, (0, 0, w, h)):
continue
matches.append(TemplateMatchResult(
@ -208,8 +208,8 @@ def template_match(
def hist_match(
image: MatLike | str,
template: MatLike | str,
rect: Rect,
threshold: float = 0.8,
rect: Rect | None = None,
threshold: float = 0.9,
) -> bool:
"""
对输入图像的矩形部分与模板进行颜色直方图匹配
@ -219,7 +219,7 @@ def hist_match(
:param image: 输入图像
:param template: 模板图像
:param rect: 待匹配的矩形区域
:param rect: 输入图像中待匹配的矩形区域
:param threshold: 相似度阈值默认为 0.8
:return: 是否匹配成功
"""
@ -228,12 +228,16 @@ def hist_match(
template = _unify_image(template)
# 从图像中裁剪出矩形区域
x, y, w, h = rect
roi = image[y:y+h, x:x+w]
if rect is None:
roi = image
else:
x, y, w, h = rect
roi = image[y:y+h, x:x+w]
# 确保尺寸一致
if roi.shape != template.shape:
roi = cv2.resize(roi, (template.shape[1], template.shape[0]))
# roi = cv2.resize(roi, (template.shape[1], template.shape[0]))
raise ValueError('Expected two images with the same size.')
# 将图像分为上中下三个区域
h = roi.shape[0]

0
tests/core/__init__.py Normal file
View File

15
tests/core/test_image.py Normal file
View File

@ -0,0 +1,15 @@
import cv2
from util import BaseTestCase
from kotonebot.backend.image import *
class TestImage(BaseTestCase):
def test_hist_match(self):
add_black = cv2.imread('tests/images/template_match/add.png')
add_gray = cv2.imread('tests/images/template_match/add_disabled.png')
self.assertFalse(hist_match(add_black, add_gray))
self.assertTrue(hist_match(add_black, add_black))
self.assertTrue(hist_match(add_gray, add_gray))
with self.assertRaises(ValueError):
hist_match(add_black, add_gray, rect=(0, 0, 1, 1))

Binary file not shown.

After

Width:  |  Height:  |  Size: 654 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B