Pergunta

I am learning OpenCV and at the moment I am trying to understand the underlying data stored in a KeyPoint so that I can better utilize that data for an application I'm working on.

So far I have been going through these two pages:

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

When I follow the tutorial, however, using drawKeypoints(), the points are all the same size and shape, and are drawn with a seemingly arbitrary color.

I guess I could iterate through the attributes for each key point: draw a circle, draw an arrow (for the angle), give it a color based on the response, etc. But I figured there had to be a better way.

Is there a built-in method or other approach similar to drawKeypoints() that will help me more efficiently visualize the KeyPoints of an image?

Foi útil?

Solução

Yes, there is the method to perform your task. As says in documentation

For each keypoint the circle around keypoint with keypoint size and orientation will be drawn

If you are using Java, you can simply specify the type of keypoints:

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

In C++:

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

Outras dicas

I had a similair problem and wanted to customize the points that are drawn, decided to share my solution because I wanted to alter the shape of the points drawn.

You can alter the line with cv2.circle with what you want. im is the input image you want the points to be drawn in, keyp are the keypoints you want to draw, col is the line color, th is the thickness of the circle edge.

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)

You can iterate through the vector of keypoints that you detect and draw (for example) a circle on every KeyPoint.pt having radius analogous to KeyPoint.size and color with respect to KeyPoint.response.. This is of course just an example; you could write more complicated drawing functions based on the octave and angle of the KeyPoint (if your detector gives that output)..

Hope this helps.

hello it is my code @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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top