From cb8a3d77f9ec91b9829578da9b8f7bf4e072d5b2 Mon Sep 17 00:00:00 2001 From: Leon Chen Date: Tue, 20 Sep 2016 12:39:51 -0400 Subject: [PATCH] add 1d-tensor to image conversion util function --- demos/src/utils/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/demos/src/utils/index.js b/demos/src/utils/index.js index 7804213..9c1e417 100644 --- a/demos/src/utils/index.js +++ b/demos/src/utils/index.js @@ -108,6 +108,19 @@ export function tensorMinMax (tensor) { return { min, max } } +/** + * Takes in a ndarray of shape [x] + * and creates image data + */ +export function image1Dtensor (tensor) { + const { min, max } = tensorMinMax(tensor) + let imageData = new Uint8ClampedArray(tensor.size * 4) + for (let i = 0, len = imageData.length; i < len; i += 4) { + imageData[i + 3] = 255 * (tensor.data[i / 4] - min) / (max - min) + } + return new ImageData(imageData, tensor.shape[0], 1) +} + /** * Takes in a ndarray of shape [x, y] * and creates image data