赞
踩
import cv2 import numpy as np # 特征点过滤阈值 verify_ratio = 0.8 # 过滤后特征点存储空间 verify_matches = [] min_matches = 8 # 最少特征点匹配数 # 获取单应性矩阵函数 def get_homo(img1, img2): # 创建特征转换对象 sift = cv2.SIFT_create() # 通过特征转换对象获得特征点和描述子 k1, d1 = sift.detectAndCompute(img1, None) k2, d2 = sift.detectAndCompute(img2, None) # 创建特征匹配器,进行特征匹配 bf = cv2.BFMatcher() matches = bf.knnMatch(d1, d2, k = 2) #过滤特征,找出有效的特征匹配点 for m1, m2 in matches: if m1.distance < verify_ratio * m2.distance: verify_matches.append(m1) # 获取单应性矩阵 if len(verify_matches) > min_matches: img1_pts = [] img2_pts = [] for m in verify_matches: img1_pts.append(k1[m.queryIdx].pt) img2_pts.append(k2[m.trainIdx].pt) img1_pts = np.float32(img1_pts).reshape(-1, 1, 2) #修改数据格式,n行1列,每列2个数据 img2_pts = np.float32(img2_pts).reshape(-1, 1, 2) H, mask = cv2.findHomography(img1_pts, img2_pts, cv2.RANSAC, 5) return H else: print('err: Not enough matches!') exit() # 图像拼接函数 def stitch_img(img1, img2, H): # 获得图像的四个角点 h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] img1_dims = np.float32([[0, 0], [0, h1], [w1, h1], [w1, 0]]).reshape(-1, 1, 2) img2_dims = np.float32([[0, 0], [0, h2], [w2, h1], [w2, 0]]).reshape(-1, 1, 2) # 对图像进行变换(单应性矩阵试图进行旋转,平移) img1_transform = cv2.perspectiveTransform(img1_dims, H) print(img1_dims) print(img2_dims) print(img1_transform) # 创建一张大图,将两张图拼到一起 result_dims = np.concatenate((img2_dims, img1_transform), axis = 0) print(result_dims) [x_min, y_min] = np.int32(result_dims.min(axis = 0).ravel() - 0.5) # 返回每一列(x轴)最小值 [x_max, y_max] = np.int32(result_dims.max(axis = 0).ravel() + 0.5) transform_dist = [-x_min, -y_min] # 平移距离 '''平移矩阵: [1, 0, dx 0, 1, dy 0, 0, 1] ''' transform_array = np.array([[1, 0, transform_dist[0]], [0, 1, transform_dist[1]], [0, 0, 1]]) result_img = cv2.warpPerspective(img1, transform_array.dot(H), (x_max - x_min, y_max - y_min)) # 拼接图像 result_img[transform_dist[1]:transform_dist[1]+ h2, transform_dist[0]:transform_dist[0]+ w2] =img2 return result_img # 读取文件,将图片设置为一样大小 640x480 img1 = cv2.imread('./picture/shan2.jpeg') img2 = cv2.imread('./picture/shan1.jpeg') img1 = cv2.resize(img1, (640, 480)) img2 = cv2.resize(img2, (640, 480)) #将两张图横向排列并显示 inputs = np.hstack((img1, img2)) # 找特征点,根据特征点和计算描述子,得到单应性矩阵 H = get_homo(img1, img2) # 根据单应性矩阵进行图像变换,然后进行平移 result_img = stitch_img(img1, img2, H) # 图像拼接并输出图像 cv2.imshow('result', result_img) cv2.waitKey(0)
结果展示:
import cv2 # 读取文件,将图片设置为一样大小 640x480 img1 = cv2.imread('./picture/shan2.jpeg') img2 = cv2.imread('./picture/shan1.jpeg') img1 = cv2.resize(img1, (640, 480)) img2 = cv2.resize(img2, (640, 480)) imgs = [] imgs.append(img1) imgs.append(img2) # 这一行需要注意 stitcher = cv2.Stitcher_create(cv2.Stitcher_PANORAMA) status, result_image = stitcher.stitch(imgs) if status != cv2.Stitcher_OK: print("Can't stitch images, error code = %d" % status) exit() cv2.imshow('result', result_image) cv2.waitKey(0)
结果展示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。