JuliaImages / ImageFiltering.jl

Julia implementations of multidimensional array convolution and nonlinear stencil operations
Other
99 stars 49 forks source link

Allow unitful kernels when filtering unitful AxisArrays #176

Open stillyslalom opened 4 years ago

stillyslalom commented 4 years ago

ref. https://discourse.julialang.org/t/images-kernel-sizes-and-imageaxes/

In the following example, the kernel size is calculated based on array indices, not the provided physical dimensions of the image.

using AxisArrays, Unitful

julia> img = AxisArray(zeros(3, 5),
                       Axis{:x}(1mm:2mm:5mm),
                       Axis{:y}(1mm:1mm:5mm)); img[2, 3] = 1;

julia> img
2-dimensional AxisArray{Float64,2,...} with axes:
    :x, (1:2:5) mm
    :y, (1:5) mm
And data, a 3×5 Array{Float64,2}:
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0

julia> imfilter(img, Kernel.LoG((0.25, 0.25)))
2-dimensional AxisArray{Float64,2,...} with axes:
    :x, (1:2:5) mm
    :y, (1:5) mm
And data, a 3×5 Array{Float64,2}:
 0.0  0.000137553    0.191352  0.000137553  0.0
 0.0  0.191352     -81.4873    0.191352     0.0
 0.0  0.000137553    0.191352  0.000137553  0.0

An error is thrown when physical dimensions are provided for the kernel:

julia> imfilter(img, Kernel.LoG((0.25mm, 0.25mm)))
ERROR: round can only be well-defined for dimensionless numbers. For dimensionful numbers, different input units yield physically different results.
Stacktrace:
 [1] error(::String, ::String, ::String) at .\error.jl:42
 [2] _dimerr(::Symbol) at C:\Users\Alex\.julia\packages\Unitful\KE9TK\src\quantities.jl:293
 [3] round(::Type{Int64}, ::Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}, ::RoundingMode{:Up}; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\Alex\.julia\packages\Unitful\KE9TK\src\quantities.jl:316
 [4] round(::Type{Int64}, ::Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}, ::RoundingMode{:Up}) at C:\Users\Alex\.julia\packages\Unitful\KE9TK\src\quantities.jl:316
 [5] ceil(::Type{Int64}, ::Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\Alex\.julia\packages\Unitful\KE9TK\src\quantities.jl:335
 [6] ceil(::Type{Int64}, ::Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}) at C:\Users\Alex\.julia\packages\Unitful\KE9TK\src\quantities.jl:335
 [7] (::ImageFiltering.Kernel.var"#3#6")(::Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}) at C:\Users\Alex\.julia\packages\ImageFiltering\eH8Od\src\kernel.jl:316
 [8] map(::ImageFiltering.Kernel.var"#3#6", ::Tuple{Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}},Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}}) at .\tuple.jl:158
 [9] LoG(::Tuple{Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}},Quantity{Float64,�,Unitful.FreeUnits{(mm,),�,nothing}}}) at C:\Users\Alex\.julia\packages\ImageFiltering\eH8Od\src\kernel.jl:316
 [10] top-level scope at REPL[39]:1
timholy commented 4 years ago

Yes, when the kernel's dimensions are provided in physical units, this is doable. ImageFiltering does not currently depend on Unitful, but it does use Requires, and so we can add a "soft" dependency. PR welcome, or someone will get around to this eventually.

yakir12 commented 2 years ago

Came here for the same reason.

I'd add that if the input image is an AxisArray then the dimensions of the kernel should be the same as the dimensions of the axes of the image -- regardless of any Unitful dimensions. So, I think that:

img = AxisArray(zeros(3, 3), 
    Axis{:x}(0:100:200),
    Axis{:y}(0:100:200))
imfilter(img, Kernel.LoG(10))

should result in a similar image to:

img = zeros(3, 3)
imfilter(img, Kernel.LoG(0.1))