OpenCV - 为给定的哈里斯角特征计算 SIFT 描述符

OpenCV - Calculating SIFT Descriptors for given Harris Corner Features

我需要从 OpenCV 中的哈里斯角检测计算给定特征的 SIFT 描述符。我该怎么做?您能提供一些代码示例来修改 SIFT 计算方法吗?

到目前为止我的代码:

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)

现在我想添加如下内容:

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

这可能吗?我搜索了一会儿,但找不到任何东西。谢谢大家。

此处已有此主题的答案:

How do I create KeyPoints to compute SIFT?

解决方法:

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()