1

I have an MNIST dataset of the shape (60000, 28, 28) and I need a way to crop and shift the images such that the middle pixel value in each image is the same as that in the cropped images. I have extracted the first and last rows and columns of the black pixels in each image. I need an output array of shape (60000, 20, 20) containing the cropped images.

I have tried using this code to crop out all images:

cropped = train_images_raw[:, first_ink_rows[1]:last_ink_rows[1]+1, first_ink_cols[1]:last_ink_cols[1]+1]

but the shape is only fixed for a specific black pixel row and column.

How can shift the image to fit in a boundary box of the size (20, 20) and still retain the value of the mid pixel?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • What is the `mid pixel`? Always `(14, 14)` or do you have `60000` different ones? – scleronomic Sep 21 '20 at 18:03
  • Yes I do. Since each image seems to be positioned differently. – hashPhoeNiX Sep 21 '20 at 18:16
  • And is the `mid pixel` always at least 10 pixels from the boundary away or is padding necessary? – scleronomic Sep 21 '20 at 18:30
  • you could do something like [this](https://stackoverflow.com/questions/46734116/numpy-how-to-slice-index-an-array-using-arrays), but i guess just looping over your array is the best way – scleronomic Sep 21 '20 at 18:46
  • The mid pixel is calculated with this formula: ```mid_row = (first_inky_rows + last_inky_rows)//2``` and ```mid_col = (first_inky_cols + last_inky_cols)//2``` for both rows and columns which are then used to access the middle value through indexing. ```image[mid_row, mid_col] ``` – hashPhoeNiX Sep 21 '20 at 18:47

0 Answers0