Himanshu Naidu
Himanshu Naidu

Reputation: 89

Resize a DICOM image in python

I am trying to resize DICOM images of different dimensions into a common dimension size for training my neural network. I thought that cv2 could solve my problem. But I am getting a 'datatype not understood error' in my jupyter notebook

I am trying to create a tensorflow neural network that could predict the class of the image. Thus, I need images of a common dimension size for the first layer training

Here is the function I have created:

IMG_PX_SIZE = 224
def resize(img_dcm):
    return cv2.resize(np.array(img_dcm.pixel_array, (IMG_PX_SIZE,IMG_PX_SIZE)))

This is how I read the dcm files and pass it to the function:

img = pydi.dcmread(PATH)
image = resize(img)

I expected it to output a 224*224 sized image. But I am getting the following error:

<ipython-input-66-3cf283042491> in resize(img_dcm)
      1 IMG_PX_SIZE = 224
      2 def resize(img_dcm):
----> 3     return cv2.resize(np.array(image.pixel_array, (IMG_PX_SIZE,IMG_PX_SIZE)))

TypeError: data type not understood

Upvotes: 2

Views: 11164

Answers (3)

Mohamed Berrimi
Mohamed Berrimi

Reputation: 148

You can use this functions from here

You need to first read your dicom/Niftii files

def read_nifti_file(filepath):
"""Read and load volume"""
# Read file
   scan = nib.load(filepath)
# Get raw data
   scan = scan.get_fdata()
   return scan

then you can resize your volume:

def resize_volume(img):
"""Resize across z-axis"""
# Set the desired depth
desired_depth = 64
desired_width = 128
desired_height = 128
# Get current depth
current_depth = img.shape[-1]
current_width = img.shape[0]
current_height = img.shape[1]
# Compute depth factor
depth = current_depth / desired_depth
width = current_width / desired_width
height = current_height / desired_height
depth_factor = 1 / depth
width_factor = 1 / width
height_factor = 1 / height

# Resize across z-axis
img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)
return img

Upvotes: 0

T A
T A

Reputation: 1756

DICOM is not supported in OpenCV, see here. You will have to convert all of your images into a suitable format (e.g. jpg or png) before you are able to resize them with OpenCV:

OpenCV does not support DICOM images so that you will have to find a suitable libary (like http://dicom.offis.de/dcmtk.php.en ) and convert the loaded image to a cv::Mat.

Then again you may want to use a different library for re-sizing as well, it is probably not worth the effort to:

  1. Convert the images to a readable format for OpenCV
  2. Re-size them with OpenCV
  3. Convert them back to DICOM

I'd recommend you instead look into a library or tool specifically designed to work with DICOM images.

Upvotes: 0

kmario23
kmario23

Reputation: 61505

Here's an alternative way to resize the images using Scikit-Image:

In [105]: from pydicom.data import get_testdata_files

# read a sample image
In [106]: filename = get_testdata_files('MR_small.dcm')[0]
     ...: ds = pydicom.dcmread(filename)

In [107]: data = ds.pixel_array

In [108]: type(data)
Out[108]: numpy.ndarray

In [109]: data.shape
Out[109]: (64, 64)

In [111]: from skimage.transform import resize
In [114]: IMG_PX_SIZE = 32

# resize to new size
In [115]: resized_img = resize(data, (IMG_PX_SIZE, IMG_PX_SIZE), anti_aliasing=True)

In [116]: resized_img.shape
Out[116]: (32, 32)

Upvotes: 10

Related Questions