深度图 + RGB图像 转点云 python

1、深度图(.png)转点云(有内参)

import numpy as np
import open3d as o3d
from PIL import Image

def depth_to_point_cloud(depth_map, fx, fy, cx, cy):
    # h, w = depth_map.shape
    h, w = 1184, 1600
    points = []
    for v in range(h):
        for u in range(w):
            Z = depth_map[v, u]
            X = (u - cx) * Z / fx
            Y = (v - cy) * Z / fy
            points.append([X, Y, Z])
    return np.array(points)

depth_path = r'./depth.png'
depth_map = Image.open(depth_path).convert("L")  # 替换成自己的路径
depth_map = np.array(depth_map)

fx = 2892.33
fy = 2883.18
cx = 823.205
cy = 619.071

points = depth_to_point_cloud(depth_map, fx, fy, cx, cy)
# print(points)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
o3d.io.write_point_cloud('./output.ply', pcd)
# o3d.io.write_point_cloud("./output.pcd", pcd)


2、深度图(.pfm)转点云(有内参)

import re
import numpy as np
import open3d as o3d

def read_pfm(file_path):
    with open(file_path, 'rb') as f:
        color = None
        width = None
        height = None
        scale = None
        endian = None

        header = f.readline().decode('utf-8').rstrip()
        if header == 'PF':
            color = True
        elif header == 'Pf':
            color = False
        else:
            raise Exception('Not a PFM file.')

        dim_match = re.match(r'^(\d+)\s(\d+)\s$', f.readline().decode('utf-8'))
        if dim_match:
            width, height = map(int, dim_match.groups())
        else:
            raise Exception('Malformed PFM header.')

        scale = float(f.readline().decode('utf-8').rstrip())
        if scale < 0:
            endian = '<'
            scale = -scale
        else:
            endian = '>'

        depth_data = np.fromfile(f, endian + 'f')
        shape = (height, width, 3) if color else (height, width)

        depth_data = np.reshape(depth_data, shape)
        depth_data = np

你可能感兴趣的:(python,开发语言)