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



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

我的代码:

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)

这可能吗?我找了一会儿,但什么也没找到。谢谢大家。

这个话题已经有了答案:

我如何创建关键点来计算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()

最新更新