Skip to content

Commit 33bf57a

Browse files
authored
Merge pull request #43 from lorenzoh/lorenzoh/hotfix
Hotfixes
2 parents 7918c34 + 3cdcc6e commit 33bf57a

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

src/projective/affine.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@ end
4040

4141

4242
function getprojection(scale::ScaleKeepAspect{N}, bounds; randstate = nothing) where N
43-
ratio = maximum(scale.minlengths ./ length.(bounds.rs))
43+
# Offset `minlengths` by 1 to avoid black border on one side
44+
ratio = maximum((scale.minlengths .+ 1) ./ length.(bounds.rs))
4445
upperleft = SVector{N, Float32}(minimum.(bounds.rs)) .- 1
4546
P = scaleprojection(Tuple(ratio for _ in 1:N))
4647
if upperleft != SVector(0, 0)
47-
P = P Translation(-upperleft)
48+
P = P Translation(-upperleft)
4849
end
4950
return P
5051
end
5152

5253
function projectionbounds(tfm::ScaleKeepAspect{N}, P, bounds::Bounds{N}; randstate = nothing) where N
5354
origsz = length.(bounds.rs)
54-
ratio = maximum(tfm.minlengths ./ origsz)
55-
sz = round.(Int, ratio .* origsz)
55+
ratio = maximum((tfm.minlengths) ./ origsz)
56+
sz = floor.(Int,ratio .* origsz)
5657
bounds_ = transformbounds(bounds, P)
57-
return offsetcropbounds(sz, bounds_, ntuple(_ -> 1., N))
58+
bs_ = offsetcropbounds(sz, bounds_, ntuple(_ -> 1., N))
59+
return bs_
5860
end
5961

6062
"""
@@ -71,7 +73,7 @@ end
7173

7274

7375
function getprojection(scale::ScaleFixed, bounds; randstate = nothing)
74-
ratios = scale.sizes ./ length.(bounds.rs)
76+
ratios = (scale.sizes .+ 1) ./ length.(bounds.rs)
7577
upperleft = SVector{2, Float32}(minimum.(bounds.rs)) .- 1
7678
P = scaleprojection(ratios)
7779
if upperleft != SVector(0, 0)
@@ -209,11 +211,12 @@ at one.
209211
struct PinOrigin <: ProjectiveTransform end
210212

211213
function getprojection(::PinOrigin, bounds; randstate = nothing)
212-
# TODO: translate by actual minimum x and y coordinates
213-
return Translation((-SVector{2, Float32}(minimum.(bounds.rs))) .+ 1)
214+
p = (-SVector{2, Float32}(minimum.(bounds.rs))) .+ 1
215+
P = Translation(p)
216+
return P
214217
end
215218

216-
function apply(::PinOrigin, item::Union{Image, MaskMulti, MaskBinary}; randstate = nothing)
219+
function apply(::PinOrigin, item::Union{<:Image, <:MaskMulti, <:MaskBinary}; randstate = nothing)
217220
item = @set item.data = parent(itemdata(item))
218221
item = @set item.bounds = Bounds(size(itemdata(item)))
219222
return item
@@ -229,6 +232,8 @@ end
229232
# This overwrites the default composition.
230233

231234
compose(cropped::CroppedProjectiveTransform, pin::PinOrigin) = Sequence(cropped, pin)
235+
compose(cropped::ComposedProjectiveTransform, pin::PinOrigin) = Sequence(cropped, pin)
236+
compose(cropped::ProjectiveTransform, pin::PinOrigin) = Sequence(cropped, pin)
232237

233238
# ## Resize crops
234239

src/projective/crop.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ function projectionbounds(
8484
sz = length.(ranges)
8585
pad = (cropped.crop.by .- (sz .% cropped.crop.by)) .% cropped.crop.by
8686

87-
indices = UnitRange.(getindex.(ranges, 1), sz .+ pad)
88-
return Bounds(indices)
87+
start = minimum.(ranges)
88+
end_ = start .+ sz .+ pad .- 1
89+
rs = UnitRange.(start, end_)
90+
return Bounds(rs)
8991
end
9092

9193

src/projective/warp.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ function getprojection(
1818
randstate = getrandstate(tfm))
1919
T = Float32
2020
rng = Random.seed!(Random.MersenneTwister(), randstate)
21-
scale = sqrt(prod(sum.(bounds.rs)))
21+
scale = sqrt(prod(length.(bounds.rs)))
2222

2323
srcps = shuffle(rng, SVector{2, T}.(corners(bounds)))[1:3]
2424
offsets = rand(rng, SVector{2, T}, 3)
2525
offsets = map(v -> (v .* (2one(T)) .- one(T)) .* convert(T, scale * tfm.σ), offsets)
26+
2627
return threepointwarpaffine(srcps, srcps .+ offsets)
2728
end
2829

test/projective/affine.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ include("../imports.jl")
6060
imbounds = getbounds(image)
6161
kpbounds = getbounds(keypoints)
6262
tfm1 = ScaleKeepAspect((50, 50))
63-
@test getprojection(tfm1, imbounds) getprojection(ScaleRatio((1, 1)), imbounds)
64-
@test getprojection(tfm1, kpbounds) getprojection(ScaleRatio((1, 1)), kpbounds)
65-
6663
tfm2 = ScaleKeepAspect((25, 25))
67-
@test getprojection(tfm2, imbounds) getprojection(ScaleRatio((1 / 2, 1 / 2)), imbounds)
68-
@test getprojection(tfm2, kpbounds) getprojection(ScaleRatio((1 / 2, 1 / 2)), kpbounds)
64+
testprojective(tfm1)
65+
testprojective(tfm2)
6966
end
7067

7168

@@ -79,7 +76,7 @@ include("../imports.jl")
7976
@test_nowarn apply(tfm, keypoints)
8077
timage = apply(tfm, image)
8178
tkeypoints = apply(tfm, keypoints)
82-
@test getbounds(timage).rs == (1:25, 1:25)
79+
@test length.(getbounds(timage).rs) == (25, 25)
8380
@test getbounds(timage) == getbounds(tkeypoints)
8481
testprojective(tfm)
8582

test/projective/crop.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ end
2525
item = Image(img)
2626
tfm = ResizePadDivisible((32, 32), 4)
2727
titem = apply(tfm, item)
28-
@show size(titem.data)
2928
@test size(titem.data) == (32, 48)
3029
end

0 commit comments

Comments
 (0)