Slicing torch images as we do in numpy images

Hello guys !
I am working on a problem in which I have the coordinates to slice the image like [y, y+height, x, x+width]. So if if I have torch image obtained using

 img = Variable(img.cuda())

how can we slice the image to get that specific area of image [y:y+height, x:x+width] .

You can directly index your image tensor:

img = torch.randn(1, 3, 10, 10, device='cuda')
x, y = 1, 1
width, height = 5, 5
img[:, :, y:y+height, x:x+width]
1 Like