python 图像配准ctselect_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值