Skip to content

Commit

Permalink
Formatted with industry standard.
Browse files Browse the repository at this point in the history
  • Loading branch information
preethamam committed Oct 4, 2022
1 parent 0aa22d0 commit c4c7b10
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 65 deletions.
8 changes: 8 additions & 0 deletions constants.py
@@ -0,0 +1,8 @@
# Depth scale (constant) to convert mm to m vice-versa
DEPTH_SCALE = 1000

# JSON file of camera intrinsics
JSON_FILE = "camera.json"

# Output file name
PCD_FILENAME = "output_py.pcd"
29 changes: 29 additions & 0 deletions main.py
@@ -0,0 +1,29 @@
from PIL import Image
from numpy import asarray
from constants import *

from rgbd2pointcloud import RGBD2Pointcloud


if __name__ == "__main__":

# Inputs
# Read color image
color = Image.open("redwood_847.png")

# Read depth image
depth = Image.open("redwood_847d.png")

# Convert to nnumpy array
color = asarray(color)
depth = asarray(depth)

# Convert RGBD to point cloud and write
# Object files
pcd = RGBD2Pointcloud(JSON_FILE, color, depth, DEPTH_SCALE)

# Get the XYZ annd RGB files
xyzRGB = pcd.xyz_rgb()

# Write to point cloud file
pcd.write2file(xyzRGB, PCD_FILENAME)
123 changes: 58 additions & 65 deletions rgbd2pointcloud.py 100755 → 100644
@@ -1,92 +1,85 @@
import open3d as o3d
import numpy as np
import json
import os

from PIL import Image
from numpy import asarray

# ----------------------------------------------------------------------------------------------------------
# Inputs
# ----------------------------------------------------------------------------------------------------------
# Read color image
color = Image.open("redwood_847.png")

# Read depth image
depth = Image.open("redwood_847d.png")

# Depth scale (constant) to convert mm to m vice-versa
depth_scale = 1000

# JSON file of camera intrinsics
json_file = 'camera.json'

# Output file name
pcd_filename = 'output_py.pcd'
import numpy as np
import open3d as o3d

# Convert to nnumpy array
color = asarray(color)
depth = asarray(depth)
from constants import JSON_FILE

# ----------------------------------------------------------------------------------------------------------
# RGBD to pointcloud class
# ----------------------------------------------------------------------------------------------------------
class RGBD2Pointcloud:
# Constructor
def __init__(self, json_filename, color_image, depth_image, depth_scale = 1000):
self.json_filename = json_filename
self.depth_scale = depth_scale
self.color_image = np.reshape(np.array(color_image), (-1,3), order='C')
self.depth_image = np.reshape(np.array(depth_image), -1, order='C')

def __init__(
self,
json_filename: str,
color_image: np.uint8,
depth_image: np.uint16,
depth_scale: int = 1000,
) -> None:
"""Init xyz
Args:
json_filename (str): _description_
color_image (np.uint8): _description_
depth_image (np.uint8): _description_
depth_scale (int, optional): _description_. Defaults to 1000.
"""
self.json_filename = json_filename
self.depth_scale = depth_scale
self.color_image = np.reshape(np.array(color_image), (-1, 3), order="C")
self.depth_image = np.reshape(np.array(depth_image), -1, order="C")

# Camera intrinsics
def __get_camera_intrincs(self):
camera_intrinsic = json.load(open(json_file))
cx = camera_intrinsic['cx']
cy = camera_intrinsic['cy']
fx = camera_intrinsic['fx']
fy = camera_intrinsic['fy']
width = camera_intrinsic['width']
height = camera_intrinsic['height']
@staticmethod
def __get_camera_intrincs():
"""_summary_
Returns:
_type_: _description_
"""
camera_intrinsic = json.load(open(JSON_FILE))
cx = camera_intrinsic["cx"]
cy = camera_intrinsic["cy"]
fx = camera_intrinsic["fx"]
fy = camera_intrinsic["fy"]
width = camera_intrinsic["width"]
height = camera_intrinsic["height"]

return cx, cy, fx, fy, width, height

# XYZ and RGB data
def xyz_rgb(self):
"""_summary_
Returns:
_type_: _description_
"""
cx, cy, fx, fy, width, height = self.__get_camera_intrincs()

xx = np.arange(0, width)
yy = np.arange(0, height)
xv, yv = np.meshgrid(xx, yy)
u = np.reshape(xv, -1, order='C')
v = np.reshape(yv, -1, order='C')

u = np.reshape(xv, -1, order="C")
v = np.reshape(yv, -1, order="C")

z = self.depth_image / self.depth_scale
x = (u - cx) * z / fx
y = (v - cy) * z / fy

xyzRGB = np.concatenate((np.vstack((x, y, z)).T,
self.color_image), axis=1)

xyzRGB = np.concatenate((np.vstack((x, y, z)).T, self.color_image), axis=1)
return xyzRGB

# Write the point cloud data
def write2file(self, xyzRGB, file_name):
@staticmethod
def write2file(xyzRGB: np.float32, file_name: str):
"""_summary_
Args:
xyzRGB (_type_): _description_
file_name (_type_): _description_
"""
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyzRGB[:,0:3])
pcd.colors = o3d.utility.Vector3dVector(xyzRGB[:,3:6] / 255.0)
pcd.points = o3d.utility.Vector3dVector(xyzRGB[:, 0:3])
pcd.colors = o3d.utility.Vector3dVector(xyzRGB[:, 3:6] / 255.0)
o3d.io.write_point_cloud(file_name, pcd)


# ----------------------------------------------------------------------------------------------------------
# Convert RGBD to point cloud and write
# ----------------------------------------------------------------------------------------------------------
# Object files
pcd = RGBD2Pointcloud(json_file, color, depth, depth_scale)

# Get the XYZ annd RGB files
xyzRGB = pcd.xyz_rgb()

# Write to point cloud file
pcd.write2file(xyzRGB, pcd_filename)

0 comments on commit c4c7b10

Please sign in to comment.