webdnn/image
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.
createImageData
createImageData(array: Uint8ClampedArray , width: number , height: number ): ImageData
[image/image_array.ts:378]
| Name | Type | Default | Description |
|---|---|---|---|
array
|
Uint8ClampedArray
|
||
width
|
number
|
||
height
|
number
|
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
imageis an instance ofstring, it will be regarded as image url, and this method fetches that url.If
imageis an instance ofHTMLInputElement, it will be regarded as file input, and this method loads the selected image file.Otherwise,
imagewill 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.typeis the type of packed typed array. As default, Float32Array is used.options.coloris the color format of packed typed array. As default,RGBis used.options.orderis the data order of packed typed array. As default,HWCis used.options.biasis the bias value. If specified, this method subtracts this value from original pixel value.options.scaleis the scale value. If specified, original pixel values are divided by this value.options.scaleandoptions.biasis used for converting pixel valuexand packed valueyas follows:y = (x - bias) / scalex = 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 valueycan be calculated from pixel valuexas 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] });
| Name | Type | Default | Description |
|---|---|---|---|
image
|
ImageSource
|
please see above descriptions |
|
options
|
|
{}
|
please see above descriptions. |
Promise<Float32Array | Int32Array >
Created typed array
loadImageByUrl
loadImageByUrl(url: string ): Promise<HTMLImageElement >
[image/image_source.ts:13]
Load image of specified url
| Name | Type | Default | Description |
|---|---|---|---|
url
|
string
|
the image url |
Promise<HTMLImageElement >
image element
loadImageFromFileInput
loadImageFromFileInput(input: HTMLInputElement ): Promise<HTMLImageElement >
[image/image_source.ts:31]
Load image file selected in <input type="file"> element.
| Name | Type | Default | Description |
|---|---|---|---|
input
|
HTMLInputElement
|
the |
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. });
| Name | Type | Default | Description |
|---|---|---|---|
array
|
|
array which contains image data |
|
imageW
|
number
|
width of image |
|
imageH
|
number
|
height of image. The length of |
|
canvas
|
HTMLCanvasElement
|
destination canvas |
|
options
|
|
{}
|
please see above descriptions and descriptions in getImageArray().
|
void
-
Enumerations -
Interfaces -
Type aliases -
Functions
Module
WebDNN.Imageprovides basic image processing operations like follows.