Skip to content

Commit

Permalink
Merge pull request #9 from Evizero/eltype
Browse files Browse the repository at this point in the history
add ConvertEltype operation
  • Loading branch information
Evizero authored Jul 3, 2017
2 parents 63a70a5 + aac513e commit 46b4f73
Show file tree
Hide file tree
Showing 22 changed files with 237 additions and 28 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# v0.2.0

New operations:

- `ConvertEltype`: Convert the array elements to the given type

Other changes:

- `Either` can now lazily combine affine operations with operations
such as `Crop`, `Zoom`, and `Resize`. This is because a new kind
of support was introduced called `Augmentor.supports_affineview`,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ look at the corresponding section of the
| | `CropSize` | Crop area around the center with specified size.
| | `CropRatio` | Crop to specified aspect ratio.
| | `RCropRatio` | Crop random window of specified aspect ratio.
| *Conversion:* | `ConvertEltype` | Convert the array elements to the given type.
| *Layout:* | `SplitChannels` | Separate the color channels into a dedicated array dimension.
| | `CombineChannels` | Collapse the first dimension into a specific colorant.
| | `PermuteDims` | Reorganize the array dimensions into a specific order.
Expand Down
1 change: 1 addition & 0 deletions REQUIRE
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
Expand Down
38 changes: 38 additions & 0 deletions docs/usersguide/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ functionality.
| Cropping | :class:`Crop` :class:`CropNative` :class:`CropSize` :class:`CropRatio` |
| | :class:`RCropRatio` |
+-----------------------+----------------------------------------------------------------------------+
| Conversion | :class:`ConvertEltype` |
+-----------------------+----------------------------------------------------------------------------+
| Information Layout | :class:`SplitChannels` :class:`CombineChannels` :class:`PermuteDims` |
| | :class:`Reshape` |
+-----------------------+----------------------------------------------------------------------------+
Expand Down Expand Up @@ -554,6 +556,42 @@ Resizing
| .. image:: https://raw.githubusercontent.com/JuliaML/FileStorage/master/Augmentor/testpattern_small.png | .. image:: https://raw.githubusercontent.com/JuliaML/FileStorage/master/Augmentor/operations/Resize.png |
+---------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+

Conversion
--------------------

.. class:: ConvertEltype

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
:class:`SplitChannels` or :class:`CombineChannels` for those
purposes.

.. code-block:: jlcon
julia> op = ConvertEltype(Gray)
Convert eltype to Gray
julia> img = testpattern()
300×400 Array{RGBA{N0f8},2}:
[...]
julia> augment(img, op)
300×400 Array{Gray{N0f8},2}:
[...]
+----------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------+
| Input | Output for ``ConvertEltype(GrayA)`` |
+================================================================================================================+================================================================================================================+
| .. image:: https://raw.githubusercontent.com/JuliaML/FileStorage/master/Augmentor/testpattern_small.png | .. image:: https://raw.githubusercontent.com/JuliaML/FileStorage/master/Augmentor/operations/ConvertEltype.png |
+----------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------+

Information Layout
--------------------

Expand Down
7 changes: 7 additions & 0 deletions src/Augmentor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Augmentor

using ColorTypes
using ColorTypes: AbstractGray
using MappedArrays
using ImageCore
using ImageTransformations
using ImageFiltering
Expand All @@ -19,11 +20,16 @@ using Base.PermutedDimsArrays: PermutedDimsArray

export

Gray,
RGB,

SplitChannels,
CombineChannels,
PermuteDims,
Reshape,

ConvertEltype,

Rotate90,
Rotate180,
Rotate270,
Expand Down Expand Up @@ -61,6 +67,7 @@ include("types.jl")
include("operation.jl")

include("operations/channels.jl")
include("operations/convert.jl")

