python代码进行图像配准

这段代码演示了如何使用ORB特征检测器和特征匹配来进行图像配准。
图像配准是将两幅图像对齐,使得它们在同一空间中表现出相似的视觉内容。

一、效果图展示

在这里插入图片描述

二、代码

import cv2
import numpy as np

# 读取两张图像
# image1 是RGB  image2 是高光谱相机拍的伪RGB
# iamge1 和 iamge2 尺寸可以是不一样的
image1 = cv2.imread('datasets/image/ccc.bmp', cv2.IMREAD_COLOR)
image2 = cv2.imread('datasets/image/image.png', cv2.IMREAD_COLOR)
# 创建ORB特征检测器
orb = cv2.ORB_create()

# 在两张图像中寻找特征点
kp1, des1 = orb.detectAndCompute(image1, None)
kp2, des2 = orb.detectAndCompute(image2, None)

# 创建BF匹配器
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# 特征匹配
matches = bf.match(des1, des2)

# 将匹配结果按照距离排序
matches = sorted(matches, key=lambda x: x.distance)

# 获取匹配点对的坐标
src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

# 计算透视变换矩阵
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

# 计算逆变换矩阵
M_inv = np.linalg.inv(M)

# 对第一张图像进行透视变换,映射到第二张图像上
result_gray = cv2.warpPerspective(image1, M, (image2.shape[1], image2.shape[0]))
print(result_gray.shape)
cv2.imshow('Cropped Area', result_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 将裁剪的结果保存为彩色图像
cv2.imwrite('datasets/image/cropped_area.jpg', result_gray)


  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
图像配准是将多个图像对齐使得它们在同一几何坐标系下具有相似的形状、大小和方向。以下是使用Python实现图像配准的步骤和代码示例: 1. 导入所需的库: ```python import cv2 import numpy as np ``` 2. 加载待配准的图像: ```python image1 = cv2.imread('image1.jpg', 0) image2 = cv2.imread('image2.jpg', 0) ``` 3. 提取关键特征点: ```python # 创建ORB特征检测器 orb = cv2.ORB_create() # 在图像中检测关键特征点和描述符 keypoints1, descriptors1 = orb.detectAndCompute(image1, None) keypoints2, descriptors2 = orb.detectAndCompute(image2, None) ``` 4. 使用特征描述符进行匹配: ```python # 创建BFMatcher对象 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # 使用描述符进行匹配 matches = bf.match(descriptors1, descriptors2) # 按照匹配程度排序 matches = sorted(matches, key=lambda x: x.distance) # 选择前n个最佳匹配 n = 50 best_matches = matches[:n] # 提取关键点位置 points1 = np.float32([keypoints1[m.queryIdx].pt for m in best_matches]).reshape(-1, 1, 2) points2 = np.float32([keypoints2[m.trainIdx].pt for m in best_matches]).reshape(-1, 1, 2) ``` 5. 使用找到的匹配点对进行图像配准: ```python # 使用RANSAC算法估计单应性矩阵 homography, _ = cv2.findHomography(points1, points2, cv2.RANSAC) # 对第一个图像进行透视变换 aligned_image = cv2.warpPerspective(image1, homography, (image2.shape[1], image2.shape[0])) ``` 完整的代码示例: ```python import cv2 import numpy as np # 加载图像 image1 = cv2.imread('image1.jpg', 0) image2 = cv2.imread('image2.jpg', 0) # 创建ORB特征检测器 orb = cv2.ORB_create() # 在图像中检测关键特征点和描述符 keypoints1, descriptors1 = orb.detectAndCompute(image1, None) keypoints2, descriptors2 = orb.detectAndCompute(image2, None) # 创建BFMatcher对象 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # 使用描述符进行匹配 matches = bf.match(descriptors1, descriptors2) # 按照匹配程度排序 matches = sorted(matches, key=lambda x: x.distance) # 选择前n个最佳匹配 n = 50 best_matches = matches[:n] # 提取关键点位置 points1 = np.float32([keypoints1[m.queryIdx].pt for m in best_matches]).reshape(-1, 1, 2) points2 = np.float32([keypoints2[m.trainIdx].pt for m in best_matches]).reshape(-1, 1, 2) # 使用RANSAC算法估计单应性矩阵 homography, _ = cv2.findHomography(points1, points2, cv2.RANSAC) # 对第一个图像进行透视变换 aligned_image = cv2.warpPerspective(image1, homography, (image2.shape[1], image2.shape[0])) # 显示配准后的图像 cv2.imshow('Al
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值