|
| 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 |
0 commit comments