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

optimized mean filter using IntegralArrays #241

Open
johnnychen94 opened this issue Nov 30, 2021 · 0 comments
Open

optimized mean filter using IntegralArrays #241

johnnychen94 opened this issue Nov 30, 2021 · 0 comments
Labels
good first issue Would make a good contribution from new contributors help wanted

Comments

@johnnychen94
Copy link
Member

When one needs to compute the sum/average of all blocks extracted from an image, pre-building the integral array usually provides a more efficient computation.

using BenchmarkTools, IntegralArrays

# simplified 3x3 mean filter; only for demo purpose
function mean_filter_naive!(out, X)
    Δ = CartesianIndex(1, 1)
    for i in CartesianIndex(2, 2):CartesianIndex(size(X).-1)
        block = @view X[i-Δ: i+Δ]
        out[i] = mean(block)
    end
    return out
end
function mean_filter_integral!(out, X)
    iX = IntegralArray(X)
    for i in CartesianIndex(2, 2):CartesianIndex(size(X).-1)
        x, y = i.I
        out[i] = iX[x±1, y±1]/9
    end
    return out
end

X = Float32.(rand(1:5, 64, 64));
m1 = copy(X);
m2 = copy(X);

@btime mean_filter_naive!($m1, $X); # 65.078 μs (0 allocations: 0 bytes)
@btime mean_filter_integral!($m2, $X); # 12.161 μs (4 allocations: 16.17 KiB)
m1 == m2 # true

One needs to specialize the mapwindow on mean function, e.g.,

function mapwindow(::typeof(mean), img, ...)
    ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Would make a good contribution from new contributors help wanted
Projects
None yet
Development

No branches or pull requests

1 participant