From c5ac323def408c2ff72f36e95d6c4228a123f8a2 Mon Sep 17 00:00:00 2001 From: Yuchen Liu Date: Thu, 4 Jun 2026 20:22:18 -0700 Subject: [PATCH] Fix: Handle grayscale image matching with RGB templates Fixed OpenCV matchTemplate assertion error when matching RGB template frames against grayscale images. The error occurred in campaign OCR when TEMPLATE_STAGE_CLEAR_20240725 (GIF with RGB frames) was matched against grayscale stage images. Changes: - Added automatic grayscale conversion of templates when input image is grayscale in Template.match_multi() - Handles both GIF and non-GIF templates - Prevents cv2.error: (-215:Assertion failed) type mismatch --- module/base/template.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/module/base/template.py b/module/base/template.py index 6a16533255..6623f11ece 100644 --- a/module/base/template.py +++ b/module/base/template.py @@ -262,13 +262,26 @@ def match_multi(self, image, scaling=1.0, similarity=0.85, threshold=3, name=Non raw = image if self.is_gif: result = [] + # Check if image is grayscale + is_gray = len(image.shape) == 2 for template in self.image: - res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED) + # Convert template to grayscale if image is grayscale + if is_gray and len(template.shape) == 3: + template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) + res = cv2.matchTemplate(image, template_gray, cv2.TM_CCOEFF_NORMED) + else: + res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED) res = np.array(np.where(res > similarity)).T[:, ::-1].tolist() result += res result = np.array(result) else: - result = cv2.matchTemplate(image, self.image, cv2.TM_CCOEFF_NORMED) + # Check if image is grayscale and template is RGB + is_gray = len(image.shape) == 2 + if is_gray and len(self.image.shape) == 3: + template_gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) + result = cv2.matchTemplate(image, template_gray, cv2.TM_CCOEFF_NORMED) + else: + result = cv2.matchTemplate(image, self.image, cv2.TM_CCOEFF_NORMED) result = np.array(np.where(result > similarity)).T[:, ::-1] # result: np.array([[x0, y0], [x1, y1], ...)