Module arraymancer_vision

Types

PadMode = enum
  PadConstant = 0, PadNearest = 1
ScaleMode = enum
  ScaleNearest = 0, ScaleBilinear = 1

Procs

proc channels[T](img: Tensor[T]): int {.
inline
.}
Return number of channels of the image
proc height[T](img: Tensor[T]): int {.
inline
.}
Return height of the image
proc width[T](img: Tensor[T]): int {.
inline
.}
Return width of the image
proc hwc2chw[T](img: Tensor[T]): Tensor[T]
Convert image from HxWxC convetion to the CxHxW convention, where C,W,H stands for channels, width, height, note that this library only works with CxHxW images for optimization and internal usage reasons using CxHxW for images is also a common approach in deep learning
proc chw2hwc[T](img: Tensor[T]): Tensor[T]
Convert image from CxHxW convetion to the HxWxC convention, where C,W,H stands for channels, width, height, note that this library only works with CxHxW images for optimization and internal usage reasons using CxHxW for images is also a common approach in deep learning
proc pixels(img: Tensor[uint8]): seq[uint8] {.
raises: [], tags: []
.}
proc load(filename: string; desired_channels: int = 0): Tensor[uint8] {.
raises: [IOError], tags: []
.}

Load image from file, with the desired number of channels, into a contiguous CxHxW Tensor[uint8]. Desired channels defaults to 0 meaning that it will auto detect the number of channels, the returned image tensor will be in the CxHxW format even for images with a single channel.

Supports PNG, JPG, BMP, TGA and HDR formats

On error an IOError exception will be thrown

proc loadFromMemory(contents: string; desired_channels: int = 0): Tensor[uint8] {.
raises: [STBIException], tags: []
.}
Like load but loads from memory, the contents must be a buffer for a supported image format
proc loadFromDir(dir: string; desired_channels: int = 0): seq[Tensor[uint8]] {.
raises: [IOError, IOError, OSError], tags: [ReadDirEffect]
.}
Load batch of images from a directory into a seq of tensors, the load is non recursive, throws an IOError exception on error.
proc save(img: Tensor[uint8]; filename: string; jpeg_quality: int = 100) {.
raises: [IOError], tags: []
.}
Save an image to a file, supports PNG, BMP, TGA and JPG. Argument jpeg_quality can be passed to inform the saving quality from a range 0 to 100, defaults to 100
proc toPNG(img: Tensor[uint8]): string {.
raises: [Exception, IOError], tags: [WriteIOEffect]
.}
Convert an image to PNG into a string of bytes
proc toBMP(img: Tensor[uint8]): string {.
raises: [Exception, IOError], tags: [WriteIOEffect]
.}
Convert an image to BMP into a string of bytes
proc toTGA(img: Tensor[uint8]): string {.
raises: [Exception, IOError], tags: [WriteIOEffect]
.}
Convert an image to TGA into a string of bytes
proc toJPG(img: Tensor[uint8]; quality: int = 100): string {.
raises: [Exception, IOError], tags: [WriteIOEffect]
.}
Convert an image to JPG into a string of bytes. Argument jpeg_quality can be passed to inform the saving quality from a range 0 to 100, defaults to 100
proc hflip(img: Tensor[uint8]): Tensor[uint8] {.
inline, raises: [IndexError], tags: []
.}
Horizontal flips an image
proc vflip(img: Tensor[uint8]): Tensor[uint8] {.
inline, raises: [IndexError], tags: []
.}
Vertical flips an image
proc vhflip(img: Tensor[uint8]): Tensor[uint8] {.
inline, raises: [IndexError], tags: []
.}
Flip vertically and horizontally an image
proc crop(img: Tensor[uint8]; x, y, width, height: int): Tensor[uint8] {.
inline, raises: [IndexError], tags: []
.}
Crop an image
proc center_crop[T](img: Tensor[T]; width, height: int): Tensor[T] {.
inline
.}
Crop an image to center
proc random_crop[T](img: Tensor[T]; width, height: int): Tensor[T] {.
inline
.}
Random crop an image
proc rot90[T](img: Tensor[T]; k: int): Tensor[T]
Rotate an image 90 degrees clockwise k times
proc quantize_bytes[T: SomeReal](img: Tensor[T]; U: typedesc): Tensor[U]
Quantize image bytes, from type T to U, useful for converting images from floats to ints
proc quantize_bytes[T: SomeInteger](img: Tensor[T]; U: typedesc): Tensor[U]
Quantize image bytes, from type T to U, useful for converting images from floats to ints
proc im2col[T](input: Tensor[T]; ksize: int; pad: int = 0; mode: PadMode; pad_constant: int): Tensor[
    T]
