You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用OpenCV SimpleBlobDetector的detect()抛出未知C++异常如何解决

问题修复方案

报错根因

OpenCV 3.0及以上版本调整了SimpleBlobDetector的实例化接口,原有直接调用cv2.SimpleBlobDetector()的写法已废弃,会触发底层C++异常。

修复后可运行代码

import cv2
import numpy as np


def get_circle(img_path):
    im = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
    # 修正检测器实例化方式
    detector = cv2.SimpleBlobDetector_create()
    keypoints = detector.detect(im)
    im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    cv2.imshow("Keypoints", im_with_keypoints)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

半圆检测适配建议

默认SimpleBlobDetector参数是针对完整圆形优化的,如果要检测半圆,需要自定义参数过滤规则:

  • 调整filterByArea参数设置合理的面积区间,过滤过小噪点
  • 调整filterByCircularity参数的阈值,半圆的圆形度低于完整圆形,可适当降低阈值
  • 可配合边缘检测(如Canny)+ 霍夫圆变换的方案做补充检测,提升半圆识别准确率

内容的提问来源于stack exchange,提问作者buymyopps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 15:24:05