# Strange ImageFiltering.imfilter behaviour. (differs from matlab)

**URL:** <https://discourse.julialang.org/t/strange-imagefiltering-imfilter-behaviour-differs-from-matlab/36678>\
**Category:** New to Julia\
**Tags:** question, images, first-steps\
**Created:** [March 29, 2020, 3:16pm UTC](https://discourse.julialang.org/t/strange-imagefiltering-imfilter-behaviour-differs-from-matlab/36678 "2020-03-29T15:16:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunz-David](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kunz-david/32/24888_2.png) [@Kunz-David](https://discourse.julialang.org/u/Kunz-David)\
**Post date:** [March 29, 2020, 3:16pm UTC](https://discourse.julialang.org/t/strange-imagefiltering-imfilter-behaviour-differs-from-matlab/36678/1 "2020-03-29T15:16:05Z")

</div>

Hello.

I was debugging my code and ran into something I don’t understand.  
When using `imfilter` (and `imfilter!`) from `ImageFiltering` on a simple zero and one matrix the output matrix is scrambled in a specific way. The rows and columns I’d expect to be the first and second are wrapped around as last and second to last.  
Example:

```julia
using ImageFiltering: kernelfactors, imfilter, Pad
mask = ones(10, 10)
mask[3:8, 3:8] .= 0

ker = [0.5, 2, 0.5]
kernf = kernelfactors((ker, ker));

out = imfilter(mask, kernf, Pad(:symmetric))

```

```julia
out =
 2.75 1.5 1.5 1.5 1.5 2.75 7.75 9.0 9.0 7.75
 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 9.0 7.5 
 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 9.0 7.5 
 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 9.0 7.5 
 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 9.0 7.5 
 2.75 1.5 1.5 1.5 1.5 2.75 7.75 9.0 9.0 7.75
 7.75 7.5 7.5 7.5 7.5 7.75 8.75 9.0 9.0 8.75
 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 
 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 
 7.75 7.5 7.5 7.5 7.5 7.75 8.75 9.0 9.0 8.75

```

What I expected: (similar code in matlab produces this)

```julia
 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 
 9.0 8.75 7.75 7.5 7.5 7.5 7.5 7.75 8.75 9.0 
 9.0 7.75 2.75 1.5 1.5 1.5 1.5 2.75 7.75 9.0 
 9.0 7.5 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 
 9.0 7.5 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 
 9.0 7.5 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 
 9.0 7.5 1.5 0.0 0.0 0.0 0.0 1.5 7.5 9.0 
 9.0 7.75 2.75 1.5 1.5 1.5 1.5 2.75 7.75 9.0 
 9.0 8.75 7.75 7.5 7.5 7.5 7.5 7.75 8.75 9.0 
 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 

```

Can someone please tell me what I’m missing?

Thanks in advance, DK 🙂

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 29, 2020, 4:37pm UTC](https://discourse.julialang.org/t/strange-imagefiltering-imfilter-behaviour-differs-from-matlab/36678/2 "2020-03-29T16:37:12Z")

</div>

You are missing `ImageFiltering.centered`. With `ker = centered([0.5, 2, 0.5])` it should look more like you expect.

---

<div class="post-metadata">

**Author:** ![Kunz-David](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kunz-david/32/24888_2.png) [@Kunz-David](https://discourse.julialang.org/u/Kunz-David)\
**Post date:** [March 29, 2020, 4:55pm UTC](https://discourse.julialang.org/t/strange-imagefiltering-imfilter-behaviour-differs-from-matlab/36678/3 "2020-03-29T16:55:47Z")

</div>

Perfect! Thank you.  
Does exactly what I wanted. 🙂
