如何在 Tensorflow.js 中计算拉普拉斯算子?

ere*_*aud 5 javascript tensorflow.js

我目前正在尝试设计一个 Node.js 代码,它能够判断图像是否模糊。为了实现这一目标,我从这个问题中获得了灵感。所以我需要做的是计算矩阵拉普拉斯(这就是我们所说的?),然后计算方差。

我用 OpenCV 做这件事没有问题(使用opencv4nodejs):

# load image
_cvImg = cv.imread _file

# get grayscale image
_cvImgGray =_cvImg.bgrToGray()

# Compute laplacian
_laplacian = _cvImgGray.laplacian(cv.CV_8U)

# Get the standard deviation
_meanStdDev = _laplacian.meanStdDev()

_stddevarray = _meanStdDev.stddev.getDataAsArray()
_stdDeviation = _stddevarray[0]

# Get the variation
_variation = Math.pow(_stdDeviation, 2)
Run Code Online (Sandbox Code Playgroud)

但现在,我正在使用 Tensorflow.js,它确实不太容易......这是我尝试做的:

# load image
_cvImg = cv.imread _file

#convert frame to a tensor
try
    _data = new Uint8Array(_frame.cvtColor(cv.COLOR_BGR2RGB).getData().buffer)
    _tensorFrame = tfjs.tensor3d(_data, [_frame.rows, _frame.cols, 3])
catch _err
    @log.error "Error instantiating tensor !!!"
    @log.error _err.message

# get grayscale image
_grayscaleFrame = _tensorFrame.mean(2).expandDims(2)

# prepare convolution to get laplacian
laplaceFilter = tfjs.tensor2d([[0,1,0], [1,-4,1], [0,1,0]])
laplaceFilter3D = laplaceFilter.expandDims(2)

# get laplacian
_laplacian = tfjs.conv1d _tensorFrame, laplaceFilter3D, 1, 'same'

# get standard deviation
_standardDeviation = tfjs.moments(_laplacian2).variance.buffer().values[0]

# get variance
_variance = _standardDeviation * _standardDeviation

# dispose tensor to avoid memeory leaks
_tensorFrame.dispose()
Run Code Online (Sandbox Code Playgroud)

毫不奇怪,上面的代码不起作用。我知道我的卷积应该是二维的(tf.conv2d)而不是一维(tf.conv1d),因为我正在处理图像。如果我查看API 中的 tf.conv2d,我可以看到这个签名:

tf.conv2d (x, filter, strides, pad, dataFormat?, dilations?, dimRoundingMode?) 
Run Code Online (Sandbox Code Playgroud)

过滤器应该是 tf.Tensor4D !但我不知道如何将以下滤波器矩阵转换为 tf.Tensor4D

    1
1   4   1   
    1
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么 ?如何在 Tensorflow.js 中获取矩阵的拉普拉斯?即如何在代表图像的tensor2d 和上述过滤器之间执行简单的卷积?

edk*_*ked 2

可以通过首先扩展二维张量的维度来对二维张量执行卷积。同样对于过滤器使用tf.reshape

计算完成后,tf.squeeze 将删除形状 1 的维度,从而返回 2d 张量

const x = tf.tensor2d(Array.from({length: 16}, (_, k) => k+1), [4, 4]); // input image 2d

const filter = tf.tensor2d([0, 1, 0, 1, 0, 1, 0, 1, 0], [3, 3]) // input filter 2d

x.reshape([1, ...x.shape, 1]).conv2d(filter.reshape([...filter.shape, 1, 1]), 1, 'valid').squeeze().print() // conv
Run Code Online (Sandbox Code Playgroud)
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.1"> </script>
  </head>

  <body>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)