[ITK] [ITK-users] Image conversion from 3D volume to 2D slices

Shrabani Ghosh srbn.ghosh99 at gmail.com
Wed May 8 13:54:08 EDT 2019


I did it like this and I am getting images like this now. 
<http://itk-insight-users.2283740.n2.nabble.com/file/t396636/slice1.png> 



int main( int argc, char ** argv )
{
  
  //typedef signed short        InputPixelType;
  /*typedef unsigned char       InputPixelType;
  typedef unsigned char        OutputPixelType;*/
  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/subject1.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::RescaleIntensityImageFilter< InputImageType, InputImageType
> RescaleFilterType;
   RescaleFilterType::Pointer rescalefilter = RescaleFilterType::New();
   rescalefilter->SetInput( reader->GetOutput() );

  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);//atoi(argv[3]);
  start[2] = sliceNumber;

  InputImageType::RegionType desiredRegion;
  desiredRegion.SetSize(  size  );
  desiredRegion.SetIndex( start );

  filter->SetExtractionRegion( desiredRegion );

  filter->SetInput( rescalefilter->GetOutput() );
  writer->SetInput( filter->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;
}



--
Sent from: http://itk-insight-users.2283740.n2.nabble.com/
The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
https://itk.org/mailman/listinfo/insight-users


More information about the Community mailing list