Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Feature2D (such as SimpleBlobDetector) correctly? (Python + OpenCV)

Tags:

python

opencv

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?

like image 351
John M. Avatar asked Jan 07 '18 12:01

John M.


1 Answers

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)
like image 134
Kinght 金 Avatar answered Oct 24 '22 18:10

Kinght 金