Resize all the images in a genarator with cv2 (MedMNIST)

Viewed 30

I've been experimenting on MedMNIST data and I noticed that the default loader produces 28x28x3 images. I wanted to resize all images via cv2 to 32x32x3 I previously tried this method but my CNN models fail to achieve good accuracy.

 def regen(generate):
  for i, j in generate:
    a= np.zeros((64, 32, 32, 3))
    a[:,2:30,2:30,:]=i
    b = a/255
  yield  b, j

Here is a link to the original dataset https://medmnist.com/

1 Answers

Not sure if this is what you tried before, but you can try resizing the images using cv2.resize:

import cv2

img = cv2.imread('my_image.jpg')
res = cv2.resize(img, dsize=(32, 32))

You can also try changing the interpolation argument which defaults to INTER_LINEAR, but there are others that may work better for your case. The possible values from the documentation I linked:

  • INTER_NEAREST - a nearest-neighbor interpolation
  • INTER_LINEAR - a bilinear interpolation (used by default)
  • INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
  • INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
  • INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
Related