附录
透视变换矩阵求解代码:
import cv2
import numpy as np
x1=128
y1=162
x2=139
y2=156
x3=127
y3=155
x4=117
y4=159
X1=0.0
Y1=0.0
X2=2.68
Y2=-1.43
X3=0.65
Y3=-1.43
X4=-3.36
Y4=-0.72
# 图像中的点对
image_points = np.array([
[x1, y1],
[x2, y2],
[x3, y3],
[x4, y4]
], dtype=np.float32)
# 实际世界中的点对
world_points = np.array([
[X1, Y1],
[X2, Y2],
[X3, Y3],
[X4, Y4]
], dtype=np.float32)
# 计算单应矩阵
H, status = cv2.findHomography(image_points, world_points)
# 打印单应矩阵
print("Homography matrix:")
print(H)
# 映射一个新的点
point = np.array([74, 217, 1]).reshape(-1, 1)
mapped_point = np.dot(H, point)
mapped_point = mapped_point / mapped_point[2] # 转换为非齐次坐标
# 提取实际世界中的坐标
X = mapped_point[0]
Y = mapped_point[1]
# 打印实际世界中的坐标
print("Mapped point in world coordinates:")
print(f"X: {X}, Y: {Y}")