如何在 Matlab 中以堆栈样式绘制多个 2D 图像? [英] How can I plot several 2D image in a stack style in Matlab?

查看:23
本文介绍了如何在 Matlab 中以堆栈样式绘制多个 2D 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Matlab 中,我想绘制几个二维图像(I(x,y) 格式的二维矩阵中的所有数据).我知道对于单个图,imagesc(I) 可以实现所需的图像.但是,现在,我得到了一些图像,并希望将它们放在堆栈格式中,就像 .例如:

[xs,ys,zs] = ndgrid( 1:25 , 1:50 , 1:4 ) ;

将创建一个大小为 [25x50x4] 的网格/坐标系.只需替换数字即可构建所需的网格坐标.

In Matlab, I want to plot several 2D image (all data in a 2 dimension matrix in I(x,y) format). I know for a single plot, imagesc(I) could achieve the desired image. However, now, I got a few images and want to put them in a stack format, just like the image shown in Examples

解决方案

As you got hinted, the most useful function for your problem is : slice. You should also have a good read at this Mathworks article: Exploring Volumes with Slice Planes as it gives more examples on how to work with slice.

In your case, you have the data for each of your slices (each image is a slice), you just need to pack them together so Matlab will interpret them as volumetric data.

Since you didn't give any sample data to work with, I have to generate a small sample. I will use the Matlab function flow to generate volumetric data and extract 4 images (4 slices) out of it:

%% Generate sample images
[x,y,z,v] = flow; %// x,y,z and v are all of size [25x50x25]

im1 = v(:,:,5);  %// extract the slice at Z=5.  im1 size is [25x50]
im2 = v(:,:,10); %// extract the slice at Z=10. im2 size is [25x50]
im3 = v(:,:,15); %// extract the slice at Z=15. im3 size is [25x50]
im4 = v(:,:,20); %// extract the slice at Z=20. im4 size is [25x50]

hf = figure ;
subplot(221);imagesc(im1);title('Z=5');
subplot(222);imagesc(im2);title('Z=10');
subplot(223);imagesc(im3);title('Z=15');
subplot(224);imagesc(im4);title('Z=20');

%// This is just how I generated sample images, it is not part of the "answer" !

Which gives you 4 simple images:


Now is the real fun. Stack all your images in one matrix as if they were just slices:

M(:,:,1) = im1 ;
M(:,:,2) = im2 ;
M(:,:,3) = im3 ;
M(:,:,4) = im4 ;

You now have a matrix M [25x50x4]. If you have too many images you can work out a loop to stack them.

From there on, it's just a simple matter of calling slice to get your desired picture. Look at the documentation to explore all the possible rendering options. A simple example is:

hf2 = figure ;
hs = slice(M,[],[],1:4) ;
shading interp
set(hs,'FaceAlpha',0.8);

Which produces:


note: This uses the default indexing, which should be good for the problem you describe (simply stacking some images). If you want your volume to have real coordinates, you can build a coordinate system with ndgrid. Ex:

[xs,ys,zs] = ndgrid( 1:25 , 1:50 , 1:4 ) ;

will create a grid/coordinate system of size [25x50x4]. Just replace the numbers to build the grid coordinate you need.

这篇关于如何在 Matlab 中以堆栈样式绘制多个 2D 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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