import cv2 # 读取两幅待处理的图像 img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE) img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE) # 对图像进行高斯模糊 img1 = cv2.GaussianBlur(img1, (5, 5), 0) img2 = cv2.GaussianBlur(img2, (5, 5), 0) # 使用Shi-Tomasi算法检测特征点 corners1 = cv2.goodFeaturesToTrack(img1, 100, 0.01, 10) corners2 = cv2.goodFeaturesToTrack(img2, 100, 0.01, 10) # 对特征点进行亚像素定位 corners1 = cv2.cornerSubPix(img1, corners1, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)) corners2 = cv2.cornerSubPix(img2, corners2, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)) # 对特征点进行匹配 matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING) kps1, descs1 = sift.detectAndCompute(img1, None) kps2, descs2 = sift.detectAndCompute(img2, None) matches = matcher.match(descs1, descs2) # 使用RANSAC算法进行匹配点筛选 src_pts = np.float32([kps1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([kps2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 对图像进行配准和拼接 result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0])) result[0:img2.shape[0], 0:img2.shape[1]] = img2 # 显示结果 cv2.imshow('Result', result) cv2.waitKey() cv2.destroyAllWindows()改进这段代码使其输出特征点连线图和拼接图
时间: 2023-07-15 17:12:15 浏览: 161
import cv2
import numpy as np
# 读取两幅待处理的图像
img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
# 对图像进行高斯模糊
img1 = cv2.GaussianBlur(img1, (5, 5), 0)
img2 = cv2.GaussianBlur(img2, (5, 5), 0)
# 使用Shi-Tomasi算法检测特征点
corners1 = cv2.goodFeaturesToTrack(img1, 100, 0.01, 10)
corners2 = cv2.goodFeaturesToTrack(img2, 100, 0.01, 10)
# 对特征点进行亚像素定位
corners1 = cv2.cornerSubPix(img1, corners1, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
corners2 = cv2.cornerSubPix(img2, corners2, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
# 对特征点进行匹配
sift = cv2.xfeatures2d.SIFT_create()
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
kps1, descs1 = sift.detectAndCompute(img1, None)
kps2, descs2 = sift.detectAndCompute(img2, None)
matches = matcher.match(descs1, descs2)
# 使用RANSAC算法进行匹配点筛选
src_pts = np.float32([kps1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kps2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 画出特征点连线图
matchesMask = mask.ravel().tolist()
h, w = img1.shape
draw_params = dict(matchColor=(0, 255, 0), singlePointColor=None, matchesMask=matchesMask, flags=2)
img3 = cv2.drawMatches(img1, kps1, img2, kps2, matches, None, **draw_params)
cv2.imshow('Feature Matching', img3)
# 对图像进行配准和拼接
result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0]))
result[0:img2.shape[0], 0:img2.shape[1]] = img2
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
阅读全文