-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
julia 0.6 | ||
MappedArrays 0.0.3 | ||
ImageCore 0.1.2 | ||
ImageTransformations 0.3.0 | ||
ImageFiltering 0.1.4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
ConvertEltype <: Augmentor.Operation | ||
Description | ||
-------------- | ||
Convert the element type of the given array/image into the given | ||
`eltype`. This operation is especially useful for converting | ||
color images to grayscale (or the other way around). That said | ||
the operation is not specific to color types and can also be used | ||
for numeric arrays (e.g. with separated channels). | ||
Note that this is an element-wise convert function. Thus it can | ||
not be used to combine or separate color channels. Use | ||
[`SplitChannels`](@ref) or [`CombineChannels`](@ref) for those | ||
purposes. | ||
Usage | ||
-------------- | ||
ConvertEltype(eltype) | ||
Arguments | ||
-------------- | ||
- **`eltype`** : The eltype of the resulting array/image. | ||
Examples | ||
-------------- | ||
```julia | ||
julia> using Augmentor, Colors | ||
julia> A = rand(RGB, 10, 10) # three color channels | ||
10×10 Array{RGB{Float64},2}: | ||
[...] | ||
julia> augment(A, ConvertEltype(Gray)) # convert to grayscale | ||
10×10 Array{Gray{Float64},2}: | ||
[...] | ||
julia> augment(A, ConvertEltype(Gray{Float32})) # more specific | ||
10×10 Array{Gray{Float32},2}: | ||
[...] | ||
``` | ||
see also | ||
-------------- | ||
[`CombineChannels`](@ref), [`SplitChannels`](@ref), [`augment`](@ref) | ||
""" | ||
struct ConvertEltype{T} <: Operation | ||
eltype::Type{T} | ||
end | ||
|
||
@inline supports_lazy(::Type{<:ConvertEltype}) = true | ||
|
||
function applyeager(op::ConvertEltype{T}, img::AbstractArray) where T | ||
convert(Array{T}, img) | ||
end | ||
|
||
function applylazy(op::ConvertEltype{T}, img::AbstractArray) where T | ||
mappedarray(c->convert(T,c), img) | ||
end | ||
|
||
function showconstruction(io::IO, op::ConvertEltype) | ||
print(io, typeof(op).name.name, '(') | ||
ImageCore.showcoloranttype(io, op.eltype) | ||
print(io, ')') | ||
end | ||
|
||
function Base.show(io::IO, op::ConvertEltype) | ||
if get(io, :compact, false) | ||
print(io, "Convert eltype to ") | ||
ImageCore.showcoloranttype(io, op.eltype) | ||
else | ||
print(io, "Augmentor.") | ||
showconstruction(io, op) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
@testset "ConvertEltype" begin | ||
@test (ConvertEltype <: Augmentor.AffineOperation) == false | ||
@test (ConvertEltype <: Augmentor.ImageOperation) == false | ||
@test (ConvertEltype <: Augmentor.Operation) == true | ||
|
||
@testset "constructor" begin | ||
@test_throws MethodError ConvertEltype() | ||
@test typeof(@inferred(ConvertEltype(Float64))) <: ConvertEltype <: Augmentor.Operation | ||
@test typeof(@inferred(ConvertEltype(RGB))) <: ConvertEltype <: Augmentor.Operation | ||
@test typeof(@inferred(ConvertEltype(RGB{N0f8}))) <: ConvertEltype <: Augmentor.Operation | ||
@test str_show(ConvertEltype(Float64)) == "Augmentor.ConvertEltype(Float64)" | ||
@test str_show(ConvertEltype(RGB)) == "Augmentor.ConvertEltype(RGB{Any})" | ||
@test str_show(ConvertEltype(Gray{N0f8})) == "Augmentor.ConvertEltype(Gray{N0f8})" | ||
@test str_showconst(ConvertEltype(Float64)) == "ConvertEltype(Float64)" | ||
@test str_showconst(ConvertEltype(RGB{N0f8})) == "ConvertEltype(RGB{N0f8})" | ||
@test str_showcompact(ConvertEltype(Float64)) == "Convert eltype to Float64" | ||
@test str_showcompact(ConvertEltype(Gray)) == "Convert eltype to Gray{Any}" | ||
end | ||
@testset "eager" begin | ||
@test Augmentor.supports_eager(ConvertEltype) === true | ||
@test Augmentor.supports_eager(ConvertEltype{Float64}) === true | ||
let img = @inferred(Augmentor.applyeager(ConvertEltype(Gray), rgb_rect)) | ||
@test typeof(img) == Array{Gray{N0f8},2} | ||
@test img == convert.(Gray, rgb_rect) | ||
end | ||
let img = @inferred(Augmentor.applyeager(ConvertEltype(Gray{Float32}), rgb_rect)) | ||
@test typeof(img) == Array{Gray{Float32},2} | ||
@test img == convert.(Gray{Float32}, rgb_rect) | ||
end | ||
let img = @inferred(Augmentor.applyeager(ConvertEltype(Float32), checkers)) | ||
@test typeof(img) == Array{Float32,2} | ||
@test img == convert(Array{Float32}, checkers) | ||
end | ||
let img = @inferred(Augmentor.applyeager(ConvertEltype(RGB), checkers)) | ||
@test typeof(img) == Array{RGB{N0f8},2} | ||
@test img == convert.(RGB, checkers) | ||
end | ||
end | ||
@testset "affine" begin | ||
@test Augmentor.supports_affine(ConvertEltype) === false | ||
end | ||
@testset "affineview" begin | ||
@test Augmentor.supports_affineview(ConvertEltype) === false | ||
end | ||
@testset "lazy" begin | ||
@test Augmentor.supports_lazy(ConvertEltype) === true | ||
@test Augmentor.supports_lazy(ConvertEltype{Float64}) === true | ||
@test @inferred(Augmentor.supports_lazy(typeof(ConvertEltype(Gray)))) === true | ||
let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray), rgb_rect)) | ||
@test typeof(img) <: ReadonlyMappedArray{Gray{N0f8},2} | ||
@test img == convert.(Gray, rgb_rect) | ||
end | ||
let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), rgb_rect)) | ||
@test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2} | ||
@test img == convert.(Gray{Float32}, rgb_rect) | ||
end | ||
let img = @inferred(Augmentor.applylazy(ConvertEltype(Float32), checkers)) | ||
@test typeof(img) <: ReadonlyMappedArray{Float32,2} | ||
@test img == convert(Array{Float32}, checkers) | ||
end | ||
let img = @inferred(Augmentor.applylazy(ConvertEltype(RGB), checkers)) | ||
@test typeof(img) <: ReadonlyMappedArray{RGB{N0f8},2} | ||
@test img == convert.(RGB, checkers) | ||
end | ||
end | ||
@testset "view" begin | ||
@test Augmentor.supports_view(ConvertEltype) === false | ||
end | ||
@testset "stepview" begin | ||
@test Augmentor.supports_stepview(ConvertEltype) === false | ||
end | ||
@testset "permute" begin | ||
@test Augmentor.supports_permute(ConvertEltype) === false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters