【问题标题】:OpenCV return keypoints coordinates and area from blob detection, PythonOpenCV从blob检测返回关键点坐标和区域,Python
【发布时间】:2015-08-28 16:59:59
【问题描述】:

我遵循了一个 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 消息发送以进行交互。

【问题讨论】:

  • 尝试打印dir(keypoint) where keypoint is obviously a keypoint :) That should give you a list of methods. Also, you can try tuple(keypoint)` 看看它是否可以转换。
  • 我试过 tuple(keypoint) 它返回相同,只是在元组而不是数组中。
  • 而且 dir(keypoint) 确实返回了许多方法: ['delattr', 'delitem', 'doc', 'eq', '格式', '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', '弹出','删除','反向',]。但不知道接下来会发生什么
  • 虽然 Joao 提供了答案,但我对元组的建议当然是在 keypoint 上,而不是在对象列表上......

标签: python opencv image-processing blob keypoint


【解决方案1】:

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

【讨论】:

  • 感谢您提供文档链接!
  • 为什么坐标是浮点数?
【解决方案2】:

如果您有关键点列表。然后就可以如下图打印了

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

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

【讨论】:

  • 属性.size是斑点的直径,而不是它的面积。
  • @Delgan 不,我说.size 是指我回答中的区域。请正确阅读。
  • 您是在含蓄地说,因为作者问“如何获得关键点的质心及其面积的坐标”。任何阅读您的答案的人都会错误地从中推断出.size 返回 blob 的区域。
  • 非常感谢。您的回答今天确实为我节省了很多工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2017-02-25
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
相关资源
最近更新 更多