Convert blocks of an image into columns, useful for preprocessing an image before convolutions, pad mode.
proc correlate2d[T, U](input: Tensor[T]; weights: Tensor[U]; pad: int = 0;
                     mode: PadMode = PadConstant; cval: U = 0): Tensor[int]
Correlate an image with the given kernel weights, this is a convolution without flipping the kernel
proc convolve2d(input: Tensor[uint8]; weights: Tensor[int]; pad: int;
               mode: PadMode = PadConstant; cval: int = 0): Tensor[int] {.
raises: [IndexError, ValueError, Exception], tags: [RootEffect]
.}
Convolve an image with the given kernel weights, like correlate but it flips the kernel before.
proc tile_collection(imgs: Tensor[uint8]; max_width: int = 0): Tensor[uint8] {.
raises: [IndexError, Exception, ValueError], tags: [RootEffect]
.}
Combine multiple images into one big tiled image and returns it. The new generated image width will be at maximum the given max width if supplied, otherwise will be calculated to create a square image.
proc kernel[T, U](img: Tensor[T]; kernel: Tensor[U]; scale: U = 1; offset: U = 0): Tensor[T]

Applies a kernel matrix to an image and divides the outputs by scale factor and them sun offset. For more information see https://en.wikipedia.org/wiki/Kernel_(image_processing)

Implementation details: This functions does not flip the kernel, so it does image correlation instead of convolution. The padding borders of the image is replaced with the nearest neighbourhood border.

proc filter_blur[T](img: Tensor[T]): Tensor[T]
Blur an image using a predefied kernel
proc filter_contour[T](img: Tensor[T]): Tensor[T]
Contour an image using a predefied kernel
proc filter_detail[T](img: Tensor[T]): Tensor[T]
Detail an image using a predefied kernel
proc filter_edge_enhance[T](img: Tensor[T]): Tensor[T]
Enhance edges of an image using a predefied kernel
proc filter_edge_enhance_more[T](img: Tensor[T]): Tensor[T]
Enhance edges of an image using a predefied kernel
proc filter_emboss[T](img: Tensor[T]): Tensor[T]
Enhance an image using a predefied kernel
proc filter_sharpen[T](img: Tensor[T]): Tensor[T]
Sharpen an image using a predefied kernel
proc filter_smooth[T](img: Tensor[T]): Tensor[T]
Smooth an image using a predefied kernel
proc filter_find_edges[T](img: Tensor[T]): Tensor[T]
Find edges of image using a predefied kernel
proc filter_smooth_more[T](img: Tensor[T]): Tensor[T]
Smooth more an image using a predefied kernel
proc scale[T](src: Tensor[T]; width, height: int; mode: ScaleMode = ScaleNearest): Tensor[
    T]
Scale an image to a new size, suppored modes are nearest, and bilinear, defaults to nearest.
proc newVisdomClient(host: string = "localhost"; port: int = 8097): VisdomClient {.
raises: [], tags: []
.}
Prepare a visdom client for visualization
proc image(vis: VisdomClient; img: Tensor[uint8]; window: string = "";
          caption: string = ""; title: string = "") {.
raises: [Exception, IOError, ValueError, HttpRequestError, SslError, OSError, TimeoutError, ProtocolError, KeyError, OverflowError], tags: [WriteIOEffect, ReadIOEffect, TimeEffect]
.}
Show image into visdom with the given title and specified window