include("operations/noop.jl")
include("operations/cache.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/distortedview.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
immutable DistortedView{T,P<:AbstractMatrix,E<:AbstractExtrapolation,G,D} <: AbstractArray{T,2}
struct DistortedView{T,P<:AbstractMatrix,E<:AbstractExtrapolation,G,D} <: AbstractArray{T,2}
parent::P
etp::E
grid::G
Expand Down
4 changes: 2 additions & 2 deletions src/operations/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ see also
[`augment`](@ref)
"""
immutable CacheImage <: ImageOperation end
struct CacheImage <: ImageOperation end

applyeager(op::CacheImage, img::Array) = img
applyeager(op::CacheImage, img::OffsetArray) = img
Expand Down Expand Up @@ -79,7 +79,7 @@ end
see [`CacheImage`](@ref)
"""
immutable CacheImageInto{T<:AbstractArray} <: ImageOperation
struct CacheImageInto{T<:AbstractArray} <: ImageOperation
buffer::T
end
CacheImage(buffer::AbstractArray) = CacheImageInto(buffer)
Expand Down
8 changes: 4 additions & 4 deletions src/operations/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ see also
[`PermuteDims`](@ref), [`CombineChannels`](@ref), [`augment`](@ref)
"""
immutable SplitChannels <: Operation end
struct SplitChannels <: Operation end

@inline supports_eager(::Type{SplitChannels}) = false
@inline supports_lazy(::Type{SplitChannels}) = true
Expand Down Expand Up @@ -126,7 +126,7 @@ see also
[`SplitChannels`](@ref), [`PermuteDims`](@ref), [`augment`](@ref)
"""
immutable CombineChannels{T<:Colorant} <: Operation
struct CombineChannels{T<:Colorant} <: Operation
colortype::Type{T}
end

Expand Down Expand Up @@ -221,7 +221,7 @@ see also
[`SplitChannels`](@ref), [`CombineChannels`](@ref), [`augment`](@ref)
"""
immutable PermuteDims{N,perm,iperm} <: Operation end
struct PermuteDims{N,perm,iperm} <: Operation end
PermuteDims() = throw(MethodError(PermuteDims, ()))
PermuteDims(perm::Tuple{}) = throw(MethodError(PermuteDims, (perm,)))
PermuteDims(perm::NTuple{N,Int}) where {N} = PermuteDims{N,perm,invperm(perm)}()
Expand Down Expand Up @@ -297,7 +297,7 @@ see also
[`CombineChannels`](@ref), [`augment`](@ref)
"""
immutable Reshape{N} <: Operation
struct Reshape{N} <: Operation
dims::NTuple{N,Int}
end
Reshape() = throw(MethodError(Reshape, ()))
Expand Down
80 changes: 80 additions & 0 deletions src/operations/convert.jl
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
10 changes: 5 additions & 5 deletions src/operations/crop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ see also
[`CropNative`](@ref), [`CropSize`](@ref), [`CropRatio`](@ref), [`augment`](@ref)
"""
immutable Crop{N,I<:Tuple} <: ImageOperation
struct Crop{N,I<:Tuple} <: ImageOperation
indexes::I

function Crop{N}(indexes::NTuple{N,UnitRange}) where N
Expand Down Expand Up @@ -143,7 +143,7 @@ see also
[`Crop`](@ref), [`CropSize`](@ref), [`CropRatio`](@ref), [`augment`](@ref)
"""
immutable CropNative{N,I<:Tuple} <: ImageOperation
struct CropNative{N,I<:Tuple} <: ImageOperation
indexes::I

function CropNative{N}(indexes::NTuple{N,UnitRange}) where N
Expand Down Expand Up @@ -229,7 +229,7 @@ see also
[`CropRatio`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable CropSize{N} <: ImageOperation
struct CropSize{N} <: ImageOperation
size::NTuple{N,Int}

function CropSize{N}(size::NTuple{N,Int}) where N
Expand Down Expand Up @@ -329,7 +329,7 @@ see also
[`RCropRatio`](@ref), [`CropSize`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable CropRatio <: ImageOperation
struct CropRatio <: ImageOperation
ratio::Float64

function CropRatio(ratio::Real)
Expand Down Expand Up @@ -451,7 +451,7 @@ see also
[`CropRatio`](@ref), [`CropSize`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable RCropRatio <: ImageOperation
struct RCropRatio <: ImageOperation
ratio::Float64

function RCropRatio(ratio::Real)
Expand Down
2 changes: 1 addition & 1 deletion src/operations/distortion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ see also
[`RandomDistortion`](@ref), [`augment`](@ref)
""" ->
immutable ElasticDistortion <: ImageOperation
struct ElasticDistortion <: ImageOperation
gridheight::Int
gridwidth::Int
scale::Float64
Expand Down
2 changes: 1 addition & 1 deletion src/operations/either.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ see also
[`augment`](@ref)
"""
immutable Either{N,T<:Tuple} <: ImageOperation
struct Either{N,T<:Tuple} <: ImageOperation
operations::T
chances::SVector{N,Float64}
cum_chances::SVector{N,Float64}
Expand Down
4 changes: 2 additions & 2 deletions src/operations/flip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ see also
[`FlipY`](@ref), [`Either`](@ref), [`augment`](@ref)
"""
immutable FlipX <: AffineOperation end
struct FlipX <: AffineOperation end
FlipX(p::Number) = Either(FlipX(), p)

@inline supports_stepview(::Type{FlipX}) = true
Expand Down Expand Up @@ -133,7 +133,7 @@ see also
[`FlipX`](@ref), [`Either`](@ref), [`augment`](@ref)
"""
immutable FlipY <: AffineOperation end
struct FlipY <: AffineOperation end
FlipY(p::Number) = Either(FlipY(), p)

@inline supports_stepview(::Type{FlipY}) = true
Expand Down
2 changes: 1 addition & 1 deletion src/operations/noop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ image but instead passes it along unchanged (without copying).
Usually used in combination with [`Either`](@ref) to denote a
"branch" that does not perform any computation.
"""
immutable NoOp <: AffineOperation end
struct NoOp <: AffineOperation end

@inline supports_eager(::Type{NoOp}) = false
@inline supports_stepview(::Type{NoOp}) = true
Expand Down
2 changes: 1 addition & 1 deletion src/operations/resize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ see also
[`CropSize`](@ref), [`augment`](@ref)
"""
immutable Resize{N} <: ImageOperation
struct Resize{N} <: ImageOperation
size::NTuple{N,Int}

function Resize{N}(size::NTuple{N,Int}) where N
Expand Down
8 changes: 4 additions & 4 deletions src/operations/rotation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ see also
[`Rotate180`](@ref), [`Rotate270`](@ref), [`Rotate`](@ref),
[`Either`](@ref), [`augment`](@ref)
"""
immutable Rotate90 <: AffineOperation end
struct Rotate90 <: AffineOperation end
Rotate90(p::Number) = Either(Rotate90(), p)

@inline supports_permute(::Type{Rotate90}) = true
Expand Down Expand Up @@ -137,7 +137,7 @@ see also
[`Rotate90`](@ref), [`Rotate270`](@ref), [`Rotate`](@ref),
[`Either`](@ref), [`augment`](@ref)
"""
immutable Rotate180 <: AffineOperation end
struct Rotate180 <: AffineOperation end
Rotate180(p::Number) = Either(Rotate180(), p)

@inline supports_stepview(::Type{Rotate180}) = true
Expand Down Expand Up @@ -208,7 +208,7 @@ see also
[`Rotate90`](@ref), [`Rotate180`](@ref), [`Rotate`](@ref),
[`Either`](@ref), [`augment`](@ref)
"""
immutable Rotate270 <: AffineOperation end
struct Rotate270 <: AffineOperation end
Rotate270(p::Number) = Either(Rotate270(), p)

@inline supports_permute(::Type{Rotate270}) = true
Expand Down Expand Up @@ -307,7 +307,7 @@ see also
[`Rotate90`](@ref), [`Rotate180`](@ref), [`Rotate270`](@ref),
[`CropNative`](@ref), [`augment`](@ref)
"""
immutable Rotate{T<:AbstractVector} <: AffineOperation
struct Rotate{T<:AbstractVector} <: AffineOperation
degree::T

function Rotate{T}(degree::T) where {T<:AbstractVector{S} where S<:Real}
Expand Down
2 changes: 1 addition & 1 deletion src/operations/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ see also
[`Zoom`](@ref), [`Resize`](@ref), [`augment`](@ref)
"""
immutable Scale{N,T<:AbstractVector} <: AffineOperation
struct Scale{N,T<:AbstractVector} <: AffineOperation
factors::NTuple{N,T}

function Scale{N}(factors::NTuple{N,T}) where {N,T<:AbstractVector}
Expand Down
Loading

0 comments on commit 46b4f73

Please sign in to comment.