Search code examples
c++machine-learningmnist

center of mass of pixels in grayscale image


I'm working on a program that lets the user draw a digit in a "scribbling area" and with the press of a button the application will predict the digit that he entered, using a neural net classifier.

Now, to train the neural net, I used MNIST database which specifies the following: "images from NIST were size normalized to fit in a 20x20 pixel box while preserving their aspect ratio [...] the images were centered in a 28 x 28 image by computing the center of mass of the pixels".

The problem that I'm facing is that after resizing the digit that the user drew in the scribbling area to a size of 20 x 20, I need to compute the center of mass of the pixels so I can center it in the middle of the 28 x 28 image.

How can I compute that ?


Solution

  • "Center of mass" (for binary images) is a bit convoluted way of saying "mean value across each dimension". In other words - take all x coordinates and average them - and you got x coordinate of your "center of mass", the same for y.

    In python, for data in X it would be

    center_of_mass = X.mean(axis=0)
    

    If you have pixels' intensities you can use them as "weights" thus leading to weighted averages, that's all.