add 1d-tensor to image conversion util function

This commit is contained in:
Leon Chen
2016-09-20 12:39:51 -04:00
parent cbdce83f1e
commit cb8a3d77f9
+13
View File
@@ -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