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:
JavaScript
x
8
1
# read file bytes
2
image_bytes = tf.io.read_file(image_path)
3
4
# Convert to a tensor
5
image_as_tensor = tfio.image.decode_dicom_image(image_bytes, dtype=IMAGE_TYPE)
6
7
print(image_as_tensor.get_shape())
8
JavaScript
1
2
1
(1, 519, 519, 1)
2
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:
JavaScript
1
8
1
# read into dicom file
2
ds = pydicom.dcmread(image_path)
3
print(ds.pixel_array.shape)
4
5
# take pixel array, and lets create a tensor
6
image_as_tensor = tf.convert_to_tensor(ds.pixel_array, dtype=IMAGE_TYPE)
7
print(image_as_tensor.get_shape())
8
JavaScript
1
2
1
(519, 519)
2
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.:
JavaScript
1
12
12
1
import numpy as np
2
3
arr = np.zeros((20, 30))
4
shape = arr.shape
5
6
print(shape)
7
# (20, 30)
8
9
arr = arr.reshape(1, *shape, 1)
10
print(arr.shape)
11
# (1, 20, 30, 1)
12