I’m currently working with DICOM files and the TensorFlow IO library for DICOM files seems to throw some errors. So what I was originally doing was this:
# read file bytes image_bytes = tf.io.read_file(image_path) # Convert to a tensor image_as_tensor = tfio.image.decode_dicom_image(image_bytes, dtype=IMAGE_TYPE) print(image_as_tensor.get_shape())
(1, 519, 519, 1)
Anyways, I instead decided to load the DICOM files with pydicom
, which seems to work loading the data into a numpy
array. Yet, when I create a tensor from the data, I can’t seem to get it in the correct dimensions:
# read into dicom file ds = pydicom.dcmread(image_path) print(ds.pixel_array.shape) # take pixel array, and lets create a tensor image_as_tensor = tf.convert_to_tensor(ds.pixel_array, dtype=IMAGE_TYPE) print(image_as_tensor.get_shape())
(519, 519)
Ultimately, I do want the (Z, X, Y, D) format for some later parts of the program, but not sure how to get the 2D tensor into that version.
Thanks!
Advertisement
Answer
You could just use numpy.reshape()
, e.g.:
import numpy as np arr = np.zeros((20, 30)) shape = arr.shape print(shape) # (20, 30) arr = arr.reshape(1, *shape, 1) print(arr.shape) # (1, 20, 30, 1)