# Smoothing 2D data homogeneously regardless of resolution

**URL:** https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082
**Category:** Signal and Image Processing
**Tags:** images, smoothing
**Created:** [April 26, 2022, 5:48pm UTC](https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082 "2022-04-26T17:48:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)
#### Post date: [April 26, 2022, 5:48pm UTC](https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082/1 "2022-04-26T17:48:55Z")

</div>

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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/3/4/340d1fcb79ec9f9be406f23d90417bc2cb94ef1d.png)

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

I have tried to blurred using ImageFiltering that way:

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

```

with the result:  
 ![plot_50](https://global.discourse-cdn.com/julialang/original/3X/8/f/8fd445689df96d3fbb79d31f5bd750737437da67.png)

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?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 28, 2022, 9:45am UTC](https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082/2 "2022-04-28T09:45:48Z")

</div>

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

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

```

Resulting in:  
 ![ImageFiltering_kx_ky_kernel_smoothing](https://global.discourse-cdn.com/julialang/original/3X/9/5/951ec8f8dc90abb678a47ad8f9171553bbdebc0e.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 28, 2022, 10:23am UTC](https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082/3 "2022-04-28T10:23:07Z")

</div>

> [@Iddingsite](#):
>
> ```julia
> model = zeros(ny, nx)
> 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
> 
> ```

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

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

```

---

<div class="post-metadata">

### Author: ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)
#### Post date: [May 3, 2022, 10:23am UTC](https://discourse.julialang.org/t/smoothing-2d-data-homogeneously-regardless-of-resolution/80082/4 "2022-05-03T10:23:22Z")

</div>

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,
