Skip to content

Commit 160bee5

Browse files
committed
add WarpAffine
1 parent cb34b5e commit 160bee5

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
1414
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1515
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1616
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
17+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1718
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
1819
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
1920
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

src/DataAugmentation.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using Interpolations
1212
using OffsetArrays: OffsetArray
1313
using LinearAlgebra: I
1414
using Parameters
15+
using Random
1516
using Rotations
1617
using Setfield
1718
using StaticArrays
@@ -32,6 +33,7 @@ include("./projective/bounds.jl")
3233
include("./projective/compose.jl")
3334
include("./projective/crop.jl")
3435
include("./projective/affine.jl")
36+
include("./projective/warp.jl")
3537
include("./preprocessing.jl")
3638
include("./colortransforms.jl")
3739

@@ -68,6 +70,7 @@ export Item,
6870
OneHot,
6971
apply,
7072
Reflect,
73+
WarpAffine,
7174
FlipX,
7275
FlipY,
7376
PinOrigin,

src/projective/warp.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
WarpAffine(σ = 0.1) <: ProjectiveTransform
3+
4+
A three-point affine warp calculated by randomly moving 3 corners
5+
of an item. Similar to a random translation, shear and rotation.
6+
"""
7+
struct WarpAffine <: ProjectiveTransform
8+
σ
9+
end
10+
11+
12+
getrandstate(::WarpAffine) = abs(rand(Int))
13+
14+
15+
function getprojection(
16+
tfm::WarpAffine,
17+
bounds::AbstractArray{SVector{2, T}};
18+
randstate = getrandstate(tfm)) where T
19+
rng = Random.seed!(Random.MersenneTwister(), randstate)
20+
scale = sqrt(prod(boundssize(bounds)))
21+
22+
srcps = shuffle(bounds)[1:3]
23+
offsets = rand(SVector{2, T}, 3)
24+
offsets = map(v -> (v .* (2one(T)) .- one(T)) .* convert(T, scale * tfm.σ), offsets)
25+
return threepointwarpaffine(srcps, srcps .+ offsets)
26+
end
27+
28+
29+
# Adapted from
30+
"""
31+
threepointwarpaffine(srcps, dstps)
32+
33+
Calculate an affine [`CoordinateTransformations.LinearMap`](#)
34+
from 3 source points to 3 destination points.
35+
36+
Adapted from [CoordinateTransformations.jl#30](https://github.com/JuliaGeometry/CoordinateTransformations.jl/issues/30#issuecomment-610337378).
37+
"""
38+
function threepointwarpaffine(
39+
srcps::T, dstps::T) where {V, T<:AbstractArray{<:SVector{2,V}}}
40+
X = vcat(hcat(dstps...), ones(1,3))'
41+
Y = hcat(srcps...)'
42+
c = (X \ Y)'
43+
A = SMatrix{2, 2, V}(c[:, 1:2])
44+
b = SVector{2, V}(c[:, 3])
45+
AffineMap(A, b)
46+
end

test/projective/warp.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
include("../imports.jl")
3+
4+
@testset ExtendedTestSet "WarpAffine" begin
5+
tfm = WarpAffine(0.1)
6+
image = Image(rand(RGB, 50, 50))
7+
8+
@test_nowarn apply(tfm, image)
9+
@test boundssize(getbounds(apply(tfm, image))) != (50, 500)
10+
buf = apply(tfm, image)
11+
ctfm = tfm |> CenterCrop((50, 50))
12+
@test_nowarn apply(ctfm, image)
13+
buf = apply(ctfm, image)
14+
@test_nowarn apply!(buf, ctfm, image)
15+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include("./imports.jl")
99
include("projective/compose.jl")
1010
include("projective/crop.jl")
1111
include("projective/affine.jl")
12+
include("projective/warp.jl")
1213
include("items/image.jl")
1314
include("items/keypoints.jl")
1415
include("items/mask.jl")

0 commit comments

Comments
 (0)