如何使用 TensorflowJS 将网络摄像头图像转换为灰度? [英] How to transform a webcam image to grayscale using TensorflowJS?
问题描述
我正在尝试使用模型对灰度图像进行分类.
I am trying to use a model to classify grayscale images.
我使用了网络摄像头类 一些 TensorflowJS 示例中提供的使用网络摄像头获取图像.它运行良好,但返回的是 RGB 图像.
I used the Webcam class provided in some of the TensorflowJS examples to get an image using the webcam. It worked well, but it returns an RGB image.
将此图像转换为灰度的最佳方法是什么?我是否需要计算这个从张量中获取每个元素并计算一个表示灰度图像的新矩阵,或者是否有更简单或更方便的方法?
What is the best way to transform this image to grayscale? Do I need to compute this getting each element from the tensor and compute a new matrix that represents the grayscale image or is there a more simple or convenient way?
推荐答案
如果你需要以 [width, height, 1] 的形式来:
If you need to have it in the form [width, height, 1]:
tf.browser.fromPixels(image)
.mean(2)
.toFloat()
.expandDims(-1)
如果你需要以[1, width, height, 1]的形式:
If you need in the form [1, width, height, 1]:
tf.browser.fromPixels(image)
.mean(2)
.toFloat()
.expandDims(0)
.expandDims(-1)
这篇关于如何使用 TensorflowJS 将网络摄像头图像转换为灰度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!