我需要從 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)
這可能嗎?我搜索了一段時間,但找不到任何東西。謝謝你們。
uj5u.com熱心網友回復:
這個話題已經有了答案:
如何創建關鍵點來計算 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471841.html
標籤:Python opencv 计算机视觉 特征提取 特征描述符
