From b0bec8d46fd864185a609456c7c98e1fb8724e34 Mon Sep 17 00:00:00 2001 From: Miles Lucas Date: Fri, 17 Apr 2020 23:39:37 -1000 Subject: [PATCH 1/2] add tophat filter --- docs/src/function_reference.md | 1 + src/ImageFiltering.jl | 2 +- src/kernel.jl | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) 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..7955327 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 +end + """ reflect(kernel) --> reflectedkernel From c6f1071a09972956b1c3faf683b3c9a6f226e9fb Mon Sep 17 00:00:00 2001 From: Miles Lucas Date: Fri, 17 Apr 2020 23:45:34 -1000 Subject: [PATCH 2/2] add normalization step --- src/kernel.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel.jl b/src/kernel.jl index 7955327..03051e2 100644 --- a/src/kernel.jl +++ b/src/kernel.jl @@ -442,7 +442,7 @@ function tophat(r::Real, sizes::Tuple{Integer, Integer}) kern[idx] = amplitude end end - return kern + return kern ./ sum(kern) # renormalize end """