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

Compatibility with ImageTransformations 0.9 #64

Merged
merged 4 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/items/image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 51 additions & 1 deletion src/projective/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down Expand Up @@ -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...)
4 changes: 3 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ authors = ["lorenzoh <lorenz.ohly@gmail.com>"]
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"
3 changes: 2 additions & 1 deletion test/imports.jl
Original file line number Diff line number Diff line change
@@ -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,
Expand Down