如何将nii格式文件转换为2D图像 [英] How to convert nii format file into 2D image

查看:844
本文介绍了如何将nii格式文件转换为2D图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展名为.nii的文件.我不知道如何将.nii文件转换为2D格式.我的问题是将.nii文件转换为2D时,我是否丢失了一些有关该文件的信息.哪种格式好? dicom或png或bmp.

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);

返回

ans =

    39   305   305  

它是uint8格式

我可以使用挤压还是调整大小?如何对此图像应用调整大小;它是否会丢失信息?

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

推荐答案

是的,您可以像处理任何数组一样操作图像/序列.

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

这是一个简单的示例,其中的数据可从NIH 此处获取.

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

数据集采用4D格式,名为"filtered_func_data.nii".

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

让我们加载数据集并访问结果结构的img字段:

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

S = load_nii('filtered_func_data.nii')

在这里,S是具有以下字段的结构:

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]

我们可以使用img字段访问图像数据(您已经知道了):

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

因此,数据集包含64x64张图像,其深度/切片数为21,帧数为180.

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

在这一点上,我们可以根据需要调整A的形状,大小或其他任何内容.

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

这是一个简单的代码(动画gif),可以循环遍历4D数组中第一个时间点的每个片段:

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

输出:

对我来说哪个看起来不错.

Which looks pretty nice to me.

因此,在您的情况下,大小为[39 305 305],并且可以应用与3D数据集相同的操作.

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.

编辑 与您的数据相同:

S = load_nii('Subject01.nii');

A = S.img;

NumSlices = size(A,3);

然后,如果要获得2D图像,则需要在3D阵列中选择一个切片.

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)

其余的依次类推.

要将图像另存为png,请使用 imwrite .

To save images as png, use imwrite.

希望有帮助!

这篇关于如何将nii格式文件转换为2D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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