如何用手电筒添加图像通道?

How to add image channel with torch?

我已经加载了一张 3 通道的 jpg 图像,如何添加最后一个通道以生成具有 4 通道的图像张量?

假设您有一个具有 3 个通道的图像,高度 height 和宽度 width:

a = torch.Tensor(3, height, width) -- this is your image
b = torch.Tensor(1, height, width) -- the channel you want to add
c = torch.cat(a,b,1)

一个工作示例:

th> a = torch.Tensor(3,3,3):fill(1)                                                               
th> b = torch.Tensor(1,3,3):fill(0)
th> c = torch.cat(a,b,1)                                                                   
th> c
(1,.,.) = 
  1  1  1
  1  1  1
  1  1  1

(2,.,.) = 
  1  1  1
  1  1  1
  1  1  1

(3,.,.) = 
  1  1  1
  1  1  1
  1  1  1

(4,.,.) = 
  0  0  0
  0  0  0
  0  0  0
[torch.DoubleTensor of size 4x3x3]