-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgbd2pointcloud.m
executable file
·152 lines (136 loc) · 4.95 KB
/
rgbd2pointcloud.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%%***********************************************************************%
%* RGBD to point cloud *%
%* Generates point cloud from the color and depth image *%
%* *%
%* *%
%* Author: Dr. Preetham Manjunatha *%
%* Github link: https://github.com/preethamam *%
%* Date: 10/02/2022 *%
%************************************************************************%
%
%************************************************************************%
%
% Usage: obj = rgbd2pointcloud(color, depth, cam_intrinsics, ...
% json_filename)
%
% Properties:
% color - Color image or file name
% depth - Depth image or file name
% json_filename - JSON file of camera intrinsic parameters
% cam_intrinsics - camera intrinsic parameters (if no JSON
% file is provided)
%
% Outputs:
%
% xyz - X, Y and Z points
% rgb - coordinates of all each point in the graph
% Point cloud file - Output point cloud .ply or .pcd file
%
%--------------------------------------------------------------------------
% Example 1: Generate a 3D point cloud without JSON file
% Read the RGB and D images
% color = imread("redwood_1.png");
% depth = imread("redwood_1d.png");
%
% Json filename
% json_filename = [];
%
% Output point cloud filename
% file_name = 'output.pcd';
%
% Camera intrinsics
% camera_intrinsic.cx = 964.957;
% camera_intrinsic.cy = 522.586;
% camera_intrinsic.fx = 1390.53;
% camera_intrinsic.fy = 1386.99;
% camera_intrinsic.depth_scale = 1000; % Depth scale (constant)
% to convert mm to m vice-versa
% camera_intrinsic.width = 640;
% camera_intrinsic.height = 480;
%
% obj = rgbd2pointcloud(color, depth, camera_intrinsic, json_filename);
% [xyz, rgb] = obj.xyz_rgb();
% obj.write2file(xyz, rgb, file_name)
%
%--------------------------------------------------------------------------
%
% Example 2: Generate a 3D point cloud with JSON file
% Read the RGB and D images
% color = imread("redwood_1.png");
% depth = imread("redwood_1d.png");
%
% Json filename
% json_filename = 'camera.json';
%
% Output point cloud filename
% file_name = 'output.pcd';
%
% obj = rgbd2pointcloud(color, depth, camera_intrinsic, json_filename);
% [xyz, rgb] = obj.xyz_rgb();
% obj.write2file(xyz, rgb, file_name)
% Class definition
classdef rgbd2pointcloud < handle
% Public properties
properties
color
depth
json_filename
cam_intrinsics
end
% Public methods
methods
% Constructor
function obj = rgbd2pointcloud(color, depth, cam_intrinsics, json_filename)
if isstring(color)
obj.color = imread(color);
else
obj.color = color;
end
if isstring(depth)
obj.depth = double(imread(depth));
else
obj.depth = double(depth);
end
obj.json_filename = json_filename;
obj.cam_intrinsics = cam_intrinsics;
end
% Get the XYZ and RGB data
function [xyz, rgb] = xyz_rgb(obj)
cam_params = get_camera_intrinsics(obj);
xx = 1 : cam_params.width;
yy = 1 : cam_params.height;
[xv, yv] = meshgrid(xx, yy);
u = reshape(xv', 1, []);
v = reshape(yv', 1, []);
z = reshape(obj.depth', 1, []) / cam_params.depth_scale;
x = (u - cam_params.cx) .* z / cam_params.fx;
y = (v - cam_params.cy) .* z / cam_params.fy;
xyz = [x; y; z]';
rgb = reshape(pagetranspose(obj.color), [], 3);
end
% Write to the point cloud
function write2file(obj, xyz, rgb, file_name)
ptCloud = pointCloud(xyz, Color = rgb);
pcwrite(ptCloud,file_name, 'Encoding', 'compressed')
end
end
% Private methods
methods (Access = private)
% JSON reader
function cam_params = readJSON(obj)
fid = fopen(obj.json_filename);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
cam_params = jsondecode(str);
end
% Get the camera intrinsics
function cam_params = get_camera_intrinsics(obj)
if ~isempty(obj.json_filename)
cam_params = readJSON;
else
cam_params = obj.cam_intrinsics;
end
end
end
end