OpenCV return keypoints coordinates and area from blob detection, Python

大城市里の小女人 提交于 2019-12-21 07:25:11

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/30807214/opencv-return-keypoints-coordinates-and-area-from-blob-detection-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!