# How to separate channels from a NRRD file?

**URL:** <https://discourse.itk.org/t/how-to-separate-channels-from-a-nrrd-file/5671>\
**Category:** Beginner Questions\
**Created:** [February 2, 2023, 10:45pm UTC](https://discourse.itk.org/t/how-to-separate-channels-from-a-nrrd-file/5671 "2023-02-02T22:45:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sterben\_01](https://discourse.itk.org/user_avatar/discourse.itk.org/sterben_01/32/3181_2.png) [@Sterben\_01](https://discourse.itk.org/u/Sterben_01)\
**Post date:** [February 2, 2023, 10:45pm UTC](https://discourse.itk.org/t/how-to-separate-channels-from-a-nrrd-file/5671/1 "2023-02-02T22:45:18Z")

</div>

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](https://examples.itk.org/src/core/imageadaptors/extractchannelofimagewithmultiplecomponents/documentation) but I still didn’t get the concept of this code.  
Currently I have my code like this:

```plaintext
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;`

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [February 3, 2023, 3:21pm UTC](https://discourse.itk.org/t/how-to-separate-channels-from-a-nrrd-file/5671/4 "2023-02-03T15:21:04Z")

</div>

Couldn’t you use the VectorIndexSelectionCastImageFilter?

[https://itk.org/Doxygen/html/classitk\_1\_1VectorIndexSelectionCastImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1VectorIndexSelectionCastImageFilter.html)

---

<div class="post-metadata">

**Author:** ![Sterben\_01](https://discourse.itk.org/user_avatar/discourse.itk.org/sterben_01/32/3181_2.png) [@Sterben\_01](https://discourse.itk.org/u/Sterben_01)\
**Post date:** [February 3, 2023, 3:23pm UTC](https://discourse.itk.org/t/how-to-separate-channels-from-a-nrrd-file/5671/5 "2023-02-03T15:23:52Z")

</div>

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!
