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

时间:2018-01-07 12:15:28

标签: 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)

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

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)

有谁知道可能出错了什么?

1 个答案:

答案 0 :(得分:34)

如果您的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)