简体   繁体   English

如何从 Matlab 获取的深度图像和彩色图像生成 3D 点云

[英]How to generate a 3D point cloud from depth image and color image acquired from Matlab

I have 2 set data acquired from Kinect 1- depth image with size 480 640 (uint16) from a scene 2- color image with same size (480 640*3 single) from same scene.我有 2 组数据从 Kinect 获取 1- 深度图像,大小为 480 640 (uint16) 来自场景 2- 彩色图像,大小相同(480 640 * 3 单个)来自同一场景。

How can I merge these data together to generate a colored 3D point clouds with PLY format in Matlab?如何将这些数据合并在一起以在 Matlab 中生成具有 PLY 格式的彩色 3D 点云?

Unfortunately I don't have an access to Kinect anymore and I should use only these data.不幸的是,我无法再访问 Kinect,我应该只使用这些数据。

I've never tried to do that in matlab, but i think that this is what you are looking for: 我从来没有试过在matlab中这样做,但我认为这就是你要找的:

http://es.mathworks.com/help/vision/ref/pcfromkinect.html http://es.mathworks.com/help/vision/ref/pcfromkinect.html

The tool is inside the Computer Vision System Toolbox™. 该工具位于Computer Vision System Toolbox™内。

Nice question. 好问题。 You should use this tutorial from Burrus - basically you need to use your depth information to convert the color/depth centroids into the 3rd dimension. 您应该使用Burrus的本教程 - 基本上您需要使用深度信息将颜色/深度质心转换为第三维。 Be aware that Kinect v1 depth and color streams are slightly mismatched, so take that into account as well. 请注意,Kinect v1深度和颜色流稍有不匹配,因此也要考虑到这一点。

The tutorial can be found here: http://nicolas.burrus.name/index.php/Research/KinectCalibration You can also use the work of this author: Khoshelham, K., & Elberink, SO (2012) - Accuracy and resolution of Kinect depth data for indoor mapping applications 教程可以在这里找到: http ://nicolas.burrus.name/index.php/Research/KinectCalibration您也可以使用这个作者的作品:Khoshelham,K。,&Elberink,SO(2012) - 准确性和分辨率用于室内地图绘制应用的Kinect深度数据

The matlab code should be something like: matlab代码应该是这样的:

% All formulas and values from:
% Khoshelham, K., & Elberink, S. O. (2012).
% Accuracy and resolution of Kinect depth data for indoor mapping applications.
% Sensors (Basel, Switzerland), 12(2), 1437–54. doi:10.3390/s120201437

load('janFrameThousand.mat')
pc=zeros([size(D) 3]);
W=size(D,2);
H=size(D,1);
f=5.453;
for indWidth = 1:W
    for indHeight= 1:H
        % copy z value
        pc(indHeight,indWidth,3)=D(indHeight,indWidth);
        % calc x value
        pc(indHeight,indWidth,1)=-(pc(indHeight,indWidth,3)/f)*...
            ((indWidth-W/2)*0.0093+0.063);
        % calc y value
        pc(indHeight,indWidth,2)=-(pc(indHeight,indWidth,3)/f)*...
            ((indHeight-H/2)*0.0093+0.039);
    end
end
X=pc(:,:,1);
% X=X(:);
Y=pc(:,:,2);
% Y=Y(:);
Z=-pc(:,:,3);
Z(Z==0)=NaN;

Surface=surf(X,Y,Z,'edgecolor','none','facecolor','interp');
lighting gouraud
camlight
% colormap(repmat(winter,20,1))
axis image
axis vis3d
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')

Here is the MATLAB OOP code to convert RGB-D images to point clouds, provided the camera's intrinsic parameters:这是将 RGB-D 图像转换为点云的 MATLAB OOP 代码,提供相机的内在参数:

% 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

Below is the code usage:下面是代码使用:

%% Inputs
% Read the RGB and D images
color = imread("redwood_847.png");
depth = imread("redwood_847d.png");

% Json filename
json_filename = [];

% Output point cloud filename
file_name = 'output.pcd';

% Camera intrinsics
camera_intrinsic.cx              = 319.5;
camera_intrinsic.cy              = 239.5;
camera_intrinsic.fx              = 525;
camera_intrinsic.fy              = 525;
camera_intrinsic.depth_scale     = 1000; % Depth scale (constant) to convert mm to m vice-versa
camera_intrinsic.width           = 640;
camera_intrinsic.height          = 480;

%% Object callback    
obj = rgbd2pointcloud(color, depth, camera_intrinsic, json_filename);
[xyz, rgb] = obj.xyz_rgb();
obj.write2file(xyz, rgb, file_name)

%% Display the point cloud
figure;
pcshow('output.pcd', 'VerticalAxis', 'x', 'VerticalAxisDir', 'down')
xlabel("X")
ylabel("Y")
zlabel("Z")

I have also uploaded this code to the MATLAB File Exchange我也已将此代码上传到MATLAB 文件交换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM