在图像中检测指定图标 Args: image: 目标图像(numpy数组或文件路径) icon_path: 图标模板路径 threshold: 相似度阈值,None时使用默认值 apply_nms: 是否应用非极大值抑制 Returns: 检测结果列表
(self,
image: Union[np.ndarray, str],
icon_path: str,
threshold: Optional[float] = None,
apply_nms: bool = True)
| 175 | return selected |
| 176 | |
| 177 | def detect_icon(self, |
| 178 | image: Union[np.ndarray, str], |
| 179 | icon_path: str, |
| 180 | threshold: Optional[float] = None, |
| 181 | apply_nms: bool = True) -> List[Dict]: |
| 182 | """ |
| 183 | 在图像中检测指定图标 |
| 184 | |
| 185 | Args: |
| 186 | image: 目标图像(numpy数组或文件路径) |
| 187 | icon_path: 图标模板路径 |
| 188 | threshold: 相似度阈值,None时使用默认值 |
| 189 | apply_nms: 是否应用非极大值抑制 |
| 190 | |
| 191 | Returns: |
| 192 | 检测结果列表 |
| 193 | """ |
| 194 | # 处理输入图像 |
| 195 | if isinstance(image, str): |
| 196 | if not os.path.exists(image): |
| 197 | logger.error(f"图像文件不存在: {image}") |
| 198 | return [] |
| 199 | target_image = cv2.imread(image, cv2.IMREAD_GRAYSCALE) |
| 200 | if target_image is None: |
| 201 | logger.error(f"无法读取图像文件: {image}") |
| 202 | return [] |
| 203 | else: |
| 204 | target_image = image |
| 205 | if len(target_image.shape) == 3: |
| 206 | target_image = cv2.cvtColor(target_image, cv2.COLOR_BGR2GRAY) |
| 207 | |
| 208 | # 加载图标模板 |
| 209 | template = self.load_icon_template(icon_path) |
| 210 | if template is None: |
| 211 | return [] |
| 212 | |
| 213 | # 使用指定阈值或默认阈值 |
| 214 | use_threshold = threshold if threshold is not None else self.default_threshold |
| 215 | |
| 216 | # 执行多尺度匹配 |
| 217 | matches = self.match_template_multiscale(target_image, template, use_threshold) |
| 218 | |
| 219 | # 应用非极大值抑制 |
| 220 | if apply_nms and matches: |
| 221 | matches = self.non_maximum_suppression(matches) |
| 222 | |
| 223 | logger.debug(f"图标检测完成,找到 {len(matches)} 个匹配") |
| 224 | return matches |
| 225 | |
| 226 | def detect_icons_batch(self, |
| 227 | image: Union[np.ndarray, str], |
no test coverage detected