MCPcopy
hub / github.com/sml2h3/ddddocr / _edge_based_match

Method _edge_based_match

ddddocr/core/slide_engine.py:236–274  ·  view source on GitHub ↗

基于边缘检测的滑块匹配 Args: target: 滑块图像 background: 背景图像 Returns: 匹配结果

(self, target: np.ndarray, background: np.ndarray)

Source from the content-addressed store, hash-verified

234 raise ImageProcessError(f"简单模板匹配失败: {str(e)}") from e
235
236 def _edge_based_match(self, target: np.ndarray, background: np.ndarray) -> Dict[str, Any]:
237 """
238 基于边缘检测的滑块匹配
239
240 Args:
241 target: 滑块图像
242 background: 背景图像
243
244 Returns:
245 匹配结果
246 """
247 try:
248 # 边缘检测
249 target_edges = cv2.Canny(target, 50, 150)
250 background_edges = cv2.Canny(background, 50, 150)
251
252 # 模板匹配
253 result = cv2.matchTemplate(background_edges, target_edges, cv2.TM_CCOEFF_NORMED)
254
255 # 找到最佳匹配位置
256 _, max_val, _, max_loc = cv2.minMaxLoc(result)
257
258 # 计算滑块中心位置
259 if len(target.shape) == 3:
260 target_h, target_w, _ = target.shape
261 else:
262 target_h, target_w = target.shape
263 center_x = max_loc[0] + target_w // 2
264 center_y = max_loc[1] + target_h // 2
265
266 return {
267 'target': [center_x, center_y],
268 'target_x': center_x,
269 'target_y': center_y,
270 'confidence': float(max_val)
271 }
272
273 except Exception as e:
274 raise ImageProcessError(f"边缘匹配失败: {str(e)}") from e
275
276 def is_ready(self) -> bool:
277 """

Callers 1

_perform_slide_matchMethod · 0.95

Calls 1

ImageProcessErrorClass · 0.85

Tested by

no test coverage detected