陆一凡
陆一凡

Reputation: 147

How to reverse RGB array to BGR array in tensorflow.js

There is a RGB image with [512,512,3] shape ,now I want to reverse the array in BGR not in RGB,how to do it in tensorflow.js

Upvotes: 3

Views: 1626

Answers (1)

edkeveked
edkeveked

Reputation: 18371

You can use tf.reverse along the channel axis

tf.reverse(tensor, -1)

One can also use tf.unstack and tf.stack. That can be useful if one wants to reverse the image channel in any order

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/[email protected]"> </script>
  </head>

  <body>
  </body>
</html>

Upvotes: 2

Related Questions