如何正确使用Feature2D(如SimpleBlobDetector)?(Python + OpenCV)

Joh*_* M. 17 python opencv

我正在尝试使用一些简单的代码运行blob检测:

img = cv2.imread(args["image"])
height, width, channels = img.shape

params = cv2.SimpleBlobDetector_Params()

params.filterByColor = True
params.blobColor = 0

blob_detector = cv2.SimpleBlobDetector(params)
keypoints = blob_detector.detect(img)
Run Code Online (Sandbox Code Playgroud)

但是我一直收到以下错误:

Traceback (most recent call last):
  File "test2.py", line 37, in <module>
    keypoints = blob_detector.detect(img)
TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)
Run Code Online (Sandbox Code Playgroud)

有谁知道什么可能是错的?

Kin*_*t 金 41

如果您的OpenCV版本是2.x,那么使用 cv2.SimpleBlobDetector().否则,如果你的OpenCV版本3.x (or 4.x),那么用于cv2.SimpleBlobDetector_create创建探测器.

## check opencv version and construct the detector
is_v2 = cv2.__version__.startswith("2.")
if is_v2:
    detector = cv2.SimpleBlobDetector()
else:
    detector = cv2.SimpleBlobDetector_create()

## detect 
kpts = detector.detect(img)
Run Code Online (Sandbox Code Playgroud)

  • 哦! 这根本不明显.你知道那里的文件吗?我看到的所有其他SO问题都使用`SimpleBlobDetector(params)`来创建检测器. (12认同)