OpenCV-Python Feature2D 特征点检测 (SIFT,SURF)

git
LINK
下面介绍属于nonfree的特征检测方法,如SIFT和SURF。

这些方法在opencv-contrib中,所以想要使用前,请卸载当前非contrib版本的opencv,即pip uninstall opencv-python后;再重新安装opencv-contrib-python,即pip install opencv-contrib-python

 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-contrib-python

SIFT Feature Detection

#!/usr/bin/env python
# -*- coding=utf-8 -*-
# Summary: 使用OpenCV3.x-Python检测SIFT特征点
# Author:  Amusi
# Date:    2018-03-17
# Reference: https://docs.opencv.org/master/d5/d3c/classcv_1_1xfeatures2d_1_1SIFT.html
 
 
import cv2
import numpy
 
 
def main():
 
 
	img = cv2.imread("lena.png")
	cv2.imshow('Input Image', img)
	cv2.waitKey(0)
	
	# 检测
	sift = cv2.xfeatures2d.SIFT_create()
	keypoints = sift.detect(img, None)
	
	# 显示
	# 必须要先初始化img2
	img2 = img.copy()
	img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
	cv2.imwrite('sift.png', img2)
	cv2.imshow('Detected SIFT keypoints', img2)
	cv2.waitKey(0)
    
if __name__ == '__main__':
	main()
 

在这里插入图片描述
SURF Feature Detection

#!/usr/bin/env python
# -*- coding=utf-8 -*-
# Summary: 使用OpenCV3.x-Python检测SURF特征点
# Author:  Amusi
# Date:    2018-03-17
# Reference: https://docs.opencv.org/master/d5/df7/classcv_1_1xfeatures2d_1_1SURF.html
 
import cv2
import numpy
 
def main():
 
	img = cv2.imread("lena.png")
	cv2.imshow('Input Image', img)
	cv2.waitKey(0)
	
	# 检测
	surf = cv2.xfeatures2d.SURF_create()
	keypoints = surf.detect(img, None)
	
	# 显示
	# 必须要先初始化img2
	img2 = img.copy()
	img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
	cv2.imshow('Detected SURF keypoints', img2)
	cv2.waitKey(0)
    
if __name__ == '__main__':
	main()
posted @ 2022-08-19 22:47  luoganttcc  阅读(40)  评论(0)    收藏  举报