module

webdnn/image

Module WebDNN.Image provides basic image processing operations like follows.

  • Load image by various way (File picker dialog, url string, canvas, video, etc.)
  • Pack image data into TypedArray
  • Crop and resize.
  • Show result on canvas element
Type aliases
# Drawable: HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | ImageData [image/image_array.ts:60]

Types which are drawable at HTMLCanvasElement

# ImageSource: string | HTMLInputElement | Drawable [image/image_array.ts:65]

All type of image source which WebDNN.Image can be handled. For string, only the url of image resource is valid.

Functions
#

createImageData

createImageData(array: Uint8ClampedArray , width: number , height: number ): ImageData [image/image_array.ts:378]
Parameters
Name Type Default Description
array Uint8ClampedArray
width number
height number
Returns
ImageData
#

getImageArray

getImageArray(image: ImageSource, options?: SourceRect & DestinationRect & ImageArrayOption): Promise<Float32Array | Int32Array > [image/image_array.ts:356]

Create typed array by packing image data from image source with specified options.

First, this method loads specified image resource. The behavior of this method depends on the image argument.

  • If image is an instance of string, it will be regarded as image url, and this method fetches that url.

  • If image is an instance of HTMLInputElement, it will be regarded as file input, and this method loads the selected image file.

  • Otherwise, image will be regarded as drawable object.

Then, loaded images are packed into typed array based on options argument.

  • The image is cropped based on {srcX, srcY, srcW, srcH}. As default, entire image is used.

  • The image is resized and translated into {dstX, dstY, dstW, dstH}. As default, no resize and translation is performed.

  • options.type is the type of packed typed array. As default, Float32Array is used.

  • options.color is the color format of packed typed array. As default, RGB is used.

  • options.order is the data order of packed typed array. As default, HWC is used.

  • options.bias is the bias value. If specified, this method subtracts this value from original pixel value.

  • options.scale is the scale value. If specified, original pixel values are divided by this value. options.scale and options.bias is used for converting pixel value x and packed value y as follows:

    • y = (x - bias) / scale
    • x = y * scale + bias
    • Note that color order is always RGB, not BGR.

Examples

  • Load image of specified url

    let image = await WebDNN.Image.load('./cat.png');
    
  • Load image selected in file input and resize it into 224 x 224

    let input = document.querySelector('input[type=file]');
    let image = await WebDNN.Image.load(input, { dstW: 224, dstH: 224 });
    
  • Load image data from canvas, normalize it into range [-1, 1). In this case, normalized value y can be calculated from pixel value x as follows: y = (x - 128) / 128.

    let canvas = document.getElementsByTagName('canvas')[0];
    let image = await WebDNN.Image.load(canvas, { bias: [128, 128, 128], scale: [128, 128, 128] });
    
Parameters
Name Type Default Description
image ImageSource

please see above descriptions

options SourceRect & DestinationRect & ImageArrayOption {}

please see above descriptions.

Returns
Promise<Float32Array | Int32Array >

Created typed array

#

loadImageByUrl

loadImageByUrl(url: string ): Promise<HTMLImageElement > [image/image_source.ts:13]

Load image of specified url

Parameters
Name Type Default Description
url string

the image url

Returns
Promise<HTMLImageElement >

image element

#

loadImageFromFileInput

loadImageFromFileInput(input: HTMLInputElement ): Promise<HTMLImageElement > [image/image_source.ts:31]

Load image file selected in <input type="file"> element.

Parameters
Name Type Default Description
input HTMLInputElement

the <input type="file"> element

Returns
Promise<HTMLImageElement >

image element

#

setImageArrayToCanvas

setImageArrayToCanvas(array: Float32Array | Int32Array , imageW: number , imageH: number , canvas: HTMLCanvasElement , options?: SourceRect & DestinationRect & ImageArrayOption): void [image/image_array.ts:427]

Set image array data into canvas.

Examples

  • Show DNN model's result

    let runner = await WebDNN.load('./model');
    let output = runner.outputs[0];
    
    await runner.run();
    
    WebDNN.Image.setImageArrayToCanvas(output.toActual(), 256, 256, document.getElementById('canvas'))
    
  • Generally image generation model's result contains noise pixel at their edge because of convolution's padding. In follow example, these noise are cut off.

    WebDNN.Image.setImageArrayToCanvas(output, 256, 256, canvas, {
       srcX: 16, srcY: 16, srcH: 256-16*2, srcW: 256-16*2, // Discard both ends 16px
       dstW: 256, dstH: 256  // Resize cropped image into original output size.
    });
    
Parameters
Name Type Default Description
array Float32Array | Int32Array

array which contains image data

imageW number

width of image

imageH number

height of image. The length of array must be imageW * imageH * (# of channels)

canvas HTMLCanvasElement

destination canvas

options SourceRect & DestinationRect & ImageArrayOption {}

please see above descriptions and descriptions in getImageArray(). srcW and srcH is ignored (overwritten by imageW and imageH).

Returns
void