Simple visualization of a 3-d array (volumetric?)

26 views (last 30 days)
I use image(sc) a lot for visualizing the contents of any 2-d array that I may be working on. Sometimes I am working on 3-d arrays, and I have found no simple approach to giving me the same kind of visualization for that case. To give you an idea of what I am thinking of, I have included a link to a figure that sort of does what I want: the data would be a 3-d array (numeric or logical), and for each element the plot would contain a small 3-d cube ("voxel"?) where its color, luminance or transparency would be controlled by the array elements corresponding value. I dont need any fancy smoothing, and I would be happy to control global transparency myself.
I am sure that I could mock something up using a lot of time, but is there no native function or 2-liner that could serve as a starting-point?
http://www.codecogs.com/users/1/cube-969.png
  1 Comment
James Ryland
James Ryland on 11 May 2017
You might want to try this, it is a full fledged matlab application for visualizing 3D density matrices. It was designed to work with fmri and mri but it can handle arbitrary 3D arrays stored as .mat files.
https://www.mathworks.com/matlabcentral/fileexchange/59161-volumetric-3?s_tid=srchtitle

Sign in to comment.

Accepted Answer

Knut
Knut on 27 Feb 2011
Thank you for your answers, but I was not able to use them for my thing. In the end, I wrote a small script for myself that is close, although very brute-force. Worse is that it is not "true" volume, only volume approximated as a number of 3d boxes whose walls opacity is controlled by input matrix values. This means that observed light intensity is dependent on the number of walls one is looking through (and their opacity), not the distance*mean opacity.
I will use this function for debugging and visualizing 3-d arrays, just like I am using image(sc) today for 2-d arrays.
Edit: Changed for a more vectorized script (seems that the low-level calls of patch (OpenGL?)) now is the bottle neck. I will settle for that.
function image3(mat)
%image3
% usage:
% clf
% mat = 0.1*rand(5,6,7);
% mat(2,3,4) = 1;
% image3(mat)
[nx,ny,nz] = size(mat);
c = [1 0 0];
[yrng_org, xrng_org, zrng_org] = meshgrid(1:ny,1:nx,1:nz);
xrng(1,:,:,:) = xrng_org-0.5;
xrng(2,:,:,:) = xrng_org+0.5;
yrng(1,:,:,:) = yrng_org-0.5;
yrng(2,:,:,:) = yrng_org+0.5;
zrng(1,:,:,:) = zrng_org-0.5;
zrng(2,:,:,:) = zrng_org+0.5;
tbl = [1 1 1 2;1 1 2 2; 1 2 2 2; 1 2 1 2];
xtbl = tbl(:,[3 3 4 1 3 3]);
ytbl = tbl(:,[2 2 3 3 4 1]);
ztbl = tbl(:,[4 1 2 2 2 2]);
x_tot = zeros(4,6,nx,ny,nz);
y_tot = x_tot;
z_tot = x_tot;
for vertex = 1:4
for face = 1:6
x_tot(vertex,face,:,:,:) = xrng(xtbl(vertex,face),:,:,:);
y_tot(vertex,face,:,:,:) = yrng(ytbl(vertex,face),:,:,:);
z_tot(vertex,face,:,:,:) = zrng(ztbl(vertex,face),:,:,:);
end
end
x_tot = reshape(x_tot, 4, nx*ny*nz*6);
y_tot = reshape(y_tot, 4, nx*ny*nz*6);
z_tot = reshape(z_tot, 4, nx*ny*nz*6);
h = patch(x_tot,y_tot,z_tot,c);
set(h, 'FaceAlpha', 'flat');
alphamat = (mat(:)*ones(1,6))';
set(h,'FaceVertexAlphaData',alphamat(:))
set(h, 'EdgeAlpha', 0);
xlabel('x');
ylabel('y');
zlabel('z');
axis([0.5 nx+0.5 0.5 ny+0.5 0.5 nz+0.5])
view([41,28]);

More Answers (3)

Jiro Doke
Jiro Doke on 21 Feb 2011
You can use scatter3 to view "voxels" in 3 dimensions, but in your case you have a 3D grid of points which may make it hard to visualize the inside points (because the outside points get in the way).
Instead, you may be interested in a slice or an isosurface.

Walter Roberson
Walter Roberson on 21 Feb 2011

James Ryland
James Ryland on 11 May 2017
You might want to try this, it is a full fledged matlab application for visualizing 3D density matrices. It was designed to work with fmri and mri but it can handle arbitrary 3D arrays stored as .mat files. It can do a very nice realtime display and it can give you raytrace renderings of your objects.
https://www.mathworks.com/matlabcentral/fileexchange/59161-volumetric-3?s_tid=srchtitle

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!