How do i fold patches back to form a complete image

I have do I fold patches back to get original image

def perturb_patch(img, patch_idx, patch_size, stride):
    img = img.unsqueeze(0)
    patches = img.unfold(2, patch_size, stride).unfold(3, patch_size, stride)
    patches = patches.reshape(1, 3, -1, patch_size, patch_size)
    patches = patches.squeeze(0).permute(1, 0, 2, 3)
    patches[patch_idx][0, :, :] = 0.09803922
    patches[patch_idx][1,:, :] = 0.21333333
    patches[patch_idx][2,:, :] = 0.61176471

    merged_patches = nnf.fold(patches.reshape(196, 768, 1), (224, 224), kernel_size=16, stride=16)
    return merged_patches

I am getting the merged_patches wrong. For the patches, I get a shape of (196, 3, 16, 16) how do I merge it to get a complete image? My image is (3, 224, 224) .

This helped me