OpenCV从blob检测返回关键点坐标和区域,Python

时间:2015-06-12 15:47:42

标签: python opencv image-processing blob keypoint

我按照blob检测示例(使用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个blob):

KeyPoint 0x10b10b870, KeyPoint 0x10b1301b0

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

2 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:8)

如果您有关键点列表。然后你可以打印如下所示

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

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