How can I plot several 2D image in a stack style in Matlab?

后端 未结 2 1827
傲寒
傲寒 2020-12-06 15:23

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

相关标签:
2条回答
  • Arrange them all in a 3D Matrix. This would be arranging them such that you get stack(i,j,k), where stack(i,:,:) gives you a particular image and i is the the slice number. Once this is done, use this library. Viewer3D pretty much lets you visualize everything in an interactive 3D GUI and also performs interpolation. However, there should be a positive correlation in the data between the slices, or the 3D image is not going to appear to make sense.

    0 讨论(0)
  • 2020-12-06 15:49

    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: enter image description here


    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: enter image description here


    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.

    0 讨论(0)
提交回复
热议问题