Niko Gamulin
Niko Gamulin

Reputation: 66565

How to transform the tensor of grayscale images to 3-channel images?

I have a tensor of n grayscale images of size W x H: images.shape outputs (n, W, H, 1) I would like to transform this tensor to (n, W, H, 3) by adding two copies of the grayscale channel (last dimension).

I would appreciate if anyone suggested a good way to do that.

Thanks!

Upvotes: 1

Views: 813

Answers (1)

Alexander Ejbekov
Alexander Ejbekov

Reputation: 5940

As far as I understand, you are looking for something like this (judging by the numpy tag):

>>> import numpy as np
>>> x = np.array([[[1,5,7],[9,2,1]], [[5,6,7], [8,2,4]]])
>>> x
array([[[1, 5, 7],
        [9, 2, 1]],

       [[5, 6, 7],
        [8, 2, 4]]])
>>> np.stack((x,) * 3, axis=-1)
array([[[[1, 1, 1],
         [5, 5, 5],
         [7, 7, 7]],

        [[9, 9, 9],
         [2, 2, 2],
         [1, 1, 1]]],


       [[[5, 5, 5],
         [6, 6, 6],
         [7, 7, 7]],

        [[8, 8, 8],
         [2, 2, 2],
         [4, 4, 4]]]])

Upvotes: 1

Related Questions