Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert nii format file into 2D image

I have a file with .nii extension. I don't know how to convert a .nii file into 2D format.My question is while converting .nii file into 2D, am I losing some information about the file.Which format is good? dicom or png or bmp.

nii = load_nii('im.nii');
size(nii.img);

returns

ans =

    39   305   305  

and it is in uint8 format

May I use squeeze or resize?How to apply resize to this image;whether it lose information?

like image 936
user1234 Avatar asked Apr 15 '15 02:04

user1234


People also ask

What is .NII file?

NIfTI is a type of file format for neuroimaging. Technically there are NIfTI-1 and NIfTI-2 file formats. The NIfTI-2 format is an update on NIfTI-1 that allows more data to be stored. NIfTI files are used very commonly in imaging informatics for neuroscience and even neuroradiology research.


1 Answers

Yes you can manipulate the image/sequence as you would with any array.

Here is a simple example with data available from the NIH here.

The data set is in 4D and is named "filtered_func_data.nii".

Let's load the dataset and access the img field of the resulting structure:

S = load_nii('filtered_func_data.nii')

Here, S is a structure with the following fields:

S = 

           hdr: [1x1 struct]
      filetype: 2
    fileprefix: 'filtered_func_data'
       machine: 'ieee-be'
           img: [4-D int16]
      original: [1x1 struct]

And we can access the image data with the field img (you already figured that out):

A = S.img

If we check the size, we get:

size(A)

ans =

    64    64    21   180

So the dataset consists in 64x64 images with a depth/number of slices of 21 and a number of frames of 180.

At this point we can manipulate A as we like to reshape, resize or anything.

Here is a simple code (animated gif) to loop through each slice of the 1st timepoint in the 4D array:

NumSlices = size(A,3)

figure(1)
filename = 'MRI_GIF.gif';

for k = 1:NumSlices

    imshow(A(:,:,k,1),[])
    drawnow

    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    if k == 1;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end

    pause(.1)
end

Output:

Which looks pretty nice to me.

So in your case, you get a size of [39 305 305] and you can apply the same manipulations I did to play around with your data set, which is in 3D.

EDIT With your data it's the same:

S = load_nii('Subject01.nii');

A = S.img;

NumSlices = size(A,3);

And then, if you want a 2D image you need to select a slice in the 3D array.

For example, the first slice is accessed like so:

A(:,:,1)

And so on for the rest.

To save images as png, use imwrite.

Hope that helps!

like image 95
Benoit_11 Avatar answered Oct 12 '22 03:10

Benoit_11