17

I'm trying to run blob detection using some 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 might be wrong?

2
  • What is the image channel? RGB or gray scale? Jan 7, 2018 at 12:19
  • @Saranraj Nambusubramaniyan It is RGB. I've already set filterByColor = True though.
    – John M.
    Jan 7, 2018 at 12:33

1 Answer 1

47

If your OpenCV version is 2.x, then use cv2.SimpleBlobDetector(). Otherwise if your OpenCV version 3.x (or 4.x), then use cv2.SimpleBlobDetector_create to create the detector.

## 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)
1
  • 12
    Oh! That wasn't obvious at all. Do you know where that's documented? All of the other SO questions I saw use SimpleBlobDetector(params) to create the detector.
    – John M.
    Jan 7, 2018 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.