opencv学习——特征匹配和单应性查找图片
来源:互联网 发布:安卓软件免费源码 编辑:程序博客网 时间:2024/06/06 00:36
"""特征匹配及查找对象"""import cv2import numpy as npimport matplotlib.pylab as pltMIN_MATCH_COUNT = 10img1 = cv2.imread('images/111.jpg') # queryImageimg2 = cv2.imread('images/11.jpg') # trainImagedef SIFT(): # Initiate SIFT detector sift = cv2.xfeatures2d.SIFT_create() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) # BFMatcher with default params bf = cv2.BFMatcher() matches = bf.knnMatch(des1,des2, k=2) # Apply ratio test good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append(m) if len(good)>MIN_MATCH_COUNT: # 获取关键点的坐标 src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) # 第三个参数 Method used to computed a homography matrix. The following methods are possible: #0 - a regular method using all the points #CV_RANSAC - RANSAC-based robust method #CV_LMEDS - Least-Median robust method # 第四个参数取值范围在 1 到 10 , 绝一个点对的阈值。原图像的点经过变换后点与目标图像上对应点的误差 # 超过误差就认为是 outlier # 返回值中 H 为变换矩阵。 H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)# wrap = cv2.warpPerspective(img2, H, (img2.shape[1]+img2.shape[1] , img2.shape[0]+img2.shape[0]))# wrap[0:img2.shape[0], 0:img2.shape[1]] = img1 matchesMask = mask.ravel().tolist()# # 获得原图像的高和宽 h,w,_ = img1.shape# # 使用得到的变换矩阵对原图像的四个角进行变换,获得在目标图像上对应的坐标。 pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,H) # 在原图中画出目标所在位置框, cv2.LINE_AA表示闭合框 cv2.polylines(img2,[np.int32(dst)],True,255,10, cv2.LINE_AA) else: print ("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)) matchesMask = None# draw_params = dict(matchColor = (0,255,0), # draw matches in green color# singlePointColor = None,# matchesMask = matchesMask, # draw only inliers# flags = 2)# img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)# img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,matchesMask = matchesMask,flags=2) good = np.expand_dims(good,1) img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good[:20],None, flags=2) return img3if __name__ == '__main__': result = SIFT() cv2.imshow('result.py',result) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1)
查找结果图:
1、只画出所有inliers的点对,cv2.drawMatches()
2、画出最匹配的前20个,cv2.drawMatchesKnn()
阅读全文
1 0
- opencv学习——特征匹配和单应性查找图片
- 【OpenCV学习笔记】三十七、特征检测与匹配(二)——SIFT特征点匹配
- OpenCV中feature2D学习——SIFT和SURF算子实现特征点提取与匹配
- OpenCV中feature2D学习——SIFT和SURF算子实现特征点提取与匹配
- 【OpenCV学习笔记】三十八、特征检测与匹配(三)——SURF特征点检测与匹配
- OpenCV实践之路——Qt中用opencv提取和匹配SIFT特征点
- OpenCV实践之路——Qt中用opencv提取和匹配SIFT特征点
- 基于OpenCV的简单图片特征匹配
- OpenCV中feature2D学习——FAST特征点检测与SIFT/SURF/BRIEF特征提取与匹配
- Opencv学习笔记(三)———特征点匹配
- OpenCV学习——对富特征进行光流匹配
- 【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配
- OpenCV学习笔记[5]FLANN特征匹配
- Python+OpenCV学习(12)---特征匹配
- opencv学习之 特征检测与匹配
- Opencv 各种特征点提取和匹配
- opencv 特征点匹配和背景识别
- Opencv 各种特征点提取和匹配
- 【MyBatis学习07】动态sql
- cookie和session的区别与联系
- 【MyBatis学习08】高级映射之一对一查询
- 内积与外积
- Struts2 拦截器学习
- opencv学习——特征匹配和单应性查找图片
- Tomcat 学习之简单构建
- 根据相机内参数及畸变系数给图像去畸变
- numpy中where的使用和pandas中iloc的使用
- 关于MobclickAgent方法意图
- Excel解析
- 获取数据的三种遍历方式
- react_basic(1)
- HEX文件合并的方法简述