Crop

Source code notebook

Subset image using Crop and CropNative

using Augmentor
using ImageShow, ImageCore
using OffsetArrays

img_in = testpattern(RGB, ratio=0.5)
img_out = augment(img_in, Crop(20:75,25:120))

mosaicview(img_in, img_out; fillvalue=colorant"white", nrow=1)

If the input image is plain arrays without offset indices, then Crop and CropNative is equivalent.

augment(img_in, Crop(20:75,25:120)) == augment(img_in, CropNative(20:75,25:120))
true

Whether you should use Crop or CropNative depends on if you want to take the index offset of the input image into consideration.

imgo_in = OffsetArray(img_in, -50, -50)
imgo_out = augment(imgo_in, Crop(20:75,25:120))
imgo_out_native = augment(imgo_in, CropNative(20:75,25:120))

(
    imgo_in[(first.(axes(imgo_in)) .+ (20, 25))...] == imgo_out[1, 1],
    imgo_in[20, 25] == imgo_out_native[1, 1]
)
(true, true)

A typical scenario that you may want to use CropNative is when you have affine operations, e.g., Rotate and ShearX.

mosaicview(
    augment(img_in, Rotate(30) |> Crop(axes(img_in))),
    augment(img_in, Rotate(30) |> CropNative(axes(img_in))),

    augment(img_in, ShearX(10) |> Crop(axes(img_in))),
    augment(img_in, ShearX(10) |> CropNative(axes(img_in)));

    fillvalue=colorant"white", rowmajor=true, nrow=2, npad=10
)

Reference

Augmentor.CropType
Crop <: Augmentor.ImageOperation

Description

Crops out the area denoted by the specified pixel ranges.

For example the operation Crop(5:100, 2:10) would denote a crop for the rectangle that starts at x=2 and y=5 in the top left corner and ends at x=10 and y=100 in the bottom right corner. As we can see the y-axis is specified first, because that is how the image is stored in an array. Thus the order of the provided axes ranges needs to reflect the order of the array dimensions.

Usage

Crop(indices)

Crop(indices...)

Arguments

  • indices : NTuple or Vararg of UnitRange that denote the cropping range for each array dimension. This is very similar to how the axes for view are specified.

See also

CropNative, CropSize, CropRatio, augment

Examples

julia> using Augmentor

julia> img = testpattern()
300×400 Array{RGBA{N0f8},2}:
[...]

julia> augment(img, Crop(1:30, 361:400)) # crop upper right corner
30×40 Array{RGBA{N0f8},2}:
[...]
source
Augmentor.CropNativeType
CropNative <: Augmentor.ImageOperation

Description

Crops out the area denoted by the specified pixel ranges.

For example the operation CropNative(5:100, 2:10) would denote a crop for the rectangle that starts at x=2 and y=5 in the top left corner of native space and ends at x=10 and y=100 in the bottom right corner of native space.

In contrast to Crop, the position x=1 y=1 is not necessarily located at the top left of the current image, but instead depends on the cumulative effect of the previous transformations. The reason for this is because affine transformations are usually performed around the center of the image, which is reflected in "native space". This is useful for combining transformations such as Rotate or ShearX with a crop around the center area.

Usage

CropNative(indices)

CropNative(indices...)

Arguments

  • indices : NTuple or Vararg of UnitRange that denote the cropping range for each array dimension. This is very similar to how the axes for view are specified.

See also

Crop, CropSize, CropRatio, augment

Examples

using Augmentor
img = testpattern()

# cropped at top left corner
augment(img, Rotate(45) |> Crop(1:300, 1:400))

# cropped around center of rotated image
augment(img, Rotate(45) |> CropNative(1:300, 1:400))
source

This page was generated using DemoCards.jl and Literate.jl.