我们认为3D模型是在相机坐标系下的三维点,我们虚拟一个相机点在图上正上方,图像呈现的是3D模型在图像被拍下的效果。由此建立映射关系。
···
def perspective_projection(image_path, bowl_vertices, focal_length,cx,cy):
img = cv2.imread(image_path)
height, width, _ = img.shape
# 构建相机矩阵
camera_matrix = np.array([[focal_length, 0, cx],
[0, focal_length, cy],
[0, 0, 1]], dtype=np.float32)
# 构建畸变系数
dist_coeffs = np.zeros((4, 1))
#旋转向量
R = np.zeros((3, 1))
#平移向量
T = np.zeros((3, 1))
# 将3D碗状模型的顶点坐标转换到2D图像上
img_points, _ = cv2.projectPoints(bowl_vertices, R, T, camera_matrix, dist_coeffs)
# 将顶点坐标转换为整数
img_points = np.int32(img_points).reshape(-1, 2)
# 在图像上绘制3D模型投影
img_proj = img.copy()
for point in img_points:
cv2.circle(img_proj, tuple(point), 2, (0, 0, 255), -1)
cv2.imshow('Perspective Projection', img_proj)
cv2.waitKey(0)
cv2.destroyAllWindows()
```