++ed by:
2 non-PAUSE users
Author image Ingo Schmid
and 1 contributors


PDL::IO::Nifti -- Module to access imaging data using Nifti-1 standard



use strict; use 5.10.0; use PDL; use PDL::IO::Nifti; use PDL::NiceSlice;

my $name=shift; my $file; say "testing write_nii()"; open $file,'>testnii.nii' or die "Cannot open file to write\n"; my $nii=PDL::IO::Nifti->new; # Creates the object $nii->img(rvals(64,32)); # Assigns data

# The following lines illustrate how to access PDLs $nii*=3000; $nii(0,).=0; say avg $nii; say $nii->info;

# Now we write out the data $nii->write_nii($file); close $file;

#Now we read a file open my $file,$name or die "Failed to read $name\n"; my $ni2=$nii->new; $ni2->read_nii($file); say $ni2->info; $ni2->(,,0)->squeeze->wpic('nii.png');


Provides methods to read and write Nifti files, read, write and manipulate the header based on PDL.

my $nii=PDL::IO::Nifti->new($pdl); $nii->write_nii($file);

is all you need to do to save your data.



Initialize the object. Calls img if passed an argument.


This method should be called whenever you want to explicitly access your piddle only. The first argument will replace your piddle with whatever it holds. It returns the piddle. This should be used if you want to also update the essential metadata like dimensions and datatype in the Nifti header.


Loads an existing .nii file. If you have an analyze image/hdr pair, please convert to nifti first. All elements are loaded and are accessible via the get_field and set_field methods.


Writes out the PDL, sets datatype and dimensions automatically, overwriting your own values, take care!


$nii->set_field(<name>,<value>,[pos]); Sets field <name> to <value>. If the field is an array, <value> is interpreted as an array reference, unless pos is supplied.


$par=$nii->get_field(<name>,[pos]); returns header values, same rules regarding arrays as in set_field applay.

write_hdr, read_hdr

so far only used/tested internally, but may be useful. They pack and write or retrieve and unpack the header, respectively.


At the moment, read_nii and write_nii have only been tested for native PDL datatypes.

In case you get failures from read_nii/write_nii, try passing either a reference to an open filehandle or a filename. It tries to guess what argument has been given, but I'm not sure if it covers all cases.

Tests are still rudimentary, for the moment, noly reading is tested for.


http://nifti.nimh.nih.gov/nifti-1 - the Nifti standard on which this module is based.


Albrecht Ingo Schmid