非水平的代码
时间: 2023-07-02 18:23:54 浏览: 49
以下是适用于任意角度拍摄的全景图拼接和图像融合代码示例:
```python
import cv2
import numpy as np
# 读取两张图片
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# 提取关键点和描述符
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
# 匹配关键点
bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
matches = bf.match(des1, des2)
# 筛选匹配点
matches = sorted(matches, key=lambda x:x.distance)
good_matches = []
for m in matches:
if m.distance < 0.7 * matches[0].distance:
good_matches.append(m)
# 计算变换矩阵
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 计算图像拼接后的大小
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
pts1 = np.float32([[0, 0], [0, h1], [w1, h1], [w1, 0]]).reshape(-1, 1, 2)
pts2 = np.float32([[0, 0], [0, h2], [w2, h2], [w2, 0]]).reshape(-1, 1, 2)
pts1_warped = cv2.perspectiveTransform(pts1, M)
pts = np.concatenate((pts1_warped, pts2), axis=0)
[xmin, ymin] = np.int32(pts.min(axis=0).ravel() - 0.5)
[xmax, ymax] = np.int32(pts.max(axis=0).ravel() + 0.5)
t = [-xmin, -ymin]
M_translate = np.array([[1, 0, t[0]], [0, 1, t[1]], [0, 0, 1]])
img1_warped = cv2.warpPerspective(img1, M_translate.dot(M), (xmax-xmin, ymax-ymin))
img1_warped[t[1]:h1+t[1], t[0]:w1+t[0]] = img2
# 图像融合
gray = cv2.cvtColor(img1_warped, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
ret, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
mask = cv2.merge([mask, mask, mask])
result = cv2.seamlessClone(img1_warped, img2, mask, (w2, h2), cv2.NORMAL_CLONE)
# 显示结果
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
与水平拍摄的代码相比,主要的区别在于:
1. 计算变换矩阵后,需要考虑图像拼接后的大小,通过计算所有关键点在图像中的位置,从而得到图像拼接后的最小和最大坐标。
2. 在图像拼接完成后,需要进行平移操作,将拼接后的图像移动到第一张图像的位置。
3. 进行图像融合时,需要使用第二张图像的大小作为目标图像的大小。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)