I am finding difficulty to convert a 3D nifti image to slices.
Here is a screenshot of input 3D image.
And the slices should look like this.
But using this code I am getting this type of output slice.
The code is below.
#include “itkImageFileReader.h”
#include “itkImageFileWriter.h”
#include “itkExtractImageFilter.h”
#include “itkImage.h”
#include<string.h>
#include <itkRescaleIntensityImageFilter.h>
int main( int argc, char ** argv )
{
typedef signed short InputPixelType;
typedef unsigned char OutputPixelType;
typedef itk::Image< InputPixelType, 3 > InputImageType;
typedef itk::Image< OutputPixelType, 2 > OutputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
const char * inputFilename = “G:/My Drive/BMES2019/New folder/subject2.nii”;
{
int i = 1;
std::string str=“input//”;
std::string s1=“slice”;
std::string s2 = std::to_string(i);
std::string s3=".png";
std::string s4=str+s1+s2+s3;
const char *mycharp = s4.c_str();
const char * outputFilename = mycharp;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( inputFilename );
writer->SetFileName( outputFilename );
typedef itk::ExtractImageFilter< InputImageType,OutputImageType > FilterType;
FilterType::Pointer filter = FilterType::New();
filter->InPlaceOn();
filter->SetDirectionCollapseToSubmatrix();
reader->UpdateOutputInformation();
InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
InputImageType::SizeType size = inputRegion.GetSize();
size[2] = 0;
InputImageType::IndexType start = inputRegion.GetIndex();
const unsigned int sliceNumber = stoi(s2);
start[2] = sliceNumber;
InputImageType::RegionType desiredRegion;
desiredRegion.SetSize( size );
desiredRegion.SetIndex( start );
filter->SetExtractionRegion( desiredRegion );
filter->SetInput( reader->GetOutput() );
typedef itk::RescaleIntensityImageFilter< OutputImageType, OutputImageType > RescaleFilterType;
RescaleFilterType::Pointer rescalefilter = RescaleFilterType::New();
rescalefilter->SetInput( filter->GetOutput() );
writer->SetInput( rescalefilter->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << “ExceptionObject caught !” << std::endl;
std::cerr << err << std::endl;
getchar();
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}