Skip to content

Commit 7ad438e

Browse files
authored
Merge pull request #64 from lorenzoh/lo/imagetransformations-0.9
Compatibility with ImageTransformations 0.9
2 parents 792cddb + d89b877 commit 7ad438e

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CoordinateTransformations = "0.6"
2929
Distributions = "0.24, 0.25"
3030
ImageCore = "0.8, 0.9"
3131
ImageDraw = "0.2"
32-
ImageTransformations = "0.8"
32+
ImageTransformations = "0.8, 0.9"
3333
IndirectArrays = "0.5, 1"
3434
Interpolations = "0.13"
3535
MosaicViews = "0.2, 0.3"

src/items/image.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function project!(bufimage::Image, P, image::Image{N, T}, bounds::Bounds{N}) whe
8888
a = OffsetArray(parent(itemdata(bufimage)), bounds.rs)
8989
res = warp!(
9090
a,
91-
box_extrapolation(itemdata(image), zero(T)),
91+
box_extrapolation(itemdata(image); fillvalue=zero(T)),
9292
inv(P),
9393
)
9494
return Image(res, bounds)

src/projective/base.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949
Apply `CoordinateTransformations.Transformation` to `bounds`.
5050
"""
5151
function transformbounds(bounds::Bounds, P::CoordinateTransformations.Transformation)
52-
return Bounds(ImageTransformations.autorange(CartesianIndices(bounds.rs), P))
52+
return Bounds(_autorange(CartesianIndices(bounds.rs), P))
5353
end
5454

5555
"""
@@ -129,3 +129,53 @@ struct Project{T<:CoordinateTransformations.Transformation} <: ProjectiveTransfo
129129
end
130130

131131
getprojection(tfm::Project, bounds; randstate = nothing) = tfm.P
132+
133+
# ## ImageTransformations.jl 0.8 internal functionality port
134+
#
135+
# Ported from ImageTransformations 0.8, since 0.9 introduced changes that broke
136+
# some assumptions.
137+
138+
function _autorange(img, tform)
139+
R = CartesianIndices(axes(img))
140+
autorange(R, tform)
141+
end
142+
143+
function _autorange(R::CartesianIndices, tform)
144+
tform = _round(tform)
145+
mn = mx = tform(SVector(first(R).I))
146+
for I in ImageTransformations.CornerIterator(R)
147+
x = tform(SVector(I.I))
148+
# we map min and max to prevent type-inference issues
149+
# (because min(::SVector,::SVector) -> Vector)
150+
mn = map(min, x, mn)
151+
mx = map(max, x, mx)
152+
end
153+
_autorange(Tuple(mn), Tuple(mx))
154+
end
155+
156+
@noinline _autorange(mn::Tuple, mx::Tuple) = map((a,b)->floor(Int,a):ceil(Int,b), mn, mx)
157+
158+
159+
# Slightly round/discretize the transformation so that the warpped image size isn't affected by
160+
# numerical stability
161+
# https://github.com/JuliaImages/ImageTransformations.jl/issues/104
162+
_default_digits(::Type{T}) where T<:Number = _default_digits(floattype(T))
163+
# these constants come from eps() digits
164+
_default_digits(::Type{<:AbstractFloat}) = 15
165+
_default_digits(::Type{Float64}) = 15
166+
_default_digits(::Type{Float32}) = 7
167+
168+
function _round(tform::T; kwargs...) where T<:CoordinateTransformations.Transformation
169+
rounded_fields = map(Base.OneTo(fieldcount(T))) do i
170+
__round(getfield(tform, i); kwargs...)
171+
end
172+
T(rounded_fields...)
173+
end
174+
if isdefined(Base, :ComposedFunction)
175+
_round(tform::ComposedFunction; kwargs...) = _round(tform.outer; kwargs...) _round(tform.inner; kwargs...)
176+
end
177+
_round(tform; kwargs...) = tform
178+
179+
__round(x; kwargs...) = x
180+
__round(x::AbstractArray; digits=_default_digits(eltype(x)), kwargs...) = round.(x; digits=digits, kwargs...)
181+
__round(x::T; digits=_default_digits(T), kwargs...) where T<:Number = round(x; digits=digits, kwargs...)

test/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ authors = ["lorenzoh <[email protected]>"]
33
version = "0.1.0"
44

55
[deps]
6+
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
67
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
8+
DataAugmentation = "88a5189c-e7ff-4f85-ac6b-e6158070f02e"
9+
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
710
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
8-
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
911
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1012
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1113
TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04"

test/imports.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using DataAugmentation
2-
using Images
32
using StaticArrays
43
using Test
54
using TestSetExtensions
65
using CoordinateTransformations
6+
using Colors
7+
using FixedPointNumbers: N0f8
78

89

910
using DataAugmentation: Item, Transform, getrandstate, itemdata, setdata, ComposedProjectiveTransform,

0 commit comments

Comments
 (0)