From 446b75d5ffd3dfbf6a0b09ae41cd0e32a4dad996 Mon Sep 17 00:00:00 2001 From: lorenzoh Date: Tue, 19 Apr 2022 12:45:55 +0200 Subject: [PATCH 1/4] Reduce test dependencies --- test/Project.toml | 4 +++- test/imports.jl | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 10e5996..6fbb4a0 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -3,9 +3,11 @@ authors = ["lorenzoh "] version = "0.1.0" [deps] +Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298" +DataAugmentation = "88a5189c-e7ff-4f85-ac6b-e6158070f02e" +FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31" -Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04" diff --git a/test/imports.jl b/test/imports.jl index 0c9ffc7..b87c72a 100644 --- a/test/imports.jl +++ b/test/imports.jl @@ -1,9 +1,10 @@ using DataAugmentation -using Images using StaticArrays using Test using TestSetExtensions using CoordinateTransformations +using Colors +using FixedPointNumbers: N0f8 using DataAugmentation: Item, Transform, getrandstate, itemdata, setdata, ComposedProjectiveTransform, From e46ac3d1e65488155ad9681bb2bb8cdc80f5c709 Mon Sep 17 00:00:00 2001 From: lorenzoh Date: Wed, 20 Apr 2022 08:17:51 +0200 Subject: [PATCH 2/4] Support ImageTransformations 0.9 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 12fbd6e..c3f86b4 100644 --- a/Project.toml +++ b/Project.toml @@ -29,7 +29,7 @@ CoordinateTransformations = "0.6" Distributions = "0.24, 0.25" ImageCore = "0.8, 0.9" ImageDraw = "0.2" -ImageTransformations = "0.8" +ImageTransformations = "0.8, 0.9" IndirectArrays = "0.5, 1" Interpolations = "0.13" MosaicViews = "0.2, 0.3" From b57ae4722285f1d864f7cad0a0d27992ef50c337 Mon Sep 17 00:00:00 2001 From: lorenzoh Date: Wed, 20 Apr 2022 09:15:32 +0200 Subject: [PATCH 3/4] Update `box_extrapolation` call --- src/items/image.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items/image.jl b/src/items/image.jl index 7360eec..0da6cfd 100644 --- a/src/items/image.jl +++ b/src/items/image.jl @@ -88,7 +88,7 @@ function project!(bufimage::Image, P, image::Image{N, T}, bounds::Bounds{N}) whe a = OffsetArray(parent(itemdata(bufimage)), bounds.rs) res = warp!( a, - box_extrapolation(itemdata(image), zero(T)), + box_extrapolation(itemdata(image); fillvalue=zero(T)), inv(P), ) return Image(res, bounds) From d89b877df957e549009ce9652d41c7a3eac5ffde Mon Sep 17 00:00:00 2001 From: lorenzoh Date: Wed, 20 Apr 2022 09:17:02 +0200 Subject: [PATCH 4/4] Port `autorange` from ImageTransformations.jl 0.8 Changes made in https://github.com/JuliaImages/ImageTransformations.jl/pull/143 lead to some output size changes after projective transformations that leave images with a black border. To revert to the pre 0.9 behavior, this ports the old `autorange`. --- src/projective/base.jl | 52 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/projective/base.jl b/src/projective/base.jl index 59d6c14..c17aa3c 100644 --- a/src/projective/base.jl +++ b/src/projective/base.jl @@ -49,7 +49,7 @@ end Apply `CoordinateTransformations.Transformation` to `bounds`. """ function transformbounds(bounds::Bounds, P::CoordinateTransformations.Transformation) - return Bounds(ImageTransformations.autorange(CartesianIndices(bounds.rs), P)) + return Bounds(_autorange(CartesianIndices(bounds.rs), P)) end """ @@ -129,3 +129,53 @@ struct Project{T<:CoordinateTransformations.Transformation} <: ProjectiveTransfo end getprojection(tfm::Project, bounds; randstate = nothing) = tfm.P + +# ## ImageTransformations.jl 0.8 internal functionality port +# +# Ported from ImageTransformations 0.8, since 0.9 introduced changes that broke +# some assumptions. + +function _autorange(img, tform) + R = CartesianIndices(axes(img)) + autorange(R, tform) +end + +function _autorange(R::CartesianIndices, tform) + tform = _round(tform) + mn = mx = tform(SVector(first(R).I)) + for I in ImageTransformations.CornerIterator(R) + x = tform(SVector(I.I)) + # we map min and max to prevent type-inference issues + # (because min(::SVector,::SVector) -> Vector) + mn = map(min, x, mn) + mx = map(max, x, mx) + end + _autorange(Tuple(mn), Tuple(mx)) +end + +@noinline _autorange(mn::Tuple, mx::Tuple) = map((a,b)->floor(Int,a):ceil(Int,b), mn, mx) + + +# Slightly round/discretize the transformation so that the warpped image size isn't affected by +# numerical stability +# https://github.com/JuliaImages/ImageTransformations.jl/issues/104 +_default_digits(::Type{T}) where T<:Number = _default_digits(floattype(T)) +# these constants come from eps() digits +_default_digits(::Type{<:AbstractFloat}) = 15 +_default_digits(::Type{Float64}) = 15 +_default_digits(::Type{Float32}) = 7 + +function _round(tform::T; kwargs...) where T<:CoordinateTransformations.Transformation + rounded_fields = map(Base.OneTo(fieldcount(T))) do i + __round(getfield(tform, i); kwargs...) + end + T(rounded_fields...) +end +if isdefined(Base, :ComposedFunction) + _round(tform::ComposedFunction; kwargs...) = _round(tform.outer; kwargs...) ∘ _round(tform.inner; kwargs...) +end +_round(tform; kwargs...) = tform + +__round(x; kwargs...) = x +__round(x::AbstractArray; digits=_default_digits(eltype(x)), kwargs...) = round.(x; digits=digits, kwargs...) +__round(x::T; digits=_default_digits(T), kwargs...) where T<:Number = round(x; digits=digits, kwargs...)