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

Add findall_window #225

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion src/ImageFiltering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export Kernel, KernelFactors,
BorderArray,
Algorithm,
imfilter, imfilter!,
mapwindow, mapwindow!,
mapwindow, mapwindow!, findall_window,
imgradients, padarray, centered, kernelfactors, reflect,
freqkernel, spacekernel,
findlocalminima, findlocalmaxima,
Expand Down Expand Up @@ -89,6 +89,7 @@ include("specialty.jl")
include("mapwindow.jl")
using .MapWindow
include("extrema.jl")
include("findall_window.jl")

function __init__()
# See ComputationalResources README for explanation
Expand Down
51 changes: 51 additions & 0 deletions src/findall_window.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
findall_window(f, img::AbstractArray{T,N}, window::Dims{N}; allow_edges::(true...,))

Return the list of window-centers for which `f(V, basepoint)` returns `true` for windowed-views `V`
"centered" on `basepoint`. At the edge of `img`, `basepoint` may not actually be in the center of `V`.
"""
function findall_window(f::F, img::AbstractArray{T,N}, window::Dims{N}; allow_edges::NTuple{N,Bool}=ntuple(_->true, N)) where {F,T<:Union{Gray,Number},N}
basepoints = Vector{CartesianIndex{N}}(undef, 0)
Iedge = CartesianIndex(map(!, allow_edges))
R0 = CartesianIndices(img)
R = clippedinds(R0, Iedge)
halfwindow = CartesianIndex(map(x -> x >> 1, window))
for i in R
Rview = _colon(i-halfwindow, i+halfwindow) ∩ R0
if f(OffsetArray(@inbounds(view(img, Rview)), Rview.indices), i)
push!(basepoints, i)

Check warning on line 16 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L7-L16

Added lines #L7 - L16 were not covered by tests
end
end
return basepoints

Check warning on line 19 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L18-L19

Added lines #L18 - L19 were not covered by tests
end

"""
all_window(f, V, basepoint, excludepoint=nothing)

Returns `true` if `f(img[centerpoint], img[otherpoint])` for all indices `otherpoint`.
Optionally exclude a single point (e.g., `basepoint` itself) by setting `excludepoint`.
"""
@inline function all_window(f::F, V::AbstractArray, basepoint::CartesianIndex, excludepoint::Union{Nothing,CartesianIndex}=nothing) where F
@inbounds ref = V[basepoint]
@inbounds for (i, v) in pairs(V)
i == excludepoint || f(ref, v) || return false
end
return true

Check warning on line 33 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L28-L33

Added lines #L28 - L33 were not covered by tests
end

"""
ismax_window(V, basepoint)

Returns `true` if `V[basepoint] > V[otherpoint]` for all indices `otherpoint != basepoint` in `V`.
"""
@inline ismax_window(V, i) = all_window(>, V, i, i)

Check warning on line 41 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L41

Added line #L41 was not covered by tests

"""
ismin_window(V, basepoint)

Returns `true` if `V[basepoint] < V[otherpoint]` for all indices `otherpoint != basepoint` in `V`.
"""
@inline ismin_window(V, i) = all_window(<, V, i, i)

Check warning on line 48 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L48

Added line #L48 was not covered by tests

findmax_window(A, window; kwargs...) = findall_window(ismax_window, A, window; kwargs...)
findmin_window(A, window; kwargs...) = findall_window(ismin_window, A, window; kwargs...)

Check warning on line 51 in src/findall_window.jl

View check run for this annotation

Codecov / codecov/patch

src/findall_window.jl#L50-L51

Added lines #L50 - L51 were not covered by tests