帮我写一个opencv拼接多张图像的python代码
时间: 2023-03-28 07:03:25 浏览: 109
好的,以下是可以用于拼接多张图像的 Python 代码:
```
import cv2
# 读取多张图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
img3 = cv2.imread('image3.jpg')
# 拼接图像
result = cv2.hconcat([img1, img2, img3])
# 显示拼接后的图像
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
```
希望对您有所帮助。
相关问题
帮我写一个opencv拼接多张图像的python代码,并显示特征匹配图
好的,以下是opencv拼接多张图像的python代码,并显示特征匹配图:
```python
import cv2
import numpy as np
# 读取多张图片
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
img3 = cv2.imread('image3.jpg')
# 创建SIFT特征检测器
sift = cv2.xfeatures2d.SIFT_create()
# 检测关键点和描述符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
kp3, des3 = sift.detectAndCompute(img3, None)
# 创建FLANN匹配器
FLANN_INDEX_KDTREE =
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
# 匹配关键点
matches12 = flann.knnMatch(des1, des2, k=2)
matches23 = flann.knnMatch(des2, des3, k=2)
# 筛选匹配点
good_matches12 = []
good_matches23 = []
for m, n in matches12:
if m.distance < .7 * n.distance:
good_matches12.append(m)
for m, n in matches23:
if m.distance < .7 * n.distance:
good_matches23.append(m)
# 获取匹配点的坐标
src_pts12 = np.float32([kp1[m.queryIdx].pt for m in good_matches12]).reshape(-1, 1, 2)
dst_pts12 = np.float32([kp2[m.trainIdx].pt for m in good_matches12]).reshape(-1, 1, 2)
src_pts23 = np.float32([kp2[m.queryIdx].pt for m in good_matches23]).reshape(-1, 1, 2)
dst_pts23 = np.float32([kp3[m.trainIdx].pt for m in good_matches23]).reshape(-1, 1, 2)
# 计算变换矩阵
M12, mask12 = cv2.findHomography(src_pts12, dst_pts12, cv2.RANSAC, 5.)
M23, mask23 = cv2.findHomography(src_pts23, dst_pts23, cv2.RANSAC, 5.)
# 拼接图像
result12 = cv2.warpPerspective(img1, M12, (img2.shape[1], img2.shape[]))
result23 = cv2.warpPerspective(img2, M23, (img3.shape[1], img3.shape[]))
result = np.concatenate((result12, result23), axis=1)
# 显示特征匹配图
matches_img12 = cv2.drawMatches(img1, kp1, img2, kp2, good_matches12, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
matches_img23 = cv2.drawMatches(img2, kp2, img3, kp3, good_matches23, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
cv2.imshow('Matches12', matches_img12)
cv2.imshow('Matches23', matches_img23)
# 显示拼接结果
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
```
希望能够帮到你!
帮我写一个opencv拼接图像的python代码
好的,以下是opencv拼接图像的python代码:
```python
import cv2
# 读取两张图片
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 拼接图片
result = cv2.hconcat([img1, img2])
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
```
希望能对您有所帮助!
阅读全文