Skip to content

Instantly share code, notes, and snippets.

@rafamanzo
Last active September 17, 2024 21:31
Show Gist options
  • Save rafamanzo/a3c258f4f74e898e9418 to your computer and use it in GitHub Desktop.
Save rafamanzo/a3c258f4f74e898e9418 to your computer and use it in GitHub Desktop.
Example on how to read and write NIfTI images using ITK
cmake_minimum_required(VERSION 3.0)
project(ITKNIfTI)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(itk_nifti main.cxx)
target_link_libraries(itk_nifti ${ITK_LIBRARIES})
The MIT License (MIT)
Copyright (c) 2014 Rafael Reggiani Manzo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#include <cstdlib>
#include <string>
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkImageRegionIterator.h>
#include <itkNiftiImageIO.h>
itk::ImageIOBase::Pointer getImageIO(std::string input){
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(input.c_str(), itk::ImageIOFactory::ReadMode);
imageIO->SetFileName(input);
imageIO->ReadImageInformation();
return imageIO;
}
itk::ImageIOBase::IOComponentType component_type(itk::ImageIOBase::Pointer imageIO){
return imageIO->GetComponentType();
}
itk::ImageIOBase::IOPixelType pixel_type(itk::ImageIOBase::Pointer imageIO){
return imageIO->GetPixelType();
}
size_t num_dimensions(itk::ImageIOBase::Pointer imageIO){
return imageIO->GetNumberOfDimensions();
}
int main(int argc, char const *argv[]){
if(argc < 2){
std::cerr << "Required: DWI filename" << std::endl;
return EXIT_FAILURE;
}
itk::ImageIOBase::Pointer imageIO = getImageIO(argv[1]);
// In order to read a image, we need its dimensionality and component type
std::cout << "numDimensions: " << num_dimensions(imageIO) << std::endl;
std::cout << "component type: " << imageIO->GetComponentTypeAsString(component_type(imageIO)) << std::endl;
// The pixel type is not necessary. This is just to let you know that it exists
std::cout << "pixel type: " << imageIO->GetPixelTypeAsString(pixel_type(imageIO)) << std::endl;
// Copy a DWI, if it looks like one
if(num_dimensions(imageIO) == 4 && component_type(imageIO) == imageIO->GetComponentTypeFromString("short")){
typedef itk::Image<short, 4> DWI;
typedef itk::ImageFileReader<DWI> DWIReader;
DWIReader::Pointer dwi_reader = DWIReader::New();
dwi_reader->SetFileName(imageIO->GetFileName());
dwi_reader->Update();
DWI::RegionType region;
DWI::IndexType start;
DWI::SizeType size;
for (int i = 0; i < num_dimensions(imageIO); ++i){
start[i] = 0;
size[i] = imageIO->GetDimensions(i);
}
region.SetSize(size);
region.SetIndex(start);
DWI::Pointer dwi = dwi_reader->GetOutput();
DWI::Pointer out_dwi = DWI::New();
out_dwi->SetRegions(region);
out_dwi->Allocate();
itk::ImageRegionIterator<DWI> imageIterator(dwi,region);
while(!imageIterator.IsAtEnd()){
out_dwi->SetPixel(imageIterator.GetIndex(), imageIterator.Value());
++imageIterator;
}
/* This should read the affine transform, but is always returning a default value
std::cout << "Affine transform" << std::endl;
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
std::cout << dwi->GetDirection()[i][j] << " ";
}
std::cout << std::endl;
}*/
itk::NiftiImageIO::Pointer nifti_io = itk::NiftiImageIO::New();
nifti_io->SetPixelType(pixel_type(imageIO));
itk::ImageFileWriter<DWI>::Pointer dwi_writer = itk::ImageFileWriter<DWI>::New();
dwi_writer->SetFileName("test.nii.gz");
dwi_writer->SetInput(out_dwi);
dwi_writer->SetImageIO(nifti_io);
dwi_writer->Update();
}
return EXIT_SUCCESS;
}
@tom-xi3
Copy link

tom-xi3 commented Sep 17, 2024

Hi Raf, do you know how to create a Nifti image from Dicom assuming you are able to read single volumes ? Let's say you want to create a 4D nifti from a series of 3D volumes, How would I have to selecty the region sizes and pixel type etc? any clue? regards

Hi, thanks for this example! I am also wondering the same question as @marcogiord. I'd be really grateful for your help.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment