import cv2
import numpy as np
# 读取图像并转为灰度
img1 = cv2.imread('output_image_rotate6.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('output_image_standard6.jpg', cv2.IMREAD_GRAYSCALE)
# 初始化SIFT检测器
sift = cv2.SIFT_create()
# 检测关键点并计算描述符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
# 使用暴力匹配器进行特征匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
# np.float64(-2.5418798426532048), np.float64(-0.7227428481105767)
result = 2.5418798426532048 / 0.7227428481105767
print(result) # 输出: 3.3333333333333335
print("result=",result)
# 应用比率测试筛选优质匹配
good_matches = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good_matches.append(m)
# 至少需要2个匹配点来估计变换
if len(good_matches) > 4:
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, _ = cv2.estimateAffinePartial2D(src_pts, dst_pts)
print(M)
if M is not None:
# 提取平移参数
tx = M[0, 2]
ty = M[1, 2]
# 提取旋转角度(弧度转角度)
angle_rad = np.arctan2(M[1, 0], M[0, 0])
angle_deg = np.degrees(angle_rad)
print(tx)
print(ty)
print(f"平移距离:Δx = {tx:.2f} 像素, Δy = {ty:.2f} 像素")
print(f"旋转角度:{angle_deg:.2f} 度")
print(ty/tx)
else:
print("无法估计变换矩阵")
else:
print("匹配点不足,无法对齐")
python如何将2个lidar点云图像对齐
于 2025-04-04 11:04:09 首次发布