import cv2
def main():
# 加載圖像
image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
# 創(chuàng)建SURF對(duì)象
surf = cv2.xfeatures2d.SURF_create()
# 檢測(cè)特征點(diǎn)和描述符
keypoints1, descriptors1 = surf.detectAndCompute(image1, None)
keypoints2, descriptors2 = surf.detectAndCompute(image2, None)
# 繪制特征點(diǎn)
result_image1 = cv2.drawKeypoints(image1, keypoints1, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
result_image2 = cv2.drawKeypoints(image2, keypoints2, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# 顯示圖像
cv2.imshow("Image 1", result_image1)
cv2.imshow("Image 2", result_image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
import cv2
import numpy as np
def main():
# 加載圖像
image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
# 創(chuàng)建SURF對(duì)象
surf = cv2.xfeatures2d.SURF_create()
# 檢測(cè)特征點(diǎn)和描述符
keypoints1, descriptors1 = surf.detectAndCompute(image1, None)
keypoints2, descriptors2 = surf.detectAndCompute(image2, None)
# 創(chuàng)建匹配器
matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_FLANNBASED)
matches = matcher.match(descriptors1, descriptors2)
# 根據(jù)距離排序匹配項(xiàng)
matches = sorted(matches, key=lambda x: x.distance)
# 提取前10個(gè)最佳匹配項(xiàng)
good_matches = matches[:10]
# 繪制匹配點(diǎn)和線(xiàn)條
result_image = cv2.drawMatches(image1, keypoints1, image2, keypoints2, good_matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
# 顯示圖像
cv2.imshow("Matches", result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-734612.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-734612.html
到了這里,關(guān)于py實(shí)現(xiàn)surf特征提取的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!