Sampling patches of a set of images

I need to get patches around certain points in a set of images. For the sake of this question, let’s say I have 2 images, and 8 points per image. I’d like to get 5-pixel-wide patches around each point on each image. My current approach is to make the indices first, and then try to sample the images with my indices. I’m stuck in the last part.

Let:

`src_uvs` → coordinates of the interest points (shape `[2, 2, 8]`: xy coords for 8 points on 2 images)

`patch_size` → patch half-width ( =5. The patch is symmetric around each point, so the size of the patch is 11x11)

`N` → Number of interest points ( =8)

`src_img_gray` → The images to be sampled, with shape [2, 540, 960] (2 images for this example)

``````# Make one patch
meshgrid_patch = torch.meshgrid(
torch.arange(-patch_size , patch_size + 1),
torch.arange(-patch_size , patch_size + 1))

# Take each coordinate of the meshgrid, and expand it to size [11, 11, 2, 8]
mm_x = meshgrid_patch[0][..., None, None]
mm_x = mm_x.expand(2 * patch_size + 1 , 2 * patch_size + 1, nsrc, N)

# We add this to the point's coordinates
x_coords = src_uvs[:, 0, :] + mm_x
y_coords = src_uvs[:, 1, :] + mm_x.transpose(0, 1) # transposing 1st 2 components.
``````

At this point, `x_coords` and `y_coords` are tensors with shape `[11, 11, 2, 8]` , which hold the coordinates of the pixels that I want to sample on the image set ( which has shape `[2, 540, 960]` ). I’m unsure how to proceed to sample the images.