How to separate channels from a NRRD file?

Hi!
I am a beginner of ITK/VTK. Currently, I have an NRRD file, and it has three channels. If I want to separate it and write it to three files, which class should I use? I know how to read and write, but I don’t know how to separate it.
I had tried this simple example Sample but I still didn’t get the concept of this code.
Currently I have my code like this:

static void CreateImage(SplitImageType::Pointer output_image, FileReaderType::Pointer input_image){

    DiffusionImageType::RegionType input_region = input_image->GetOutput()->GetLargestPossibleRegion(); //get region of original pic
    DiffusionImageType::SizeType input_size = input_region.GetSize(); //get size from original region

    std::cout << input_size << std::endl;

    SplitImageType::IndexType output_start; 
    output_start.Fill(0);

    SplitImageType::SizeType output_size;
    output_size.Fill(2);

    SplitImageType::RegionType output_region;
    output_region.SetSize(input_size); //set separate image size, 
    output_region.SetIndex(output_start); //set separate image offset?

    output_image->SetRegions(output_region); //set separate image region
    output_image->Allocate(); //generate image

    std::cout << "start" << std::endl;
    itk::ImageRegionIterator<DiffusionImageType> imageIterator(input_image->GetOutput(), input_region); //get iterator of original image
    itk::Vector<unsigned char, 1> vectorPixel; //temp storage
    while(!imageIterator.IsAtEnd()){
        vectorPixel = imageIterator.Get()[0]; //how to wrtie this line? I think is iterator both input_image and output_image and set output_image value to input_image? 
        imageIterator++;
    }
    std::cout << "end" << std::endl;
    output_image->FillBuffer(vectorPixel); //fill image。
}

Actually I don’t really understand how to iterate over the itk::Vector<unsigned char, 1> vectorPixel;

Couldn’t you use the VectorIndexSelectionCastImageFilter?

https://itk.org/Doxygen/html/classitk_1_1VectorIndexSelectionCastImageFilter.html

2 Likes

Hi
Thanks for your reply. I am not really familiar with ITK, so I don’t even know the existence of this filter. I will try this. Thank you!

1 Like