diff --git a/docs/src/function_reference.md b/docs/src/function_reference.md index a0c441e..3483d67 100644 --- a/docs/src/function_reference.md +++ b/docs/src/function_reference.md @@ -24,6 +24,7 @@ Kernel.DoG Kernel.LoG Kernel.Laplacian Kernel.moffat +Kernel.tophat ``` # KernelFactors diff --git a/src/ImageFiltering.jl b/src/ImageFiltering.jl index 675a4df..025e1a1 100644 --- a/src/ImageFiltering.jl +++ b/src/ImageFiltering.jl @@ -64,7 +64,7 @@ ArrayLike{T} = Union{ArrayType{T}, AnyIIR{T}} include("kernel.jl") using .Kernel -using .Kernel: Laplacian, reflect, ando3, ando4, ando5, scharr, bickley, prewitt, sobel, gabor, moffat +using .Kernel: Laplacian, reflect, ando3, ando4, ando5, scharr, bickley, prewitt, sobel, gabor, moffat, tophat NDimKernel{N,K} = Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}} diff --git a/src/kernel.jl b/src/kernel.jl index d10c707..03051e2 100644 --- a/src/kernel.jl +++ b/src/kernel.jl @@ -13,6 +13,7 @@ dimensionality. The following kernels are supported: - `Laplacian` - `gabor` - `moffat` + - `tophat` See also: [`KernelFactors`](@ref). """ @@ -416,6 +417,34 @@ moffat(α::Real, β::Real) = moffat(α, β, ceil(Int, (α*2*sqrt sum(x.^2) end +""" + tophat(r) -> k + tophat(r, ls) -> k + +Constructs a 2D Tophat kernel, or a 2D Disk kernel. The default size is the `2r` rounded up to the nearest odd integer. +""" +function tophat(r::Integer) + # default size: 2r rounded up to nearest odd integer + i = ceil(Int, 2 * r) + s = iseven(i) ? i + 1 : i + return tophat(r, (s, s)) +end + +function tophat(r::Real, sizes::Tuple{Integer, Integer}) + u, v = sizes + amplitude = inv(π * r^2) + kern = zeros(typeof(amplitude), u, v) + + cy, cx = u / 2 + 0.5, v / 2 + 0.5 + for idx in CartesianIndices(axes(kern)) + x, y = idx.I + if (x - cx)^2 + (y - cy)^2 ≤ r^2 + kern[idx] = amplitude + end + end + return kern ./ sum(kern) # renormalize +end + """ reflect(kernel) --> reflectedkernel