OpenCV - Calculating SIFT Descriptors for given Harris Corner Features

1.3k views Asked by At

I need to calculate the SIFT descriptors for given features from the Harris Corner detection in OpenCV. How would I do that? Could you provide me some code examples to modify the SIFT-compute method?

My code so far:

import cv2 as cv

img = cv.imread('example_image.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
dst = cv.cornerHarris(gray, 2, 3, 0.05)
dst = cv.dilate(dst, None)

And now I want to add something like this:

sift = cv.SIFT_create()
sift.compute(#Pass Harris corner features here)

Is this possible? I searched for a while, but I couldn't find anything. Thank you guys.

1

There are 1 answers

0
Kilian Kramer On BEST ANSWER

There was an answer to this topic already here:

How do I create KeyPoints to compute SIFT?

The solution:

import numpy as np
import cv2 as cv

def harris(img):
    gray_img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    gray_img = np.float32(gray_img)
    dst = cv.cornerHarris(gray_img, 2, 3, 0.04)
    result_img = img.copy() # deep copy image
    # Threshold for an optimal value, it may vary depending on the image.
    # draws the Harris corner key-points on the image (RGB [0, 0, 255] -> blue)
    result_img[dst > 0.01 * dst.max()] = [0, 0, 255]
    # for each dst larger than threshold, make a keypoint out of it
    keypoints = np.argwhere(dst > 0.01 * dst.max())
    keypoints = [cv.KeyPoint(float(x[1]), float(x[0]), 13) for x in keypoints]
    return (keypoints, result_img)


if __name__ == '__main__':
    img = cv.imread('example_Image.png')
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    # Calculate the Harris Corner features and transform them to  
    # keypoints (so they are not longer as a dst matrix) which can be 
    # used to feed them into the SIFT method to calculate the SIFT
    # descriptors:
    kp, img = harris(img)

    # compute the SIFT descriptors from the Harris Corner keypoints 
    sift = cv.SIFT_create()
    sift.compute(img, kp)
    img = cv.drawKeypoints(img, kp, img)

    cv.imshow('dst', img)
    if cv.waitKey(0) & 0xff == 27:
        cv.destroyAllWindows()