From 0e4ac18014049afbab1ad32a3d47f2cd8315626d Mon Sep 17 00:00:00 2001 From: sml2h3 Date: Tue, 25 Jan 2022 23:46:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0Readme=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ddddocr/__init__.py | 26 +++++++++++++++++--------- test.py | 11 ++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ddddocr/__init__.py b/ddddocr/__init__.py index a332fac..5c7a65c 100644 --- a/ddddocr/__init__.py +++ b/ddddocr/__init__.py @@ -1641,27 +1641,35 @@ def get_target(self, img_bytes: bytes = None): starttx = x if end_y != 0: end_x = x - return image.crop([starttx, startty, end_x, end_y]), startty + return image.crop([starttx, startty, end_x, end_y]), starttx, startty def slide_match(self, target_bytes: bytes = None, background_bytes: bytes = None, simple_target: bool=False): if not simple_target: - target, target_y = self.get_target(target_bytes) - target = cv2.cvtColor(np.asarray(target), cv2.COLOR_RGBA2GRAY) + target, target_x, target_y = self.get_target(target_bytes) + target = cv2.cvtColor(np.asarray(target), cv2.IMREAD_ANYCOLOR) else: - target = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_GRAYSCALE) + target = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_ANYCOLOR) target_y = 0 + target_x = 0 - background = cv2.imdecode(np.frombuffer(background_bytes, np.uint8), cv2.IMREAD_GRAYSCALE) - res = cv2.matchTemplate(background, target, cv2.TM_CCOEFF) + background = cv2.imdecode(np.frombuffer(background_bytes, np.uint8), cv2.IMREAD_ANYCOLOR) + + background = cv2.Canny(background, 100, 200) + target = cv2.Canny(target, 100, 200) + + background = cv2.cvtColor(background, cv2.COLOR_GRAY2RGB) + target = cv2.cvtColor(target, cv2.COLOR_GRAY2RGB) + + res = cv2.matchTemplate(background, target, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) - w, h = target.shape[::-1] + h, w = target.shape[:2] bottom_right = (max_loc[0] + w, max_loc[1] + h) return {"target_y": target_y, "target": [int(max_loc[0]), int(max_loc[1]), int(bottom_right[0]), int(bottom_right[1])]} def slide_comparison(self, target_bytes: bytes = None, background_bytes: bytes = None): - target = Image.open(io.BytesIO(target_bytes)) - background = Image.open(io.BytesIO(background_bytes)) + target = Image.open(io.BytesIO(target_bytes)).convert("RGB") + background = Image.open(io.BytesIO(background_bytes)).convert("RGB") image = ImageChops.difference(background, target) background.close() target.close() diff --git a/test.py b/test.py index fbf017a..5eecd2d 100644 --- a/test.py +++ b/test.py @@ -21,20 +21,21 @@ # 滑块模板匹配方式 import cv2 -im = cv2.imread("b.jpg") +im = cv2.imread("b.png") det = ddddocr.DdddOcr(det=False, ocr=False) -with open('a.jpg', 'rb') as f: +with open('t.png', 'rb') as f: target_bytes = f.read() -with open('b.jpg', 'rb') as f: +with open('b.png', 'rb') as f: background_bytes = f.read() -res = det.slide_match(target_bytes, background_bytes, simple_target=True) +res = det.slide_match(target_bytes, background_bytes) print(res) x = res["target"] print(x) -im = cv2.line(im, (int(x[0]), 0), (int(x[0]), 300), color=(0, 0, 255), thickness=2) +# im = cv2.line(im, (int(x[0]), 0), (int(x[0]), 200), color=(0, 0, 255), thickness=2) +im = cv2.rectangle(im, (int(x[0]), int(x[1])), (int(x[2]), int(x[3])), color=(0, 0, 255), thickness=2) cv2.imwrite("res.jpg", im) # det = ddddocr.DdddOcr(det=False, ocr=False)