SIFT图像特征提取 python3.4 + opencv3.1.0代码

opencv3.1中部分函数有改变:

1. SIFT:可以采用help(cv2.xfeatures2d)查询

2.drawKeypoints: 同样采用help()方法查询,


import cv2
import numpy as np

#read image
img = cv2.imread('test.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('origin',img)

#SIFT
sift= cv2.xfeatures2d.SIFT_create()
keypoints = sift.detect(gray, None)

#kp, des = sift.detectAndCompute(gray,None)  #des是描述子,for match, should use des, bf = cv2.BFMatcher();smatches = bf.knnMatch(des1,des2, k=2
cv2.drawKeypoints(gray, keypoints, img)
cv2.imshow('testSift', img)

cv2.waitKey(0)
cv2.destroyAllWindows()


###notes: narray to image data for sift computing:

>>> img_array=np.zeros([10,10,3])
>>> img_array[:,:,0]=np.ones([10,10])*255
>>> img_array[:,:,1]=np.ones([10,10])*255
>>> img_array[:,:,2]=np.ones([10,10])*0
>>> img_uint8=np.uint8(img_array)   #img_gray=cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
>>> sift=cv2.xfeatures2d.SIFT_create()
>>> kp, des=sift.detectAndCompute(img_uint8, None)


SIFT图像特征提取 python3.4 + opencv3.1.0代码_第1张图片





你可能感兴趣的:(机器学习)