How to use Feature2D (e.g. SimpleBlobDetector)? (Python + OpenCV)

I am trying to run blob detection with simple code:

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)

However, I keep getting the following error:

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)

Does anyone know what could be wrong?

+4
source share
1 answer

If your version of OpenCV is 3.x, use cv2.SimpleBlobDetector_createto create a detector. Or use this code:

## check opencv version and construct the detector
is_cv3 = cv2.__version__.startswith("3.")
if is_cv3:
    detector = cv2.SimpleBlobDetector_create()
else:
    detector = cv2.SimpleBlobDetecto()

## detect 
kpts = detector.detect(img)
+9
source

Source: https://habr.com/ru/post/1691843/