nimxla/image

Source   Edit  

The image module provides some simple functions for image augmentation.

Image arrays are stored as a Tensor[uint8] with dimensions [N,H,W,C] where N=number of images, HxW is image size and C is number of channels (1 for greyscale or 3 for RGB).

Types

Direction = enum
  Horizontal, Vertical
Source   Edit  
ImageOp = object
  p*: float
  case
  of Flip:
      direction*: Direction  ## Flip image around horizontal or vertical axis
    
  of Wrap:
      max_x*, max_y*: int    ## Randomly scroll image in x and y direction by up to max_x, max_y wrapping pixels. 
    
  of Affine:
      rotate*: float         ## Affine image transform with rotation, scaling and translation relative to center
      scalexy*: (float, float)
      transx*: float
      transy*: float

  of Elastic:
      kernelSize*: int       ## Elastic image distortion
      stddev*: float
      scale*: float
    
  
Apply operation to image with probability p Source   Edit  
ImageOpKind = enum
  Affine, Flip, Wrap, Elastic
Source   Edit  
ImageRequest = object
  height*: int
  width*: int
  data*: ptr uint8
Source   Edit  
TransContext = object
  cin*: ptr Channel[ImageRequest]
  cout*: ptr Channel[bool]
  
Source   Edit  
Transformer = object
  ops*: seq[ImageOp]
  ctx*: TransContext
  
Source   Edit  

Procs

proc `$`(op: ImageOp): string {....raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
proc `$`(t: Transformer): string {....raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
proc initTransformer(channels: static int; rng: var Rand;
                     ops: openArray[ImageOp]; threads = 0): Transformer
Setup a new image transform pipeline which will read apply a series of image ops. Will use threads parallel threads - sets this to no. of processor cores if <= 0 Source   Edit  
proc randomAffine(rotate = 0.0; scale = (1.0, 1.0); transx = 0.0; transy = 0.0;
                  p = 0.5): ImageOp {....raises: [], tags: [], forbids: [].}
Random affine transformation with probability p, where image is rotated by up to -rotate..+rotate degrees, scaled by factor between scale[0]..scale[1] and translated by up to transx and transy pixels along x and y. These transforms are all relative to the image center. Source   Edit  
proc randomElastic(kernelSize = 9; stddev = 4.0; scale = 0.5; p = 0.5): ImageOp {.
    ...raises: [], tags: [], forbids: [].}
Random elastic transform where kernelSize is the size of the gaussian kernel, stddev is the x and y standard deviation for the kernel, scale is the scaling factor controlling the intensity of the deformation and p is the probabilty of applying the transform. Source   Edit  
proc randomFlip(direction: Direction; p = 0.5): ImageOp {....raises: [], tags: [],
    forbids: [].}
Randomly flip images either horizontally or vertically with probability p. Source   Edit  
proc randomWrap(max_x, max_y: int; p = 0.5): ImageOp {....raises: [], tags: [],
    forbids: [].}
Translate and wrap around images such that the point which was at (0, 0) is at (dx, dy) where dx is randomly sampled from -max_x..max_x and dy from -max_y..max_y. p is probability of applying the transform to each image. Source   Edit  
proc shutdown(t: Transformer) {....raises: [Exception],
                                tags: [RootEffect, TimeEffect], forbids: [].}
Shutdown worker threads Source   Edit  
proc transform(t: Transformer; arr: var Tensor[uint8]) {....raises: [ValueError],
    tags: [], forbids: [].}
Apply series of image transforms to an array of images in [N,H,W,C] format. The transform will be applied in parallel using multiple threads - the proc will return once all are completed and the data is updated. Source   Edit