# (API) Specify image channels

**URL:** https://discourse.itk.org/t/api-specify-image-channels/2009
**Category:** Engineering
**Created:** [July 3, 2019, 10:54am UTC](https://discourse.itk.org/t/api-specify-image-channels/2009 "2019-07-03T10:54:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![nm97](https://discourse.itk.org/user_avatar/discourse.itk.org/nm97/32/814_2.png) [@nm97](https://discourse.itk.org/u/nm97)
#### Post date: [July 3, 2019, 10:54am UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/1 "2019-07-03T10:54:32Z")

</div>

How can I specify image channels.  
I have created image object.

```auto
        using ImageType = itk::Image< uint8_t, 2 >;
        ImageType::Pointer image = ImageType::New();

        ImageType::SizeType size;
        size[0] = width;
        size[1] = height;

        ImageType::IndexType start;
        start.Fill( 0 );

        ImageType::RegionType region;
        region.SetSize( size );
        region.SetIndex( start );

        image->SetRegions( region );
        image->Allocate();

```

I can specify size, start but could not find in documentation how to specify image channels.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 3, 2019, 2:30pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/2 "2019-07-03T14:30:40Z")

</div>

You just have to define vector image type.

```auto
using VectorPixelType = itk::Vector<uint8_t, 10>; // 10 channel pixel
using ImageType = itk::Image< VectorPixelType, 2 >; // 2D image

```

