Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gaussian Filtering doesn't work for zeros #45

Closed
sebaseb98 opened this issue Sep 16, 2024 · 2 comments
Closed

Gaussian Filtering doesn't work for zeros #45

sebaseb98 opened this issue Sep 16, 2024 · 2 comments

Comments

@sebaseb98
Copy link

the code filter_gaussian(zeros(5)) returns [NaN, NaN, NaN, NaN, NaN].

Not that it is too important to be able to filter zero arrays, but in my case I want to filter images column-wise and it could happen to have columns of zeros. For such cases I would prefer the package to return the correct result rather than work around this bug.
When I looked into the code, I found that the division by the sum might cause the error (its line 315 in src/fourier_filtering.jl) or line 5 in the following snippet.

function filter_gaussian!(arr, sigma=eltype(arr)(1); real_space_kernel=true, border_in=(real(eltype(arr)))(0), border_out=(real(eltype(arr))).(2 ./ (pi .* sigma)), kwargs...)
    if real_space_kernel
        mysum = sum(arr)
        fourier_filter!(arr, gaussian; transform_win=true, sigma=sigma, kwargs...)
        arr .*= (mysum/sum(arr))
        return arr
    else
        return fourier_filter!(arr; border_in=border_in, border_out=border_out, kwargs...)
    end
end

For this particular case a short check that sum(arr) != 0 should do the job, but I am not sure how this fits in the philosophy of the whole package and if there are functions having the same problems

@RainerHeintzmann
Copy link
Member

Thanks a lot for picking this up. The error actually goes a bit deeper. Using the sum of the data is just not the right way to normalize it. E.g. an array can be zero integral also by containing positive and negative values. I thus shifted the (optional) normalization to the kernel and introduced an optional argument, which seems more sensible.
Such a normalization is advantageous for real-space Gaussians which are first transformed and then used for filtering. This is the preferred mode for small kernel sizes.
Here is the pull request.

@roflmaostc
Copy link
Member

I tagged 0.4.6
So please update your package after it got merged
3697e74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants