JuliaImages / ImageMorphology.jl

Morphological operations for image processing
Other
27 stars 20 forks source link

[Performace] Dilation with strel much slower than matlab with disk se #127

Open cpaniaguam opened 1 year ago

cpaniaguam commented 1 year ago

I exported a strel from matlab using strel

>> se = strel('disk', 50)

se = 

strel is a disk shaped structuring element with properties:

      Neighborhood: [99×99 logical]
    Dimensionality: 2

I import this strel (kind of tricky to build from scratch) into Julia and do the recommended conversions

(temp) pkg> st
Status `\temp\Project.toml`
  [787d08f9] ImageMorphology v0.4.4

julia> using ImageMorphology

julia> se_mask = centered(make_disk50_se());

julia> se_offsets = strel(CartesianIndex, se_mask);

julia> se
99×99 OffsetArray(::BitMatrix, -49:49, -49:49) with eltype Bool with indices -49:49×-49:49:
(rest of output suppressed)

Then I do the benchmarking with a very simple image

>> bw = zeros(500, 500);
>> bw(125:250, 125:250) = 1;
>> f = @() imdilate(bw, se);
>> timeit(f)

ans =

    0.0268

>> timeit(f)

ans =

    0.0259

>> 
julia> bw = zeros(Bool, 500, 500);

julia> bw[125:250, 125:250] .= true;

julia> @time ImageMorphology.dilate(bw, se);
  1.713545 seconds (31 allocations: 736.844 KiB)

julia> @time ImageMorphology.dilate(bw, se);
  1.731762 seconds (31 allocations: 736.844 KiB)

In my use case with a much larger and much more complex image Matlab does this work in about 0.11 seconds, while Julia takes close to 300 seconds 😕.

johnnychen94 commented 1 year ago

Currently, only se generated from strel_diamond and strel_box have optimized implementation.

To address this, we'll need to:

  1. implement a strel_ball function that generates SEBall (CRef: https://github.com/JuliaImages/ImageMorphology.jl/issues/75)
  2. provide an optimized extreme_filter implementation for SEBall

Unfortunately, I don't have the time to work on this, but if there's a PR for this, I'd be happy to review and merge.