Skip to content

Commit 6b51789

Browse files
authored
Add box and laplacian2d (#226)
These come from Images.jl and are part of moving most/all code out to make it a meta-package.
1 parent 9ce17bd commit 6b51789

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/kernel.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ function product2d(kf)
3636
k1[1].*k1[2], k2[1].*k2[2]
3737
end
3838

39+
"""
40+
kern = box((m, n, ...))
41+
42+
Return a box kernel computing a moving average. `m, n, ...` specify the size of the kernel, which is centered around zero.
43+
"""
44+
box(sz::Dims) = broadcast(*, KernelFactors.box(sz)...)
45+
# We don't support box(m::Int...) mostly because of `gaussian(σ::Real) = gaussian((σ, σ))` defaulting to
46+
# isotropic 2d rather than a 1d Gaussian.
47+
3948
"""
4049
```julia
4150
diff1, diff2 = sobel()
@@ -380,6 +389,40 @@ function Base.convert(::Type{AbstractArray}, L::Laplacian{N}) where N
380389
end
381390
_reshape(L::Laplacian{N}, ::Val{N}) where {N} = L
382391

392+
"""
393+
laplacian2d(alpha::Number)
394+
395+
Construct a weighted discrete Laplacian approximation in 2d. `alpha` controls the weighting of the faces
396+
relative to the corners.
397+
398+
# Examples
399+
400+
```jldoctest
401+
julia> Kernel.laplacian2d(0) # the standard Laplacian
402+
3×3 OffsetArray(::Matrix{Float64}, -1:1, -1:1) with eltype Float64 with indices -1:1×-1:1:
403+
0.0 1.0 0.0
404+
1.0 -4.0 1.0
405+
0.0 1.0 0.0
406+
407+
julia> Kernel.laplacian2d(1) # a corner-focused Laplacian
408+
3×3 OffsetArray(::Matrix{Float64}, -1:1, -1:1) with eltype Float64 with indices -1:1×-1:1:
409+
0.5 0.0 0.5
410+
0.0 -2.0 0.0
411+
0.5 0.0 0.5
412+
413+
julia> Kernel.laplacian2d(0.5) # equal weight for face-pixels and corner-pixels.
414+
3×3 OffsetArray(::Matrix{Float64}, -1:1, -1:1) with eltype Float64 with indices -1:1×-1:1:
415+
0.333333 0.333333 0.333333
416+
0.333333 -2.66667 0.333333
417+
0.333333 0.333333 0.333333
418+
```
419+
"""
420+
function laplacian2d(alpha::Number=0)
421+
lc = alpha/(1 + alpha)
422+
lb = (1 - alpha)/(1 + alpha)
423+
lm = -4/(1 + alpha)
424+
return centered([lc lb lc; lb lm lb; lc lb lc])
425+
end
383426

384427
"""
385428
gabor(size_x,size_y,σ,θ,λ,γ,ψ) -> (k_real,k_complex)

src/kernelfactors.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
each stored in terms of their factors. The following kernels are
44
supported:
55
6+
- `box`
67
- `sobel`
78
- `prewitt`
89
- `ando3`, `ando4`, and `ando5` (the latter in 2d only)
@@ -148,6 +149,18 @@ end
148149

149150
#### FIR filters
150151

152+
"""
153+
kern1, kern2 = box(m, n)
154+
kerns = box((m, n, ...))
155+
156+
Return a tuple of "flat" kernels, where, for example, `kern1[i] == 1/m` and has length `m`.
157+
"""
158+
function box(sz::Dims)
159+
all(isodd, sz) || throw(ArgumentError("kernel dimensions must be odd, got $sz"))
160+
return kernelfactors(ntuple(d -> centered([1/sz[d] for i=1:sz[d]]), length(sz)))
161+
end
162+
box(sz::Int...) = box(sz)
163+
151164
## gradients
152165

153166
"""

test/specialty.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ using ImageFiltering: IdentityUnitRange
150150
z z z z edgecoef*c]
151151
end
152152
end
153+
@test Kernel.laplacian2d(0.5) centered([ 1/3 1/3 1/3;
154+
1/3 -8/3 1/3;
155+
1/3 1/3 1/3])
156+
end
157+
158+
@testset "box" begin
159+
@test KernelFactors.box(3) == (centered([1/3, 1/3, 1/3]),)
160+
@test KernelFactors.box(3, 5) == (centered(reshape([1/3, 1/3, 1/3], 3, 1)), centered([1/5, 1/5, 1/5, 1/5, 1/5]'))
161+
@test KernelFactors.box((3,5,7)) == KernelFactors.box(3, 5, 7)
162+
@test Kernel.box((3,)) == centered([1/3, 1/3, 1/3])
163+
@test Kernel.box((3, 5)) == centered([1/3, 1/3, 1/3] .* [1/5, 1/5, 1/5, 1/5, 1/5]')
164+
@test_throws ArgumentError KernelFactors.box((3, 4, 5))
165+
@test_throws ArgumentError Kernel.box((3, 4, 5))
153166
end
154167

155168
@testset "gaussian" begin

0 commit comments

Comments
 (0)