OpenCV关键点可视化

10

我正在学习OpenCV,目前正在尝试理解存储在KeyPoint中的底层数据,以便我可以更好地利用该数据来开发我正在工作的应用程序。

到目前为止,我一直在阅读以下这两个页面:

http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?highlight=featuredetector#FeatureDetector

http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html

然而,当我按照教程使用drawKeypoints()时,所有点的大小和形状都相同,并且以似乎是随意的颜色绘制。

我想我可以遍历每个关键点的属性:画一个圆、画一个箭头(表示角度)、根据响应给它一个颜色等。但是我认为肯定有更好的方法。

是否有内置的方法或其他方法类似于drawKeypoints(),可以帮助我更有效地可视化图像的KeyPoints

4个回答

8

是的,有一种方法可以执行您的任务。如文档中所述:

对于每个关键点,将绘制大小和方向的关键点周围的圆

如果您正在使用Java,则可以简单地指定关键点的类型:

Features2d.drawKeypoints(image1, keypoints1, imageOut2,new Scalar(2,254,255),Features2d.DRAW_RICH_KEYPOINTS);

在C++中:

drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

2
尽管最终我还是自己写了,但你的回答正是我最初寻找的。DRAW_RICH_KEYPOINTS标志是我所缺少的,这对于未来的访问者可能非常有帮助。谢谢! - jstaker7

7

我曾经遇到过类似的问题,想要自定义绘制的点,所以决定分享我的解决方案,因为我想更改绘制的点的形状。

您可以使用cv2.circle来修改线条。im是您希望在其中绘制点的输入图像,keyp是您想要绘制的关键点,col是线条颜色,th是圆形边缘的厚度。

import cv2
import numpy as np
import matplotlib.pyplot as plt

def drawKeyPts(im,keyp,col,th):
    for curKey in keyp:
        x=np.int(curKey.pt[0])
        y=np.int(curKey.pt[1])
        size = np.int(curKey.size)
        cv2.circle(im,(x,y),size, col,thickness=th, lineType=8, shift=0) 
    plt.imshow(im)    
    return im    

imWithCircles = drawKeyPts(origIm.copy(),keypoints,(0,255,0),5)

curKey.size should be curKey.size / 2 when used as radius of cv2.circle - Guglie

1
你可以遍历检测到的关键点向量,对于每个KeyPoint.pt绘制一个半径与KeyPoint.size类似的圆,并根据KeyPoint.response的值来确定颜色。当然这只是一个例子;如果您的检测器输出八度和角度,则可以根据八度和角度编写更复杂的绘图功能。
希望这能帮到你。

1

你好,这是我的代码 @Alex

def drawKeyPts(im, keyp, col, th):
    draw_shift_bits = 4
    draw_multiplier = 1 << 4
    LINE_AA = 16
    im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
    for curKey in keyp:
        center = (int(np.round(curKey.pt[0]*draw_multiplier)), int(np.round(curKey.pt[1]*draw_multiplier)))
        radius = int(np.round(curKey.size/2*draw_multiplier))
        cv2.circle(im, center, radius, col, thickness=th, lineType=LINE_AA, shift=draw_shift_bits)
        if(curKey.angle != -1):
            srcAngleRad = (curKey.angle * np.pi/180.0)
            orient = (int(np.round(np.cos(srcAngleRad)*radius)), int(np.round(np.sin(srcAngleRad)*radius)))
            cv2.line(im, center, (center[0]+orient[0], center[1]+orient[1]), col, 1, LINE_AA, draw_shift_bits)
    cv2.imshow('name1', im)
    cv2.waitKey()
    return im

来自OpenCV的参考资料 - yangninghua
大家好,我第一次在stackoverflow上回答问题,很高兴能够帮助大家,哈哈哈。 - yangninghua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接