0

I have thousands of dicom images in a folder. I read them with pydicom like this

import numpy as np
import dicom

folder = "/images"
imgs = [dicom.read_file(folder + '/' + s) for s in os.listdir(folder)]

I then want to stack all images as a numpy array, like this:

data = np.stack([i.pixel_array for i in imgs])

However, the images are or different size and therefore cannot be stacked.

How can I add a step that resizes all images to 1000x1000 ?

Suever
  • 64,497
  • 14
  • 82
  • 101
spore234
  • 3,550
  • 6
  • 50
  • 76

1 Answers1

1

If you stored then as a list of numpy arrays then they can be different size. Otherwise use scipy zoom function,

import numpy as np
import dicom
import scipy

xsize = 1000; ysize = 1000
folder = "/images"
data = np.zeros(xsize, ysize, len(os.listdir(folder)))
for i, s in enumerate(os.listdir(folder)):

    img = np.array(dicom.read_file(folder + '/' + s).pixel_array)
    xscale = xsize/img.shape[0]
    yscale = ysize/img.shape[1]
    data[:,:,i] = scipy.ndimage.interpolation.zoom(img, [xscale, yscale]))

You could save as a list and stack but seems easier to pre-allocate a numpy array of size 1000 by 1000 by len(os.listdir(folder)). I've not got dicom or the test files so cannot check your case but the idea certainly works (I've used it before to scale images to the right size). Also check the scale is correct for your case.

Ed Smith
  • 12,716
  • 2
  • 43
  • 55