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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30807214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:00:25  来源:igfitidea点击:

OpenCV return keypoints coordinates and area from blob detection, Python

pythonopencvimage-processingblobkeypoint

提问by J_yang

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:

我按照 blob 检测示例(使用cv2.SimpleBlobDetector)并成功检测到我的二进制图像中的 blob。但后来我不知道如何提取关键点的坐标和面积。以下是 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)

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

所以变量keypoints_black包含 blob(s) 的信息。当我打印变量时,它看起来像这样(找到了 2 个 blob):

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.

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

采纳答案by Jo?o Paulo

The ptproperty:

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

一些文档

回答by Karthik N G

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.

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