19

I followed a blob detection example (using cv2.SimpleBlobDetector) and successfully detected the blobs in my binary image. But then I don't know how to extract the coordinates and area of the keypoints. Here are the code for the blob detections:

# I skipped the parameter setting part. 
    blobParams = cv2.SimpleBlobDetector_Params()
    blobVer = (cv2.__version__).split('.')
    if int(blobVer[0]) < 3:
        detector = cv2.SimpleBlobDetector(blobParams)
    else:
        detector = cv2.SimpleBlobDetector_create(blobParams)

    # Detect Blobs
    keypoints_black = detector.detect(255-black_blob)
    trans_blobs = cv2.drawKeypoints(gray_video_crop, \
        keypoints_white, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

So the variable keypoints_black contains the information of the blob(s). When I printed the variable it looked something like this (2 blobs were found):

KeyPoint 0x10b10b870, KeyPoint 0x10b1301b0

So how to I get the coordinates of the centre of mass of the keypoints and their area so that I can send them as osc messages for interaction.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
J_yang
  • 2,672
  • 8
  • 32
  • 61
  • Try printing `dir(keypoint)` where `keypoint is obviously a keypoint :) That should give you a list of methods. Also, you can try `tuple(keypoint)` to see if it's convertible. – deets Jun 12 '15 at 16:05
  • I tried tuple(keypoint) it it returned the same, just in tuple rather than array. – J_yang Jun 12 '15 at 16:35
  • And the dir(keypoint) did return a buch of methods: ['__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', #'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', #'__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', #'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', #'__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', #'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', ]. But dont know what next – J_yang Jun 12 '15 at 16:37
  • while Joao provided the answer, my sugestion for tuple was of course meant on a *keypoint*, not the list of objects.... – deets Jun 12 '15 at 20:46

2 Answers2

19

The pt property:

keypoints = detector.detect(frame) #list of blobs keypoints
x = keypoints[i].pt[0] #i is the index of the blob you want to get the position
y = keypoints[i].pt[1]

Some documentation

João Paulo
  • 6,300
  • 4
  • 51
  • 80
11

If you have a list of keypoints. Then you can print as shown below

for keyPoint in keyPoints:
    x = keyPoint.pt[0]
    y = keyPoint.pt[1]
    s = keyPoint.size

Edit: Size determines the diameter of the meaningful keypoint neighborhood. You can use that size and roughly calculate the area of the blob.

Karthik N G
  • 2,111
  • 1
  • 19
  • 20
  • 1
    The attribute `.size` is the diameter of the blob, not its area. – Delgan Dec 16 '16 at 17:50
  • @Delgan No where I said `.size` means area in my answer. Please read it properly. – Karthik N G Dec 20 '16 at 15:41
  • 3
    You are saying it implicitly, as the author asked "how to I get the coordinates of the centre of mass of the keypoints and their area". Anyone reading your answer will wrongly deduce from it than `.size` returns the area of the blob. – Delgan Dec 20 '16 at 16:05
  • Thanks a lot. Your answer really saved a lot of work for me today :) – Megha Aug 18 '17 at 23:54