在 MATLAB 中构建 3D 立方体的体素 [英] Constructing voxels of a 3D cube in MATLAB

查看:67
本文介绍了在 MATLAB 中构建 3D 立方体的体素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MATLAB 中构建一个 3D 立方体.我知道任何 3D 形状的单位都是体素而不是像素.这就是我想要做的,

首先,我想构造一个具有给定维度 x、y 和 z 的立方体.

其次,根据我从不同的图像处理教程中了解到的,这个立方体必须由体素(3D 像素)组成.我想给每个体素一个初始颜色值,比如灰色.

第三,我想访问每个体素并改变它的颜色,但我想区分代表立方体面的体素和代表内部区域的体素.我想按每个体素的位置 x、y、z 轴.最后,我们将得到一个具有不同颜色区域的立方体.

我已经搜索了很多但找不到实现它的好方法,但是这里给出的代码在构建立方体的内部区域方面似乎非常接近,

如果您对绘制体素感兴趣,我通常会建议您使用

但是是的,这看起来仍然很糟糕.因为如果你想看到内部,体素绘图不是最好的选择.不同面孔的 Alpha 一个叠在一起,解决这个问题的唯一方法是编写先进的计算机图形光线追踪算法,相信我,这是一条漫长而艰难的道路.

通常有 4D 数据,因此数据包含 3D 位置和每个位置的单个数据.有人可能会认为,在这种情况下,您真的需要体素,因为每个体素都有 3D + 颜色、4D 数据.的确!你可以用体素来做到这一点,但有时用其他方式来描述它会更好.举个例子,让我们看看这个人想要在他/她的 4D 空间中突出显示一个区域

在我看来,这个看起来好多了.

自由选择,好图!

I want to construct a 3D cube in MATLAB. I know that the units of any 3D shape are voxels not pixels. Here is what I want to do,

First, I want to construct a cube with some given dimensions x, y, and z.

Second, according to what I understand from different image processing tutorials, this cube must consists of voxels (3D pixels). I want to give every voxel an initial color value, say gray.

Third, I want to access every voxel and change its color, but I want to distinguish the voxels that represent the faces of the cube from those that represent the internal region. I want to axis every voxel by its position x,y, z. At the end, we will end up with a cube that have different colors regions.

I've searched a lot but couldn't find a good way to implement that, but the code given here seems very close in regard to constructing the internal region of the cube,

http://www.mathworks.com/matlabcentral/fileexchange/3280-voxel

But it's not clear to me how it performs the process.

Can anyone tell me how to build such a cube in MATLAB?

Thanks.

解决方案

You want to plot voxels! Good! Lets see how we can do this stuff.

First of all: yeah, the unit of 3D shapes may be voxels, but they don't need to be. You can plot an sphere in 3D without it being "blocky", thus you dont need to describe it in term of voxels, the same way you don't need to describe a sinusoidal wave in term of pixels to be able to plot it on screen. Look at the figure below. (same happens for cubes)

If you are interested in drawing voxels, I generally would recommend you to use vol3D v2 from Matlab's FEX. Why that instead of your own?

Because the best (only?) way of plotting voxels is actually plotting flat square surfaces, 6 for each cube (see answer here for function that does that). This flat surfaces will also create some artifacts for something called z-fighting in computer graphics. vol3D actually only plots 3 surfaces, the ones looking at you, saving half of the computational time, and avoiding ugly plotting artifacts. It is easy to use, you can define colors per voxel and also the alpha (transparency) of each of them, allowing you to see inside.

Example of use:

% numbers are arbitrary
cube=zeros(11,11,11);
cube(3:9,3:9,3:9)=5; % Create a cube inside the region

% Boring: faces of the cube are a different color.
cube(3:9,3:9,3)=2;
cube(3:9,3:9,9)=2;
cube(3:9,3,3:9)=2;
cube(3:9,9,3:9)=2;
cube(3,3:9,3:9)=2;
cube(9,3:9,3:9)=2;

vold3d('Cdata',cube,'alpha',cube/5)

But yeah, that still looks bad. Because if you want to see the inside, voxel plotting is not the best option. Alphas of different faces stack one on top of the other and the only way of solving this is writing advanced computer graphics ray tracing algorithms, and trust me, that's a long and tough road to take.

Very often one has 4D data, thus data that contains 3D location and a single data for each of the locations. One may think that in this case, you really want voxels, as each of them have a 3D +color, 4D data. Indeed! you can do it with voxels, but sometimes its better to describe it in some other ways. As an example, lets see this person who wanted to highlight a region in his/hers 4D space (link). To see a bigger list I suggest you look at my answer in here about 4D visualization techniques.

Lets try wits a different approach than the voxel one. Lets use the previous cube and create isosurfaces whenever the 4D data changes of value.

iso1=isosurface(cube,1);
iso2=isosurface(cube,4);
p1=patch(iso1,'facecolor','r','facealpha',0.3,'linestyle','none');
p2=patch(iso2,'facecolor','g','facealpha',1,'linestyle','none');

% below here is code for it to look "fancy"
isonormals(cube,p1)

view(3); 
axis tight
axis equal
axis off
camlight 
lighting gouraud

And this one looks way better, in my opinion.

Choose freely and good plotting!

这篇关于在 MATLAB 中构建 3D 立方体的体素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