首页 > 解决方案 > 在本机反应中将RGB转换为灰度

问题描述

我已经在灰度图像上训练了一个模型。它接受 48x48 像素和灰度格式组合为 48x48x1。我在本机反应中使用此模型。我从手机存储中选择的图像被转换为​​ tensor3d RGB。我想将它们转换为灰度。

const decodeJpeg=(rawImageData)=>{
//Function to convert jpeg image to tensors
const TO_UINT8ARRAY = true;
const { width, height, data } = jpeg.decode(rawImageData, TO_UINT8ARRAY);
// Drop the alpha channel info for mobilenet
const buffer = new Uint8Array(width * height * 3);
let offset = 0; // offset into original data
for (let i = 0; i < buffer.length; i += 3) {
  buffer[i] = data[offset];
  buffer[i + 1] = data[offset + 1];
  buffer[i + 2] = data[offset + 2];
  offset += 4;
}
return tf.tensor3d(buffer, [height, width, 3]); 
}

尝试了下面的代码,但它没有帮助。

const imageTensor = decodeJpeg(imageData).resizeBilinear([48,48]).reshape([48,48,1])

标签: javascriptreact-nativetensorflowrgbgrayscale

解决方案


推荐阅读