SIFT图像特征提取 python3.6 + opencv3.3代码
Posted 推杯问盏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SIFT图像特征提取 python3.6 + opencv3.3代码相关的知识,希望对你有一定的参考价值。
opencv3.x 中部分函数有改变:
1. SIFT:可以采用help(cv2.xfeatures2d)查询
2.drawKeypoints: 同样采用help()方法查询
opencv3 版本sift,surf 及其他不稳定的算法函数都放在opencv3.x的contrib版里。该模块下载地址 https://www.lfd.uci.edu/~gohlke/pythonlibs/
import cv2 import numpy as np def sift_kp(image): gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) sift=cv2.xfeatures2d.SIFT_create() kp,des = sift.detectAndCompute(image,None) kp_image = cv2.drawKeypoints(gray_image,kp,None) return kp_image,kp,des def get_good_match(des1,des2): bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2, k=2) #des1为模板图,des2为匹配图 matches = sorted(matches,key=lambda x:x[0].distance/x[1].distance) good = [] for m, n in matches: if m.distance < 0.75 * n.distance: good.append(m) return good def siftImageAlignment(img1,img2): _,kp1,des1 = sift_kp(img1) _,kp2,des2 = sift_kp(img2) goodMatch = get_good_match(des1,des2) if len(goodMatch) > 4: ptsA= np.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2) ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2) ransacReprojThreshold = 4 H, status =cv2.findHomography(ptsA,ptsB,cv2.RANSAC,ransacReprojThreshold); imgOut = cv2.warpPerspective(img2, H, (img1.shape[1],img1.shape[0]),flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) return imgOut,H,status img1 = cv2.imread(r\'sift_img/8.png\') img2 = cv2.imread(r\'sift_img/7.png\') _,kp1,des1 = sift_kp(img1) _,kp2,des2 = sift_kp(img2) goodMatch = get_good_match(des1,des2) img3 = cv2.drawMatches(img1, kp1, img2, kp2, goodMatch[:5], None, flags=2) #----or---- #goodMatch = np.expand_dims(goodMatch,1) #img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, goodMatch[:5], None, flags=2) cv2.imshow(\'img\',img3) cv2.waitKey(0) cv2.destroyAllWindows()
SIFT特征详解:
http://www.cnblogs.com/wangguchangqing/p/4853263.html
http://blog.csdn.net/abcjennifer/article/details/7639681
以上是关于SIFT图像特征提取 python3.6 + opencv3.3代码的主要内容,如果未能解决你的问题,请参考以下文章