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

时间:2017-01-12 07:13:18

标签: c image torch

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

1 个答案:

答案 0 :(得分:1)

假设您有一个包含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]