# How to apply different kernels to different areas of an image using deconvolution process?

**URL:** <https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922>\
**Category:** Support\
**Created:** [December 1, 2018, 10:02am UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922 "2018-12-01T10:02:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahrokh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shahrokh/32/1499_2.png) [@shahrokh](https://discourse.slicer.org/u/shahrokh)\
**Post date:** [December 1, 2018, 10:02am UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922/1 "2018-12-01T10:02:19Z")

</div>

Hi Dear developers and users

I have a image that has degraded its quality due to blurring. The blurring process can be modelled as degradation function. The amount of blurring (degradation function) varies in different areas of the image.  
Also suppose that I have knowledge about these degradation functions (kernels) as following:

For pixel value equal to 10 → kernel1  
For pixel value equal to 20 → kernel2  
For pixel value equal to 30 → kernel3  
…  
For pixel value equal to 100 → kernel10

These kernels are 20x20 pixels in size. Now, I want to apply these kernels to the image depending on the pixel values of the image through deconvolution process. The kernel is selected depending on the pixel value of the image. For pixel values of an image whose kernels are not available, I can interpolate them using existing kernels. I expect that this process improves the quality of the image.  
I know there are some deconvolution filters in “itk SimpleFilters” module, but I think that all these filters apply a specific kernel over the entire image (unlike my case that I want to apply different kernels to different areas of image).

At now, my main problem is that how can I apply these kernels on different areas based on pixel values of image?  
What is the solution to do this?

Please guide me.  
Thanks a lot.  
Shahrokh

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 1, 2018, 3:25pm UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922/2 "2018-12-01T15:25:55Z")

</div>

I don’t know of existing code that does just what you describe, but it sounds like you could implement that pretty easily (sounds like you know exactly what you need it to do). If you aren’t concerned about speed, probably the easiest is to do it in python by accessing the image data as a numpy array.

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Accessing\_Volume\_data\_as\_numpy\_array](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array)

If performance is an issue, then you can write C++ code for it, but that’ll require compiling and adds complexity.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 1, 2018, 3:35pm UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922/3 "2018-12-01T15:35:30Z")

</div>

You can also perform the filtering with 10 different kernels on the entire image, then use numpy array operations to combine the 10 processed image into 1 (see a simple example [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify_voxels_in_a_volume)).

---

<div class="post-metadata">

**Author:** ![shahrokh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shahrokh/32/1499_2.png) [@shahrokh](https://discourse.slicer.org/u/shahrokh)\
**Post date:** [December 4, 2018, 12:26pm UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922/4 "2018-12-04T12:26:47Z")

</div>

Thank you very much for your guidance and links you sent to me.

Andras pointed out that _I perform the filtering with 10 different kernels on the entire image and then combine 10 processed image to 1_.

Can this be equivalent to getting an average of ten kernels and then filtering with the resulting kernel (averaged kernel) on the entire image? I’m not really sure that this is true in my case.

I think the result of this proposed solution (by Andras) is different with what I apply different kernels in different areas of the image.

Please guide me further.  
Shahrokh

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 4, 2018, 1:34pm UTC](https://discourse.slicer.org/t/how-to-apply-different-kernels-to-different-areas-of-an-image-using-deconvolution-process/4922/5 "2018-12-04T13:34:58Z")

</div>

> [@shahrokh](#):
>
> Can this be equivalent to getting an average of ten kernels and then filtering with the resulting kernel (averaged kernel) on the entire image? I’m not really sure that this is true in my case.

You don’t average kernels but instead apply the candidate kernels one by one on the original image. Then in the final image you choose voxel value from one of the processed images, based on what was the intensity in the original image.

For certain kernel types (e.g., Gaussian smoothing) you can get magnitudes faster speeds and very similar results if you use an image pyramid (you apply filtering on the result of previous filtering).
