怎样通过视频创建3d模子

[复制链接]
发表于 2026-1-4 15:31:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
通过视频创建3D模子通常包罗几个关键步调:从视频中提取帧、对帧中的物体举行特性提取、将多帧中的信息团结起来规复三维布局。Python中有一些库和工具可以资助实现这个过程,比方OpenCV、Open3D、COLMAP等。以下是一个简化的流程和代码框架:
 步调概述

1. 从视频中提取帧:使用OpenCV从视频中逐帧提取图像。
2. 特性提取与匹配:使用SIFT(Scale-Invariant Feature Transform)或ORB(Oriented FAST and Rotated BRIEF)等算法提取图像特性,并匹配相邻帧的特性。
3. 盘算相机位姿:使用图像特性盘算相机在空间中的运动。
4. 三角化规复3D点云:通过多视角的图像特性来三角化3D点。
5. 重修3D模子:将点云转化为可视化的3D模子。


实现步调

 1. 从视频中提取帧
  1. import cv2
  2. def extract_frames(video_path, output_dir):
  3.     cap = cv2.VideoCapture(video_path)
  4.     frame_count = 0
  5.     while cap.isOpened():
  6.         ret, frame = cap.read()
  7.         if not ret:
  8.             break
  9.         frame_path = f"{output_dir}/frame_{frame_count:04d}.png"
  10.         cv2.imwrite(frame_path, frame)
  11.         frame_count += 1
  12.     cap.release()
  13. #使用示例
  14. extract_frames('input_video.mp4', 'output_frames')
复制代码
 2. 特性提取与匹配
  1. import cv2
  2. def detect_and_match_features(img1, img2):
  3.     # 使用ORB特征检测
  4.     orb = cv2.ORB_create()
  5.     kp1, des1 = orb.detectAndCompute(img1, None)
  6.     kp2, des2 = orb.detectAndCompute(img2, None)
  7.     # 使用Brute-Force匹配
  8.     bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  9.     matches = bf.match(des1, des2)
  10.     matches = sorted(matches, key=lambda x: x.distance)
  11.     # 绘制匹配结果
  12.     img_matches = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
  13.     cv2.imshow("Matches", img_matches)
  14.     cv2.waitKey(0)
  15.     cv2.destroyAllWindows()
  16.     return kp1, kp2, matches
  17. # 示例
  18. img1 = cv2.imread('output_frames/frame_0000.png', cv2.IMREAD_GRAYSCALE)
  19. img2 = cv2.imread('output_frames/frame_0001.png', cv2.IMREAD_GRAYSCALE)
  20. detect_and_match_features(img1, img2)
复制代码
 
3. 盘算相机位姿
  1. import numpy as np
  2. def compute_camera_pose(kp1, kp2, matches, K):
  3.     # 从匹配的关键点中提取坐标
  4.     pts1 = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
  5.     pts2 = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
  6.     # 计算本质矩阵
  7.     E, mask = cv2.findEssentialMat(pts1, pts2, K)
  8.     # 恢复相机位姿
  9.     _, R, t, _ = cv2.recoverPose(E, pts1, pts2, K)
  10.     
  11.     return R, t
  12. # 示例相机内参矩阵
  13. K = np.array([[1000, 0, 640],
  14.               [0, 1000, 360],
  15.               [0, 0, 1]])
  16. # 示例调用
  17. kp1, kp2, matches = detect_and_match_features(img1, img2)
  18. R, t = compute_camera_pose(kp1, kp2, matches, K)
  19. print("Rotation Matrix:\n", R)
  20. print("Translation Vector:\n", t)
复制代码
4. 三角化规复3D点云
  1. def triangulate_points(kp1, kp2, matches, K, R, t):
  2.     pts1 = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 2)
  3.     pts2 = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 2)
  4.     # 将像素坐标转换为归一化图像坐标
  5.     pts1_norm = cv2.undistortPoints(np.expand_dims(pts1, axis=1), K, None)
  6.     pts2_norm = cv2.undistortPoints(np.expand_dims(pts2, axis=1), K, None)
  7.     # 相机投影矩阵
  8.     P1 = np.hstack((np.eye(3), np.zeros((3, 1))))
  9.     P2 = np.hstack((R, t))
  10.     # 三角化点
  11.     points_4d_hom = cv2.triangulatePoints(P1, P2, pts1_norm, pts2_norm)
  12.     points_3d = points_4d_hom[:3] / points_4d_hom[3]
  13.     return points_3d.T
  14. # 示例调用
  15. points_3d = triangulate_points(kp1, kp2, matches, K, R, t)
  16. print("3D Points:\n", points_3d)
复制代码
 
 5. 使用Open3D展示3D点云
  1. import open3d as o3d
  2. import numpy as np
  3. def visualize_point_cloud(points):
  4.     # 创建点云对象
  5.     pcd = o3d.geometry.PointCloud()
  6.     pcd.points = o3d.utility.Vector3dVector(points)
  7.     
  8.     # 可视化点云
  9.     o3d.visualization.draw_geometries([pcd])
  10. # 示例调用
  11. visualize_point_cloud(points_3d)
复制代码
 
 总结

1. 从视频中提取帧并举行特性匹配。
2. 使用这些匹配特性来估计相机的相对位姿。
3. 使用相机位姿和匹配特性三角化出3D点。
4. 末了用Open3D库可视化天生的3D点云。
这是一个底子的Python框架,可以在此底子上举行优化和扩展,比方加入更多的图像处置惩罚技能或使用更高级的3D重修算法。

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表