OpenCV - Display ONLY the keypoints NOT the image

2019-08-12 12:26发布

问题:

I am trying to only draw the keypoints (without the image) using this example code:

import cv2
import numpy as np

img = cv2.imread('test.png')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

sift = cv2.SIFT()
kp = sift.detect(gray,None)

img=cv2.drawKeypoints(gray,kp)

cv2.imwrite('sift_keypoints.jpg',img)

I tried cv2.drawKeypoints(None,kp)and cv2.drawKeypoints(kp) but to no avail.

Any ideas how this could be achieved ?

Thanks.

回答1:

You can get ONLY the keypoints by drawing them on a solid black image having the SAME shape of your original image.

This is the image I used:

I then obtained the keypoints:

Then I created an image of solid color(black) having same size of the original image and draw these keypoints on them.

Voila ONLY keypoints

CODE:

#---Creating image of solid color with same size as image---
mask = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
mask[:] = (0, 0, 0) 

#---Drawing keypoints on the mask image---
fmask = cv2.drawKeypoints(mask,kp,None,color=(0,255,0), flags=0)
cv2.imshow('fmask.jpg', fmask)


回答2:

OpenCV does not have any method to draw the keypoints alone.This is the code i used to find SIFT keypoints.

import org.opencv.core.*;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.*;

import com.atul.JavaOpenCV.Imshow;

public class testdraw 
{
public static void main(String args[])
{

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img=Highgui.imread("C:\\100.jpg");
        Mat outputImage = new Mat();

        FeatureDetector siftDetector = FeatureDetector.create(FeatureDetector.SIFT);
        MatOfKeyPoint siftKeypoint = new MatOfKeyPoint();

        siftDetector.detect(img,siftKeypoint);
        Features2d.drawKeypoints(img, siftKeypoint, outputImage);

        //Highgui.imwrite("C:\\101.jpg", outputImage);
        Imshow im = new Imshow("Output");
        im.showImage(outputImage);
}       

}