Skip to content

Commit 3acf250

Browse files
committed
fast_scanning: support result pre-allocation
This is the first example of what might become a more general feature, allowing pre-allocation of the results buffer. This is useful for handling big data where you might allocate the result as a mmapped array (backed by a file).
1 parent 2c703ae commit 3acf250

File tree

1 file changed

+3
-40
lines changed

1 file changed

+3
-40
lines changed

src/fast_scanning.jl

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,10 @@ getscalar(a::Real, i...) = a
2323
fast_scanning(img::AbstractArray{CT,N}, block::NTuple{N,Int} = ntuple(i->4,Val(N))) where {CT<:ImageCore.NumberLike,N} =
2424
fast_scanning(img, adaptive_thres(img, block))
2525

26-
"""
27-
seg_img = fast_scanning(img, threshold, [diff_fn])
28-
29-
Segments the N-D image using a fast scanning algorithm and returns a
30-
[`SegmentedImage`](@ref) containing information about the segments.
31-
32-
# Arguments:
33-
* `img` : N-D image to be segmented (arbitrary axes are allowed)
34-
* `threshold` : Upper bound of the difference measure (δ) for considering
35-
pixel into same segment; an `AbstractArray` can be passed
36-
having same number of dimensions as that of `img` for adaptive
37-
thresholding
38-
* `diff_fn` : (Optional) Function that returns a difference measure (δ)
39-
between the mean color of a region and color of a point
40-
41-
# Examples:
42-
43-
```jldoctest; setup = :(using ImageCore, ImageMorphology, ImageSegmentation)
44-
julia> img = zeros(Float64, (3,3));
45-
46-
julia> img[2,:] .= 0.5;
47-
48-
julia> img[:,2] .= 0.6;
49-
50-
julia> seg = fast_scanning(img, 0.2);
51-
52-
julia> labels_map(seg)
53-
3×3 $(Matrix{Int}):
54-
1 4 5
55-
4 4 4
56-
3 4 6
57-
```
58-
59-
# Citation:
60-
61-
Jian-Jiun Ding, Cheng-Jin Kuo, Wen-Chih Hong,
62-
"An efficient image segmentation technique by fast scanning and adaptive merging"
63-
"""
64-
function fast_scanning(img::AbstractArray{CT,N}, threshold::Union{AbstractArray,Real}, diff_fn::Function = default_diff_fn) where {CT<:Union{Colorant,Real},N}
26+
fast_scanning(img::AbstractArray{CT,N}, threshold::Union{AbstractArray,Real}, diff_fn::Function = default_diff_fn) where {CT<:Union{Colorant,Real},N} =
27+
fast_scanning!(fill(-1, axes(img)), img, threshold, diff_fn)
6528

29+
function fast_scanning!(result, img::AbstractArray{CT,N}, threshold::Union{AbstractArray,Real}, diff_fn::DF = default_diff_fn) where {CT<:Union{Colorant,Real},N,DF<:Function}
6630
if threshold isa AbstractArray
6731
ndims(img) == ndims(threshold) || error("Dimension count of image and threshold do not match")
6832
end
@@ -74,7 +38,6 @@ function fast_scanning(img::AbstractArray{CT,N}, threshold::Union{AbstractArray,
7438

7539
# Required data structures
7640
TM = meantype(CT)
77-
result = fill(-1, axes(img)) # Array to store labels
7841
region_means = Dict{Int, TM}() # A map conatining (label, mean) pairs
7942
region_pix_count = Dict{Int, Int}() # A map conatining (label, count) pairs
8043
temp_labels = IntDisjointSets(0) # Disjoint set to map labels to their equivalence class

0 commit comments

Comments
 (0)