-
-
Notifications
You must be signed in to change notification settings - Fork 18
Make Flips ND compatible and fix ND ScaleKeepAspect, ScaleFixed and PinOrigin #75
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
Changes from 2 commits
ea2324a
424ea6f
c05656f
5458c76
a514e33
fe42f9b
65eb33c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,7 @@ export Item, | |
| WarpAffine, | ||
| FlipX, | ||
| FlipY, | ||
| FlipZ, | ||
| PinOrigin, | ||
| AdjustBrightness, | ||
| AdjustContrast, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,15 +39,15 @@ struct ScaleKeepAspect{N} <: ProjectiveTransform | |
| end | ||
|
|
||
|
|
||
| function getprojection(scale::ScaleKeepAspect{N}, bounds; randstate = nothing) where N | ||
| function getprojection(scale::ScaleKeepAspect{N}, bounds::Bounds{N}; randstate = nothing) where N | ||
| # If no scaling needs to be done, return a noop transform | ||
| scale.minlengths == length.(bounds.rs) && return IdentityTransformation() | ||
|
|
||
| # Offset `minlengths` by 1 to avoid black border on one side | ||
| ratio = maximum((scale.minlengths .+ 1) ./ length.(bounds.rs)) | ||
| upperleft = SVector{N, Float32}(minimum.(bounds.rs)) .- 0.5 | ||
| P = scaleprojection(Tuple(ratio for _ in 1:N)) | ||
| if upperleft != SVector(0, 0) | ||
| if any(upperleft .!= 0) | ||
| P = P ∘ Translation((Float32.(P(upperleft)) .+ 0.5f0)) | ||
| end | ||
| return P | ||
|
|
@@ -75,11 +75,11 @@ struct ScaleFixed{N} <: ProjectiveTransform | |
| end | ||
|
|
||
|
|
||
| function getprojection(scale::ScaleFixed, bounds; randstate = nothing) | ||
| function getprojection(scale::ScaleFixed, bounds::Bounds{N}; randstate = nothing) where N | ||
| ratios = (scale.sizes .+ 1) ./ length.(bounds.rs) | ||
| upperleft = SVector{2, Float32}(minimum.(bounds.rs)) .- 1 | ||
| upperleft = SVector{N, Float32}(minimum.(bounds.rs)) .- 1 | ||
| P = scaleprojection(ratios) | ||
| if upperleft != SVector(0, 0) | ||
| if any(upperleft .!= 0) | ||
| P = P ∘ Translation(-upperleft) | ||
| end | ||
| return P | ||
|
|
@@ -88,7 +88,7 @@ end | |
|
|
||
| function projectionbounds(tfm::ScaleFixed{N}, P, bounds::Bounds{N}; randstate = nothing) where N | ||
| bounds_ = transformbounds(bounds, P) | ||
| return offsetcropbounds(tfm.sizes, bounds_, (1., 1.)) | ||
| return offsetcropbounds(tfm.sizes, bounds_, ntuple(_ -> 1., N)) | ||
| end | ||
|
|
||
| """ | ||
|
|
@@ -167,7 +167,7 @@ struct Reflect <: ProjectiveTransform | |
| end | ||
|
|
||
|
|
||
| function getprojection(tfm::Reflect, bounds; randstate = getrandstate(tfm)) | ||
| function getprojection(tfm::Reflect, bounds::Bounds{2}; randstate = getrandstate(tfm)) | ||
| r = tfm.γ / 360 * 2pi | ||
| return centered(LinearMap(reflectionmatrix(r)), bounds) | ||
| end | ||
|
|
@@ -178,24 +178,45 @@ end | |
| Transform `P` so that is applied around the center of `bounds` | ||
| instead of the origin | ||
| """ | ||
| function centered(P, bounds::Bounds{2}) | ||
| function centered(P, bounds::Bounds{N}) where N | ||
| upperleft = minimum.(bounds.rs) | ||
| bottomright = maximum.(bounds.rs) | ||
|
|
||
| midpoint = SVector{2, Float32}((bottomright .- upperleft) ./ 2) .+ SVector{2, Float32}(.5, .5) | ||
| midpoint = SVector{N, Float32}((bottomright .- upperleft) ./ 2) .+ .5f0 | ||
| return recenter(P, midpoint) | ||
| end | ||
|
|
||
|
|
||
| FlipX() = Reflect(180) | ||
| FlipY() = Reflect(90) | ||
|
|
||
| function reflectionmatrix(r) | ||
| A = SMatrix{2, 2, Float32}(cos(2r), sin(2r), sin(2r), -cos(2r)) | ||
| return round.(A; digits = 12) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| FlipDim{N}() | ||
| Reflect `N` dimensional data along the axis of dimension `dim`. Must satisfy 1 <= `dim` <= `N`. | ||
| ## Examples | ||
| ```julia | ||
| tfm = FlipDim{2}(1) | ||
| ``` | ||
| """ | ||
| struct FlipDim{N} <: ProjectiveTransform | ||
| dim::Int | ||
| FlipDim{N}(dim) where {N} = 1 <= dim <= N ? new{N}(dim) : error("invalid dimension") | ||
| end | ||
| # 2D images use (r, c) = (y, x) convention | ||
| FlipX(N) = FlipDim{N}(N==2 ? 2 : 1) | ||
| FlipY(N) = FlipDim{N}(N==2 ? 1 : 2) | ||
| FlipZ(N) = FlipDim{N}(3) | ||
|
||
|
|
||
| function getprojection(tfm::FlipDim{N}, bounds::Bounds{N}; randstate = nothing) where N | ||
| arr = 1I(N) | ||
| arr[tfm.dim, tfm.dim] = -1 | ||
| M = SMatrix{N, N, Float32}(arr) | ||
| return DataAugmentation.centered(LinearMap(M), bounds) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| PinOrigin() | ||
|
|
||
|
|
@@ -213,8 +234,8 @@ at one. | |
| """ | ||
| struct PinOrigin <: ProjectiveTransform end | ||
|
|
||
| function getprojection(::PinOrigin, bounds; randstate = nothing) | ||
| p = (-SVector{2, Float32}(minimum.(bounds.rs))) .+ 1 | ||
| function getprojection(::PinOrigin, bounds::Bounds{N}; randstate = nothing) where N | ||
| p = (-SVector{N, Float32}(minimum.(bounds.rs))) .+ 1 | ||
| P = Translation(p) | ||
| return P | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this should be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed