利用python,opencv实现图像拼接与融合,以及黑边处理
时间: 2024-03-18 07:42:37 浏览: 293
可以通过以下步骤来实现图像拼接、融合和黑边处理:
1. 读取需要拼接的两张图像并将它们转换为灰度图像。
```python
import cv2
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
```
2. 通过 SIFT 算法来匹配两张图像中的关键点。
```python
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
```
3. 获取两张图像中的关键点坐标并计算出变换矩阵。
```python
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)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
```
4. 将第二张图像投影到第一张图像的平面上,并创建一个新的图像来存储拼接结果。
```python
h, w = gray1.shape
result = cv2.warpPerspective(img2, M, (w + img2.shape[1], h))
result[0:h, 0:w] = img1
```
5. 进行图像融合,使用掩码来避免边缘出现黑边。
```python
mask = np.zeros(gray1.shape, dtype=np.uint8)
mask[0:h, 0:w] = 255
mask = cv2.warpPerspective(mask, M, (w + img2.shape[1], h))
result_masked = cv2.bitwise_and(result, result, mask=mask)
```
6. 去除图像的黑边。
```python
gray = cv2.cvtColor(result_masked, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
result_cropped = result_masked[y:y+h, x:x+w]
```
7. 保存拼接、融合和去黑边后的图像。
```python
cv2.imwrite('result.jpg', result_cropped)
```
完整代码如下:
```python
import cv2
import numpy as np
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
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)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
h, w = gray1.shape
result = cv2.warpPerspective(img2, M, (w + img2.shape[1], h))
result[0:h, 0:w] = img1
mask = np.zeros(gray1.shape, dtype=np.uint8)
mask[0:h, 0:w] = 255
mask = cv2.warpPerspective(mask, M, (w + img2.shape[1], h))
result_masked = cv2.bitwise_and(result, result, mask=mask)
gray = cv2.cvtColor(result_masked, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
result_cropped = result_masked[y:y+h, x:x+w]
cv2.imwrite('result.jpg', result_cropped)
```
注意:在某些情况下,可能需要对图像进行调整或剪裁才能获得更好的拼接、融合和去黑边效果。
阅读全文