Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

滑块匹配方法返回起始 x 坐标。 #176

Merged
merged 1 commit into from
May 27, 2024
Merged

Conversation

kvii
Copy link
Contributor

@kvii kvii commented Mar 27, 2024

slide_match 方法内部调用 get_target 方法时也返回了 target_x。反正 target_y 最后也返回了,那 target_x 也顺便返回一下呗?

源码 2660 行是 target_x 初始化逻辑,2685 行是 target_y 返回逻辑。

ddddocr/ddddocr/__init__.py

Lines 2656 to 2686 in 491ce02

def slide_match(self, target_bytes: bytes = None, background_bytes: bytes = None, simple_target: bool = False,
flag: bool = False):
if not simple_target:
try:
target, target_x, target_y = self.get_target(target_bytes)
target = cv2.cvtColor(np.asarray(target), cv2.IMREAD_ANYCOLOR)
except SystemError as e:
# SystemError: tile cannot extend outside image
if flag:
raise e
return self.slide_match(target_bytes=target_bytes, background_bytes=background_bytes,
simple_target=True, flag=True)
else:
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_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)
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])]}

因为有的滑块是 png 格式的,这种滑块图片四周有一圈透明的像素。如果能拿到 target_x 的话,在计算滑动距离时就能够获得更精准的结果。所以 target_x 也是有必要返回的。

image

@sml2h3 sml2h3 merged commit d69979f into sml2h3:master May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants