how to Drawing 3d Voxel data
Show older comments
I want to draw voxels in 3d space (I have them stored in a 3d matrix). i used patch to do this (or voxel function from file exchange, draws every face of each voxel). here is my code
function PlotVoxelinput()
clear all;
clc;
%PlotVoxelInput Summary of this function goes here
% Detailed explanation goes here
voxel_init_loc=[0,0,0];
voxel_loc=voxel_init_loc;
voxel_size=[1,1,1];
file='Other inputs/nucleon.raw';
%file='Lev input/Model 128/bone_model_3061.raw';
fileID=fopen(file);
[A, count] = fread(fileID);
m_hight=nthroot(count,3);
m_width=nthroot(count,3);
m_depth=nthroot(count,3);
%set figure parameters
xlim([0 m_hight]);
ylim([0 m_width]);
zlim([0 m_depth]);
view(45,45);
daspect([1 1 1])
%get 3d data
VoxelMat= multibandread(file,[m_hight,m_width,m_depth],'uint8',0,'bip','ieee-le');
for i=1:m_hight
for j=1:m_width
for k=1:m_depth
if VoxelMat(i,j,k)==1
voxel(voxel_loc,voxel_size,'b',1)
end
voxel_loc=[voxel_loc(1),voxel_loc(2),voxel_loc(3)+1];
end
voxel_loc=[voxel_loc(1),voxel_loc(2)+1,voxel_init_loc(3)];
end
voxel_loc=[voxel_loc(1)+1,voxel_init_loc(2),voxel_init_loc(3)];
end
fclose(fileID);
end
here is voxel function from file exchange
function voxel(i,d,c,alpha);
%VOXEL function to draw a 3-D voxel in a 3-D plot
%
%Usage
% voxel(start,size,color,alpha);
%
% will draw a voxel at 'start' of size 'size' of color 'color' and
% transparency alpha (1 for opaque, 0 for transparent)
% Default size is 1
% Default color is blue
% Default alpha value is 1
%
% start is a three element vector [x,y,z]
% size the a three element vector [dx,dy,dz]
% color is a character string to specify color
% (type 'help plot' to see list of valid colors)
%
%
% voxel([2 3 4],[1 2 3],'r',0.7);
% axis([0 10 0 10 0 10]);
%
% Suresh Joel Apr 15,2003
% Updated Feb 25, 2004
switch(nargin),
case 0
disp('Too few arguements for voxel');
return;
case 1
l=1; %default length of side of voxel is 1
c='b'; %default color of voxel is blue
case 2,
c='b';
case 3,
alpha=1;
case 4,
%do nothing
otherwise
disp('Too many arguements for voxel');
end;
x=[i(1)+[0 0 0 0 d(1) d(1) d(1) d(1)]; ...
i(2)+[0 0 d(2) d(2) 0 0 d(2) d(2)]; ...
i(3)+[0 d(3) 0 d(3) 0 d(3) 0 d(3)]]';
for n=1:3,
if n==3,
x=sortrows(x,[n,1]);
else
x=sortrows(x,[n n+1]);
end;
temp=x(3,:);
x(3,:)=x(4,:);
x(4,:)=temp;
h=patch(x(1:4,1),x(1:4,2),x(1:4,3),c);
set(h,'FaceAlpha',alpha);
temp=x(7,:);
x(7,:)=x(8,:);
x(8,:)=temp;
h=patch(x(5:8,1),x(5:8,2),x(5:8,3),c);
set(h,'FaceAlpha',alpha);
end;
The problem is that matlab crashes if i try to plot a 128X128X128 voxel file (only 2Mb). when i say crashes i mean its simply stuck. nothing happens and it seems most of my computer resources are trying to plot this I am pretty sure its not my pc thats the problem (i7, 8Gb RAM, bench results: 0.2806 0.2195 0.1979 0.2687 0.6053 0.6086). anyone know how can i plot these voxels?
2 Comments
Walter Roberson
on 4 Dec 2012
How much of the matrix is occupied? e.g., what is sum(VoxelMat(:)) ?
If everything was occupied you would be trying to draw two million patches, which would drag down MATLAB graphics for sure.
Itzik Ben Shabat
on 4 Dec 2012
Accepted Answer
More Answers (3)
Badreddine Alane
on 12 May 2019
0 votes
what im doing exactly is that i I have a matrix(i extracted it from 3d point cloud) with 3 columns (x, y and z coordinates) and 41472 lines. So, each line represents one point in a volume .i want to plot a voxel for this point cloud from this matrix, but i think it's not what i should give as an input for the voxel_impoved.(i have tried to draw a voxel for each point,and also tried it for the fulll matrix ) .it's first time for me working on the voxel form, hope someone will help .
3 Comments
Itzik Ben Shabat
on 12 May 2019
Walter Roberson
on 12 May 2019
You can use discretize() on the X, Y, and Z coordinate separately. Then you can accumarray() to generate the volume. (If you were working in 2D you could sparse() instead.)
This presumes that each coordinate triple can be approximated by the cuboid it falls into, which should typically be acceptable for a point cloud.
M.S. Khan
on 30 Aug 2020
i have used it but it tell that memory is out range
Badreddine Alane
on 13 May 2019
Edited: Badreddine Alane
on 13 May 2019
0 votes
so if i understand your answers, my first task is that i have to voxleize the point cloud ?; and if it's can you please guide me how can i voxleize a point cloud
3 Comments
Walter Roberson
on 13 May 2019
Edited: Walter Roberson
on 21 May 2019
I described the steps at https://www.mathworks.com/matlabcentral/answers/55633-how-to-drawing-3d-voxel-data#comment_704380
xbin = discretize(X, x_edges_go_here);
ybin = discretize(Y, y_edges_go_here);
zbin = discretize(Z, z_edges_go_here);
voxelized = accumarray([xbin(:), ybin(:), zbin(:)], 1);
Walter Roberson
on 16 May 2019
x_edges_go_here might be something like
dx = 0.01;
minx = min(x);
maxx = max(x);
first_x = floor(minx / dx) * dx;
last_x = ceil(maxx / dx) * dx;
x_edges_go_here = first_x : dx : last_x;
M.S. Khan
on 30 Aug 2020
Hi Walter. its good but tell me why you took dx =0.01. how if i took dx=0.0001
please guide me. i also want to use voxel to measure volume.
regards,
M.shafiq
Badreddine Alane
on 21 May 2019
0 votes
thank you so much for your help
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!