在Matlab中可视化3D图像

时间:2017-10-31 21:56:55

标签: matlab

将三维图像V存储在matlab中,作为红色,绿色和蓝色的数组。对于像素(i,j,k),我们会有

V(i,j,k,1)=0.1 (red)
V(i,j,k,2)=0.9 (green)
V(i,j,k,3)=0.2 (blue)

然后,我希望将此3D图像可视化为移动的2D切片。我认为“切片”命令可以解决这个问题,但它只是为了显示3D功能,而不是3D图像。你有什么建议吗?

我正在添加一个可以提供帮助但却无法满足要求的脚本:

clear all; clc; close all;

N=35;
w=zeros(N,N,N);
x=linspace(-5,5,N); y=linspace(-5,5,N); z=linspace(-5,5,N);

for i=1:N
    for j=1:N
        for k=1:N
            w(i,j,k)=cos(k)/(1+i+j)^2;
        end
    end
end

for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Nz));
rotate(hsp,[1,0,0],18*k,[0 0 0]) %rotate a slice where the image is shown
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
slice(x,y,z,w,xd,yd,zd)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end

上面的w数组现在应该是3D图像。

1 个答案:

答案 0 :(得分:1)

听起来你想要slice的功能,但是你希望它在[N x M x P x 3]数组而不是[N x M x P]数组上运行。

实现此目的的一种方法是为每个通道设置插补器:

red   = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue  = griddedInterpolant({x,y,z},V(:,:,:,3));

然后替换

slice(x,y,z,w,xd,yd,zd)

cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata);

我在你的例子中试过这个,发现w中的颜色都非常接近黑色。所以我加载了一个不同的图像来处理:

% An orange-and-black skull for Halloween
load mri
V = permute(double(D), [1 2 4 3]);
V = V / max(V(:));
V = bsxfun(@times, V, reshape([1 .3 0], [1 1 1 3]));

% Define grid coordinates and interpolator
[Nx, Ny, Nz, nCh] = size(V);
x = linspace(-5,5,Nx); 
y = linspace(-5,5,Ny); 
z = linspace(-5,5,Nz);
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));

for k = 0:.1:10
    hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Ny,Nx));
    rotate(hsp,[1,0,0],18*k,[0 0 0])
    xd = hsp.XData;
    yd = hsp.YData;
    zd = hsp.ZData;
    delete(hsp)

    cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
    surface(xd,yd,zd,cdata)

    shading flat
    hold off
    view(-25,20)
    axis([-5 5 -5 5 -5 5]);
    drawnow
end