OpenCV return 斑点检测的关键点坐标和区域,Python

OpenCV return keypoints coordinates and area from blob detection, Python

我遵循了一个斑点检测示例(使用 cv2.SimpleBlobDetector)并成功地检测到我的二值图像中的斑点。但是后来我不知道如何提取关键点的坐标和面积。以下是 blob 检测的代码:

# 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)

所以变量keypoints_black包含blob的信息。当我打印变量时,它看起来像这样(发现 2 个斑点):

KeyPoint 0x10b10b870, KeyPoint 0x10b1301b0

那么如何获取关键点的质心坐标及其面积,以便我可以将它们作为 osc 消息发送以进行交互。

pt 属性:

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

如果你有一个关键点列表。然后就可以打印如下图

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

编辑:大小决定了有意义的关键点邻域的直径。您可以使用该大小并粗略计算斑点的面积。