3d投影到2d python opencv

目录

[cv2.projectPoints 投影](#cv2.projectPoints 投影)

矩阵计算投影


cv2.projectPoints 投影

cv2.projectPoints() 是 OpenCV 中的一个函数,用于将三维空间中的点(3D points)投影到二维图像平面上。这在计算机视觉中经常用于相机标定、物体姿态估计、3D物体与2D图像之间的映射等场景。

函数原型:

cv2.projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs)

objectPoints:3D点的集合,通常是物体的真实世界坐标。

rvec:旋转向量,表示物体相对于相机的旋转。

tvec:平移向量,表示物体相对于相机的位置。

cameraMatrix:相机的内参矩阵,通常通过相机标定得到。

distCoeffs:相机的畸变系数,通常是由相机标定得到的。

python 复制代码
import cv2
import numpy as np

# 定义 3D 点(假设这些点在一个立方体的表面上)
object_points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, -1], [1, 0, -1], [1, 1, -1], [0, 1, -1]], dtype=np.float32)

# 定义相机内参矩阵
camera_matrix = np.array([[1000, 0, 320],  # fx, 0, cx
                                [0, 1000, 240],  # 0, fy, cy
                                [0, 0, 1]  # 0, 0, 1
], dtype=np.float32)

# 定义畸变系数(假设无畸变)
dist_coeffs = np.zeros((5, 1), dtype=np.float32)

# 定义相机外参(旋转向量和平移向量)
rvec = np.array([0, 0, 0], dtype=np.float32)  # 无旋转
tvec = np.array([0, 0, -10], dtype=np.float32)  # 相机在 Z 轴正方向 5 个单位处

# 将 3D 点投影到 2D 图像平面
image_points, _ = cv2.projectPoints(object_points, rvec, tvec, camera_matrix, dist_coeffs)

# 创建一个空白图像(用于可视化)
image = np.zeros((480, 640, 3), dtype=np.uint8)

image_points=np.squeeze(image_points,axis=1)
print(image_points)
# 在图像上绘制投影点
for point in image_points:
    x, y = point.ravel()
    cv2.circle(image, (int(x), int(y)), 3, (0, 255, 0), -1)  # 绘制绿色圆点

# 显示图像
cv2.imshow("Projected Points", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

矩阵计算投影

内参,外参用的左乘

python 复制代码
import numpy as np
import cv2

# 定义相机内参矩阵 (3x3)
K = np.array([[1000, 0, 320],  # fx, 0, cx
              [0, 1000, 240],  # 0, fy, cy
              [0, 0, 1]])  # 0, 0, 1

# 定义相机外参:旋转矩阵 (3x3) 和平移向量 (3x1)
R = np.eye(3)  # 假设相机没有旋转
t = np.array([[0], [0], [-10]])  # 相机在Z轴负方向平移10个单位

# 生成随机3D点云 (Nx3)
num_points = 100
# points_3d = np.random.rand(num_points, 3) * 10  # 生成100个3D点,范围在[0, 10)

points_3d = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, -1], [1, 0, -1], [1, 1, -1], [0, 1, -1]], dtype=np.float32)


# 将3D点云从世界坐标系转换到相机坐标系
points_3d_cam = R @ points_3d.T + t  # 3xN
points_3d_cam = points_3d_cam.T  # 转置为Nx3

# 将3D点云投影到2D图像平面
points_2d_homogeneous = K @ points_3d_cam.T  # 3xN
points_2d = points_2d_homogeneous[:2, :] / points_2d_homogeneous[2, :]  # 归一化
points_2d = points_2d.T  # 转置为Nx2

# 创建空白图像
image_size = (640, 480)  # 图像尺寸
image = np.zeros((image_size[1], image_size[0], 3), dtype=np.uint8)

print(points_2d)
# 将2D点绘制到图像上
for point in points_2d:
    x, y = int(point[0]), int(point[1])
    if 0 <= x < image_size[0] and 0 <= y < image_size[1]:  # 确保点在图像范围内
        cv2.circle(image, (x, y), 3, (0, 255, 0), -1)  # 绘制绿色圆点

# 显示图像
cv2.imshow("2D Projection of Point Cloud", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

总结,两种方法的结果是一样的。

相关推荐
LitchiCheng20 小时前
Mujoco 开源机械臂 RL 强化学习避障、绕障
人工智能·python·开源
A先生的AI之旅20 小时前
2026-1-30 LingBot-VA解读
人工智能·pytorch·python·深度学习·神经网络
Learn Beyond Limits20 小时前
文献阅读:A Probabilistic U-Net for Segmentation of Ambiguous Images
论文阅读·人工智能·深度学习·算法·机器学习·计算机视觉·ai
丝瓜蛋汤20 小时前
微调生成特定写作风格助手
人工智能·python
OpenMiniServer20 小时前
电气化能源革命下的社会
java·人工智能·能源
猿小羽20 小时前
探索 Codex:AI 编程助手的未来潜力
人工智能·openai·代码生成·codex·ai编程助手
菜青虫嘟嘟20 小时前
Expert Iteration:一种无需人工标注即可显著提升大语言模型推理能力的简单方法核心
人工智能·语言模型·自然语言处理
玄同76520 小时前
LangChain v1.0+ Retrieval模块完全指南:从文档加载到RAG实战
人工智能·langchain·知识图谱·embedding·知识库·向量数据库·rag
deepdata_cn20 小时前
为什么AI需要因果?
人工智能·因果学习
说私域21 小时前
社群招募文案的核心构建要点与工具赋能路径——基于AI智能名片链动2+1模式商城小程序的实践研究
人工智能·小程序·私域运营