Search code examples
matlabmat-file

How to convert a 3D matrix into several 2D matrix?


The mat file is a 156*192*25 3D matrix. The data are a series of n=25 images in a mat file. How could I convert this 3D matrix into 25 2D matrix and display them independently? Thanks~


Solution

  • I recommend not to split your stack of images, instead simply use I(:,:,3) if you for example need the third image.

    To display the images you can use:

    montage(permute(I,[1,2,4,3]))
    

    To display all images together. The permute is required because montage expects a 156*192*1*25 for stacked grey scale images, the third dimension is reserved for colour channels. Alternatively to display an individual image use:

    imshow(I(:,:,3));
    

    To save each image to an individual mat file use:

    for ix=1:size(I,3)
       M=matfile(sprintf('image%03d',ix),'writable',true);
       M.image=I(:,:,ix);
    end