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

26 次查看(过去 30 天)
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 个评论
James Ryland
James Ryland 2017-5-11
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

请先登录,再进行评论。

采纳的回答

Knut
Knut 2011-2-27
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]);

更多回答(3 个)

Jiro Doke
Jiro Doke 2011-2-21
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 2011-2-21

James Ryland
James Ryland 2017-5-11
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

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by