Search code examples
imageimage-processingscalemnist

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


I'm working on a project for recognition of multiple digits on an image using a neural network trained on the MNIST dataset. The first step is detecting digits on a binary image which is made using CCL algorithm. But the problem is that all detected digits should be size normalized with the anti-aliasing technique to fit in a 20x20 pixel box while preserving their aspect ratio (http://yann.lecun.com/exdb/mnist/). So, how can I solve this problem?

Cheers.


Solution

  • I've found the useful code for grayscale resizing using Bilinear interpolation on the following link: http://tech-algorithm.com/articles/bilinear-image-scaling/ After resizing an image on size where the longer side is 20 pixels long, the image is centered on the 28x28 image according to the centroid of the digit. The python code for calculating the centroid of the image which should be centered:

    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)