# how to apply kernel in ConvolutionImageFilter

**URL:** <https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741>\
**Category:** Engineering\
**Tags:** filter\
**Created:** [April 3, 2019, 3:16pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741 "2019-04-03T15:16:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.itk.org/u/siaeleni)\
**Post date:** [April 3, 2019, 3:16pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741/1 "2019-04-03T15:16:53Z")

</div>

Hello,

I want to apply an image (kernel) to an input Image, but I am not sure how can I apply that correctly.  
I have attached the sample of my code here:

> #include “itkImageFileReader.h”  
> #include “itkImageFileWriter.h”  
> #include “itkConvolutionImageFilter.h”
> 
> int main(int argc, char \*argv)  
> {  
> if (argc \< 3)  
> {  
> std::cerr \<\< “Usage: ” \<\< std::endl;  
> return EXIT\_FAILURE;  
> }  
> const char \* inputImage = argv[1];  
> const char \* kernelImage = argv[2];  
> const char \* outputImage = argv[3];
> 
> constexpr unsigned int Dimension = 3;  
> using InputPixelType = double;  
> using OutputPixelType = double;  
> using KernelPixelType = double;  
> using InputImageType = itk::Image\< InputPixelType, Dimension \>;  
> using OutputImageType = itk::Image\< OutputPixelType, Dimension \>;  
> using KernelImageType = itk::Image\< KernelPixelType, Dimension \>;
> 
> using ReaderType = itk::ImageFileReader\< InputImageType \>;  
> ReaderType::Pointer reader = ReaderType::New();  
> reader-\>SetFileName(inputImage);
> 
> using ReaderKernelType = itk::ImageFileReader\< KernelImageType \>;  
> ReaderKernelType::Pointer kernel = ReaderKernelType::New();  
> kernel-\>SetFileName(kernelImage);
> 
> using ImageType = itk::Image\<double, 3\>;  
> ImageType::Pointer ker = ImageType::New();
> 
> // Convolve image with kernel.  
> typedef itk::ConvolutionImageFilter FilterType;  
> FilterType::Pointer convolutionFilter = FilterType::New();  
> convolutionFilter-\>SetInput(reader-\>GetOutput());  
> convolutionFilter-\>SetKernelImage(kernel);
> 
> using WriterType = itk::ImageFileWriter\< OutputImageType \>;  
> WriterType::Pointer writer = WriterType::New();  
> //writer-\>SetInput(rescaler-\>GetOutput());  
> writer-\>SetFileName(outputImage);
> 
> try  
> {  
> writer-\>Update();  
> }  
> catch (itk::ExceptionObject & error)  
> {  
> std::cerr \<\< "Error: " \<\< error \<\< std::endl;  
> return EXIT\_FAILURE;  
> }
> 
> return EXIT\_SUCCESS;  
> }

Thanks in advance,  
Eleni

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [April 3, 2019, 3:57pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741/2 "2019-04-03T15:57:06Z")

</div>

Hi Eleni,

Following the [template requirements of itk::ConvolutionImageFilter](https://itk.org/Doxygen/html/classitk_1_1ConvolutionImageFilter.html), add its template parameters:

```auto
using FilterType = itk::ConvolutionImageFilter< InputImageType, KernelImageType, OutputImageType >;

```

and set the input to the writer to be the output of the convolution filter:

```auto
writer->SetInput(convolutionFilter->GetOutput());

```

Hope this helps,  
Matt

---

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.itk.org/u/siaeleni)\
**Post date:** [April 3, 2019, 4:08pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741/3 "2019-04-03T16:08:11Z")

</div>

Hi Matt,

I can’t click on the blue link, is there any link?  
My issue is how to connect the kernelImage with the convolutionFilter? I try the following but I get a compile error:

> using FilterType = itk::ConvolutionImageFilter\< InputImageType, KernelImageType, OutputImageType \>;  
> FilterType::Pointer convolutionFilter = FilterType::New();  
> convolutionFilter-\>SetInput(reader-\>GetOutput());  
> convolutionFilter-\>SetKernelImage(kernelImage); //kernel

Could you please provide some help?

Thanks a lot,  
Eleni

---

<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:** [April 3, 2019, 4:16pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741/4 "2019-04-03T16:16:40Z")

</div>

Matt’s blue link was originally broken, I have edited his answer to fix the link.

You need to change

```cpp
convolutionFilter->SetKernelImage(kernelImage); //kernel

```

into:

```auto
convolutionFilter->SetKernelImage(kernel->GetOutput()); //kernel

```

---

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.itk.org/u/siaeleni)\
**Post date:** [April 3, 2019, 7:53pm UTC](https://discourse.itk.org/t/how-to-apply-kernel-in-convolutionimagefilter/1741/5 "2019-04-03T19:53:40Z")

</div>

Thank you both!
