简体   繁体   English

从Matlab中的多个球体图中提取横截面

[英]Extract cross sections from a plot of multiple spheres in Matlab

I know the locations of spheres (center and radius) in a box. 我知道一个盒子中球体的位置(中心和半径)。 I want to extract cross sections. 我想提取横截面。 I am able to plot the spheres placed in a cube using the following Matlab code: 我可以使用以下Matlab代码绘制放置在立方体中的球体:

[X,Y,Z] = sphere;
for SpNum = 1:NumSpheres
    surf( X*Radius(SpNum)+Center(SpNum,1), Y*Radius(SpNum)+Center(SpNum,2), Z*Radius(SpNum)+Center(SpNum,3), ...
    'FaceColor','r' );
%shading interp;
hold on;
end
axis tight; daspect([1 1 1]);

In the above code, each sphere could have different radius and they do not overlap (so the centers are also different). 在上面的代码中,每个球体可以具有不同的半径,并且它们不重叠(因此中心也不同)。

The above code does not however generate cross sections. 但是,上面的代码不会生成横截面。 I want to extract cross sections similar to what we get from say X-ray CT data: a series of images in the Z-direction. 我想提取类似于我们从X射线CT数据中得到的横截面:Z方向上的一系列图像。 I think 'interp2/interp3' and 'slice' functions are the relevant functions, but I am not sure how to use them to generate the cross sections. 我认为'interp2 / interp3'和'slice'函数是相关的函数,但是我不确定如何使用它们生成横截面。 I would appreciate if anyone could give pointers or provide some sample code for my problem? 如果有人可以为我的问题提供指针或提供一些示例代码,我将不胜感激。

-- Thanks in advance. - 提前致谢。

Update: 更新:

I tried using meshgrid to generate the grid points followed by the function F(X,Y,Z) as follows: 我尝试使用meshgrid生成网格点,然后生成函数F(X,Y,Z),如下所示:

[X,Y,Z] = meshgrid(1:100,1:100,1:100);
F = zeros(size(X),'uint8');
for SpNum = 1:NumSpheres
    F( sqrt((X - Center(SpNum,1)).^2 + (Y - Center(SpNum,2)).^2 + (Z -     Center(SpNum,3)).^2) <= Radius(SpNum) ) = 1;
end
surf(F);

followed by: 其次是:

z = 1;
I = interp3(X, Y, Z, X*Radius(SpNum)+Center(SpNum,1), Y*Radius(SpNum)+Center(SpNum,2), Z*Radius(SpNum)+Center(SpNum,3), z, 'spline');
figure, imshow(I);

I know that interp3 is the function to use since it interpolates the values of the function F(X,Y,Z) which represent the spheres at different location within a bounded box (say 1:100, 1:100, 1:100). 我知道interp3是要使用的函数,因为它会插值函数F(X,Y,Z)的值,这些函数表示有界框内不同位置的球体(例如1:100、1:100、1:100) 。 The interpolated values at particular 'z' (= 1, 2, 3... 100) should give me 100 cross sections (in the form of 2-D images). 特定“ z”(= 1、2、3 ... 100)处的插值应该给我100个横截面(以二维图像的形式)。

The flaw is in the function F itself, since 'surf' throws an error saying that F should be an array - "CData must be an M-by-N matrix or M-by-N-by-3 array". 缺陷在于函数F本身,因为'surf'抛出一个错误,说F应该是一个数组-“ CData必须是一个M×N矩阵或M×N×3数组”。

Can anyone please help. 谁能帮忙。

I finally figured it. 我终于想通了。 For the benefit of others, here is the code. 为了他人的利益,这里是代码。

% A 3-D matrix 'F' which has its value at particular coordinate set to 255 if it belongs to any one of the spheres and 0 otherwise.
[X,Y,Z] = meshgrid(1:100,1:100,1:100);
F = zeros(size(X));
for SpNum = 1:NumSpheres
    F( sqrt((X - Center(SpNum,1)).^2 + (Y - Center(SpNum,2)).^2 + (Z - Center(SpNum,3)).^2) <= Radius(SpNum) ) = 255;        
end
% Extract cross sections from F using interp3 function along the z-axis.
I = zeros(size(X));
for z = 1:100
    I(:,:,z) = interp3(X, Y, Z, F, 1:100, (1:100)', z, 'spline');
end
implay(I,4);

You could test and visualize the output by setting Center (a 3-D vector) and Radius of each sphere (some arbitrary NumSpheres) to some random values. 您可以通过将每个球体的Center(3-D矢量)和Radius(某些任意NumSpheres)设置为一些随机值来测试和可视化输出。 The above code will display a window with cross-sections. 上面的代码将显示一个带有横截面的窗口。

Previously, I was trying to use 'surf' to render the spheres which is not right. 以前,我尝试使用“ surf”渲染不正确的球体。 To render, you have to use the first code snippet. 要进行渲染,您必须使用第一个代码段。 Another mistake I made was using a row vector for the 6th argument instead of column vector. 我犯的另一个错误是第6个参数使用行向量而不是列向量。

Hope this helps. 希望这可以帮助。

-- Cheers, Ram. -干杯,拉姆。

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

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