如何在 tensorflow.js 中将 RGB 数组反转为 BGR 数组
How to reverse RGB array to BGR array in tensorflow.js
有一个[512,512,3]形状的RGB图像,现在我想在BGR而不是RGB中反转数组,如何在tensorflow.js
中实现
可以沿通道轴tf.reverse
使用
tf.reverse(tensor, -1)
也可以使用 tf.unstack
和 tf.stack
。如果想以任何顺序反转图像通道,这会很有用
temp = tf.unstack(tensor, axis)
tf.stack([temp[2], temp[1], temp[0]], axis)
const x = tf.tensor3d([1, 2 , 3, 4 , 5, 6, 7, 8], [2, 2, 2]);
const axis = 2;
// tf.reverse
x.reverse(axis).print();
// tf.stack and tf.unstack
const temp = tf.unstack(x, axis)
tf.stack([temp[1], temp[0]], 2).print()
// tf.split and tf.concat
const temp1 = tf.split(x, 2, 2)
tf.concat([temp1[1], temp1[0]], 2).print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.1"> </script>
</head>
<body>
</body>
</html>
有一个[512,512,3]形状的RGB图像,现在我想在BGR而不是RGB中反转数组,如何在tensorflow.js
中实现可以沿通道轴tf.reverse
使用
tf.reverse(tensor, -1)
也可以使用 tf.unstack
和 tf.stack
。如果想以任何顺序反转图像通道,这会很有用
temp = tf.unstack(tensor, axis)
tf.stack([temp[2], temp[1], temp[0]], axis)
const x = tf.tensor3d([1, 2 , 3, 4 , 5, 6, 7, 8], [2, 2, 2]);
const axis = 2;
// tf.reverse
x.reverse(axis).print();
// tf.stack and tf.unstack
const temp = tf.unstack(x, axis)
tf.stack([temp[1], temp[0]], 2).print()
// tf.split and tf.concat
const temp1 = tf.split(x, 2, 2)
tf.concat([temp1[1], temp1[0]], 2).print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.1"> </script>
</head>
<body>
</body>
</html>