Smoothing 2D data homogeneously regardless of resolution

Hi,

I have 2D initial values for a model and I am trying to smooth it. I would say the easiest way is the apply a gaussian filter on it. My problem is that I don’t have the same resolution in y and in x. I would like to know if there is a way to have an isotropic blurring regardless of the initial resolution?

Here is a MRE to make it clearer:

using Plots

incircle(;x₀,y₀, r) = (y, x) -> hypot(y - y₀, x - x₀) <= r

nx = 100
ny = 200
Lx = 100
Ly = 50
x = range(1, Lx, nx)
y = range(1, Ly, ny)
model = zeros(ny, nx)

incircle(;x₀=25, y₀=50, r=20)(1,1)

for (j, y) in enumerate(y), (i, x) in enumerate(x)
    if incircle(;x₀=50, y₀=25, r=20)(y, x) == true
        model[j,i] = 1
    else
        model[j,i] = 0
    end
end

heatmap(x, y, model, aspect_ratio=:equal)

which produces:
plot_49

So I have a circle with a different resolution in x and y.

I have tried to blurred using ImageFiltering that way:

blur = imfilter(model, Kernel.gaussian(1));
heatmap(x, y,blur, aspect_ratio=:equal)

with the result:
plot_50

So you can see that it’s smoother in the x direction because there is less points

I am not an expert at all on kernels and convolutions but reading the docs, I see that I can change the size of the kernel in the x or y direction. Should I play with that? Or is what I am trying to do not achievable because I can’t get over the difference in resolution and hence precision in 1 axis?

Maybe you could use factored kernels and increase the standard deviation proportionally (4x) along the second dimension to compensate for the difference in resolution.

using ImageFiltering
kx = Kernel.gaussian((1,), (11,))
ky = Kernel.gaussian((4,), (11,))
blurxy = imfilter(model, (kx', ky))

Resulting in:
ImageFiltering_kx_ky_kernel_smoothing

3 Likes

As a sidenote, I believe that Julia broadcasting tools allow writing this in one line:

model = ifelse.(incircle(x₀=50, y₀=25, r=20).(y, x'), 1, 0)
1 Like

Great thx, that is exactly what I was looking for!

I thought I should play with the resolution, but it is better with the standard deviation.

Have a good day,

1 Like