python写tiff遥感影像同名点匹配并用小窗口输出结果
时间: 2023-09-24 21:03:35 浏览: 191
要进行遥感影像同名点匹配,可以使用OpenCV库中的SIFT算法或SURF算法。以下是一个示例代码,其中使用SIFT算法进行匹配:
```python
import cv2
import numpy as np
# 读入两张遥感影像
img1 = cv2.imread('img1.tif', 0)
img2 = cv2.imread('img2.tif', 0)
# 初始化SIFT对象
sift = cv2.xfeatures2d.SIFT_create()
# 检测关键点和描述符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, 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)
# 输出匹配结果
img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, flags=2)
# 显示匹配结果
cv2.imshow('Matched Points', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
要输出结果,可以用滑动窗口的方式截取匹配点周围的区域,并将其保存为新的图片。以下是代码示例:
```python
# 迭代输出所有匹配点的周围小窗口图像
window_size = 50
for m in good:
# 得到匹配点的坐标
img1_x, img1_y = kp1[m.queryIdx].pt
img2_x, img2_y = kp2[m.trainIdx].pt
# 在第一张图片中截取小窗口
x1, y1 = int(img1_x - window_size/2), int(img1_y - window_size/2)
x2, y2 = int(img1_x + window_size/2), int(img1_y + window_size/2)
img1_window = img1[y1:y2, x1:x2]
# 在第二张图片中截取小窗口
x1, y1 = int(img2_x - window_size/2), int(img2_y - window_size/2)
x2, y2 = int(img2_x + window_size/2), int(img2_y + window_size/2)
img2_window = img2[y1:y2, x1:x2]
# 储存小窗口图像
cv2.imwrite(f'match_{m.queryIdx}.tif', img1_window)
cv2.imwrite(f'match_{m.trainIdx}.tif', img2_window)
```
这样就可以输出所有匹配点周围50x50大小的小窗口图像了。
阅读全文