使用非整数因子重新缩放图像的最佳方法是什么?

What is the best approach to rescale an image with a non-integer factor?

我正在开展一个项目,使用在 MNIST 数据集上训练的神经网络识别图像上的多个数字。第一步是检测使用 CCL 算法制作的二值图像上的数字。但问题是所有检测到的数字都应该使用抗锯齿技术进行大小归一化,以适应 20x20 像素框,同时保持它们的纵横比 (http://yann.lecun.com/exdb/mnist/)。 那么,我该如何解决这个问题呢?

干杯。

我在以下 link 上找到了使用双线性插值调整灰度大小的有用代码:http://tech-algorithm.com/articles/bilinear-image-scaling/ 在将图像调整为长边为 20 像素的尺寸后,图像将根据数字的质心以 28x28 图像为中心。 python计算图像质心应居中的代码:

centroid = np.ones(2)
summ = 0
img2 = np.array(img2)
for i in range (img2.shape[0]):
    for j in range (img2.shape[1]):
        if img2[i][j]:
            summ+=img2[i][j]
            centroid[0]+=i*img2[i][j]
            centroid[1]+=j*img2[i][j]
centroid /= summ
centroid = np.rint(centroid)