Image with 4 channels

i have images with 4-channels that i created by stacking RGB and thermal data. as follow:


    i1= Image.open('rgb.png')   #rgb image
    i2 = Image.open('th.png')   #thermal image
    img = np.dstack((i1,i2))


I am working with pytorch to train a model on these images and at some point I have to process them separately. So I have to extract the RGB image and the thermal image. here is what I did:

def forward(self, image):      
        # split data into RGB and INF
        thermal = image[:,3:] 
        rgb = image[:,:3]   

will “thermal” extract the last channel of my 4-channels image and “rgb” will extract the 3 first channels ?

Assuming you are permuting the tensor to the channels-first memory layout and it contains a batch dimension, your code looks correct. A quick check would be:

i1 = np.zeros((24, 24, 3))
i2 = np.ones((24, 24, 4))
img = np.dstack((i1, i2))
print(img.shape)
> (24, 24, 7)

image = torch.from_numpy(img)
image = image.permute(2, 0, 1).unsqueeze(0)
thermal = image[:, 3:] 
rgb = image[:, :3]   
print((thermal == 1.).all())
> tensor(True)

print((rgb == 0.).all())
> tensor(True)