Strange ImageFiltering.imfilter behaviour. (differs from matlab)

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:

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))
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)

 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 :slight_smile:

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

1 Like

Perfect! Thank you.
Does exactly what I wanted. :slight_smile: