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

2D Tophat (Disk) Kernel #165

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/function_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Kernel.DoG
Kernel.LoG
Kernel.Laplacian
Kernel.moffat
Kernel.tophat
```

# KernelFactors
Expand Down
2 changes: 1 addition & 1 deletion src/ImageFiltering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
29 changes: 29 additions & 0 deletions src/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dimensionality. The following kernels are supported:
- `Laplacian`
- `gabor`
- `moffat`
- `tophat`

See also: [`KernelFactors`](@ref).
"""
Expand Down Expand Up @@ -416,6 +417,34 @@ moffat(α::Real, β::Real) = moffat(α, β, ceil(Int, (α*2*sqrt
sum(x.^2)
end

"""
tophat(r) -> k
tophat(r, ls) -> k
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename ls to size and explain it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also some doctests would be nice.


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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you compute amplitude above, if you normalize it away here anyway?

end

"""
reflect(kernel) --> reflectedkernel

Expand Down