首页 > 解决方案 > 用非整数因子重新缩放图像的最佳方法是什么?

问题描述

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

干杯。

标签: imageimage-processingscalemnist

解决方案


我在以下链接上找到了使用双线性插值调整灰度大小的有用代码: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)

推荐阅读