Read more about it in the [software guide](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html#x45-500004.1), section 4.1.6.

---

<div class="post-metadata">

### Author: ![nm97](https://discourse.itk.org/user_avatar/discourse.itk.org/nm97/32/814_2.png) [@nm97](https://discourse.itk.org/u/nm97)
#### Post date: [July 3, 2019, 2:57pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/3 "2019-07-03T14:57:27Z")

</div>

Thank you very much for quick response.

In that case I get an error in this line

```auto
  rescaler->SetOutputMaximum( 255 );
  rescaler->SetOutputMinimum( 0 );

```

```auto
 error: conversion from ‘int’ to ‘itk::RescaleIntensityImageFilter<itk::Image<itk::Vector<float, 3>, 2>, itk::Image<itk::Vector<unsigned char, 3>, 2> >::OutputPixelType’ {aka ‘itk::Vector<unsigned char, 3>’} is ambiguous
  180 | rescaler->SetOutputMinimum( 0 );

```

---

<div class="post-metadata">

### Author: ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)
#### Post date: [July 3, 2019, 3:09pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/4 "2019-07-03T15:09:42Z")

</div>

These methods expect arguments of same type as the pixels in your image. Since the pixel in your image is a vector (aka multi-channel image), you should input a vector.

Giving only one value is ambiguous, i.e. in which channel should the value goes ? first? all?  
If it’s the latter, you could do something like:

```auto
itk::Vector<uint8_t, 3> MaxPixel, MinPixel;
MaxPixel[0] = MaxPixel[1] = MaxPixel[2] = 255;
MinPixel[0] = MinPixel[1] = MinPixel[2] = 0;

rescaler->SetOutputMaximum( MaxPixel );
rescaler->SetOutputMinimum( MinPixel );

```

HTH,

Tim

---

<div class="post-metadata">

### Author: ![nm97](https://discourse.itk.org/user_avatar/discourse.itk.org/nm97/32/814_2.png) [@nm97](https://discourse.itk.org/u/nm97)
#### Post date: [July 3, 2019, 4:50pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/5 "2019-07-03T16:50:27Z")

</div>

In that case I get an error in the last line

```auto
        using VectorPixelType = itk::Vector<uint8_t, 3>; // 10 channel pixel

        using ImageType = itk::Image< VectorPixelType, 2 >; // 2D image

        using OutputVectorPixelType = itk::Vector<float, 3>; 
        // using OutputPixelType = float;
        using OutputImageType = itk::Image< OutputVectorPixelType, 2 >;

        using FilterType = itk::MinMaxCurvatureFlowImageFilter< ImageType, OutputImageType >;
        FilterType::Pointer filter = FilterType::New();

```

```auto
 invalid static_cast from type ‘itk::Vector<unsigned char, 3>’ to type ‘float’
  185 | auto b = static_cast< T2 >( a );

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 3, 2019, 5:01pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/6 "2019-07-03T17:01:50Z")

</div>

I guess that `MinMaxCurvatureFlowImageFilter` does not support vector images. You might use [SplitComponentsImageFilter](https://itk.org/Doxygen/html/classitk_1_1SplitComponentsImageFilter.html) or [VectorImageToImageAdaptor](https://itk.org/Doxygen/html/classitk_1_1VectorImageToImageAdaptor.html) to split the image into constituent channels, apply some processing on each channel independently, and combine them using [ComposeImageFilter](https://itk.org/Doxygen/html/classitk_1_1ComposeImageFilter.html).

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [July 3, 2019, 5:28pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/7 "2019-07-03T17:28:51Z")

</div>

Here is a snippet of code generated in SimpleITK which run certain filters on a “per component basis”:

```auto

//
// Dispatched methods to call ExecuteInternal on each component of the VectorImage
//
template <class TImageType> Image BilateralImageFilter::ExecuteInternalVectorImage ( const Image& inImage1 )
{
  typedef TImageType VectorInputImageType;
  typedef typename VectorInputImageType::InternalPixelType ComponentType;
  typedef typename itk::Image<ComponentType, VectorInputImageType::ImageDimension> ComponentImageType;

  // we must define the input and output image types should be the
  // same as the scalar execute internal method
  typedef ComponentImageType InputImageType;
  // Define output image type
  typedef InputImageType OutputImageType;
  // Get the pointer to the ITK image contained in image1
  typename VectorInputImageType::ConstPointer image1 =
    this->CastImageToITK<VectorInputImageType>( inImage1 );

  typedef itk::VectorIndexSelectionCastImageFilter< VectorInputImageType, ComponentImageType > ComponentExtratorType;
  typename ComponentExtratorType::Pointer extractor = ComponentExtratorType::New();
  extractor->SetInput( image1 );

  typedef itk::ComposeImageFilter<OutputImageType> ToVectorFilterType;
  typename ToVectorFilterType::Pointer toVector = ToVectorFilterType::New();

  unsigned int numComps = image1->GetNumberOfComponentsPerPixel();
  for ( unsigned int i = 0; i < numComps; ++i )
    {
    extractor->SetIndex( i );
    extractor->Update();

    Image tmp = this->ExecuteInternal<InputImageType>( Image( extractor->GetOutput() ) );

    typename OutputImageType::ConstPointer tempITKImage = this->CastImageToITK<OutputImageType>( tmp );

    toVector->SetInput( i, tempITKImage );
    }

  toVector->Update();

  return Image( toVector->GetOutput() );
}

```

---

<div class="post-metadata">

### Author: ![nm97](https://discourse.itk.org/user_avatar/discourse.itk.org/nm97/32/814_2.png) [@nm97](https://discourse.itk.org/u/nm97)
#### Post date: [July 3, 2019, 5:44pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/8 "2019-07-03T17:44:30Z")

</div>

> [@dzenanz](#):
>
> ependently, and combine them

Ok, but in InsightToolkit-5.0.0/Examples/Filtering/MinMaxCurvatureFlowImageFilter.cxx in this example input image has 3 channels and still it does not specify the number of channels.  
But still it filters image and writes in the file as a multichanel image

---

<div class="post-metadata">

### Author: ![nm97](https://discourse.itk.org/user_avatar/discourse.itk.org/nm97/32/814_2.png) [@nm97](https://discourse.itk.org/u/nm97)
#### Post date: [July 3, 2019, 6:02pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/9 "2019-07-03T18:02:22Z")

</div>

And one more question,  
If `MaxCurvatureFlowImageFilter` does not support vector images how can I filter multi channel images without using vector? Because as I said in example there is no use of vector, but it filters fine

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 3, 2019, 6:34pm UTC](https://discourse.itk.org/t/api-specify-image-channels/2009/10 "2019-07-03T18:34:44Z")

</div>

Instead of `itk::Vector<uint8_t, 3>`, you might want to try `itk::RGBPixel<uint8_t>`.

In the example you mentioned, the pixel type is scalar = grayscale (float). If color image is supplied, the IO mechanism in ITK converts it to grayscale and the example deals with that afterwards.
