I need some tips on how to create a DICOM file from two images. I already have a Python script with which I can create a DICOM from a 3D voxel. Now I want to add a color histogram (file format .tif) to the same DICOM. What options do I have to implement this? The 3D stack should not be overwritten, but I still want to be able to select the histogram in just one DICOM container. I would appreciate any kind of help 🙂
I have already found a way to create a valid DICOM from a .vox and now I want to add an RGB TIF image to it.
This is my code to generate a DICOM from a Vox
import matplotlib.pyplot as plt
import os
import datetime
import numpy as np
import pydicom.encaps
from PIL import Image
from pydicom.dataset import FileMetaDataset, Dataset,FileDataset
from pydicom.uid import ExplicitVRLittleEndian
from pydicom.uid import UID
def loadVOX(fname):
vol=[]
try:
f=open(fname,"rb")
size=os.path.getsize(fname)
f.seek(size-512*512*512*2)
vol=np.reshape(np.frombuffer(f.read(512*512*512*2),dtype='ushort'),(512,512,512),order='F').swapaxes(0,1)
finally:
f.close()
return vol
image_array =loadVOX("C:/Users/robin/Downloads/CT_20231116_111921.VOX")
# Umstellen des Arrays damit z nach oben zeigt
image_array = np.swapaxes(image_array, 0, 2)
plt.imshow(image_array[215])
#Konvertierung in 8 bit
min_val = np.min(image_array)
max_val = np.max(image_array)
normalized_data = (image_array - min_val) / (max_val - min_val)
scaled_data = (normalized_data * 255).astype(np.uint8)
# das b'' ist ein Bytes Literal --> ein leeres Objekt das 8 bit objekte halten kann. b steht für byte so wie f für float stehen würde. '' ist eine leere Variable in Python.
# Wieder was gelernt.
# Join erlaubt eine Verkettung der einzelnen Frames hintereinander, so kommen die dann in 512 Schritten zum Schluss raus
multi_frame_pixel_data = b''.join(frame.tobytes() for frame in scaled_data )
# Create a FileDataset (subclass of Dataset) to hold the DICOM data
ds = FileDataset("example.dcm", {}, preamble=b"" * 128)
# DICOM Tags die ich durch Try and Error rausbekommen habe
ds.PatientName = "John Doe"
ds.PatientID = "12345"
ds.StudyDescription = "Example Study"
ds.SeriesDescription = "Example Series"
ds.Modality = "CT"
ds.Rows = image_array.shape[0]
ds.Columns = image_array.shape[1]
ds.NumberOfFrames = len(image_array)
print(ds.Rows,ds.Columns,ds.NumberOfFrames)
ds.ImagePositionPatient = r"01"
ds.ImageOrientationPatient = r"011"
ds.PixelSpacing = r"11"
ds.BitsAllocated = 8 # Jetzt hier 8, da ich oben konvertiert hatte
ds.BitsStored = 8
ds.HighBit = 7
ds.PixelRepresentation = 0
ds.SamplesPerPixel = 1
ds.PhotometricInterpretation = "MONOCHROME2"
#Speichern
ds.PixelData = multi_frame_pixel_data
ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
ds.save_as("test.dcm")
dataset = pydicom.dcmread('test.dcm')
frame_generator = pydicom.encaps.generate_pixel_data_frame(dataset.PixelData)
if 'PixelData' in dataset:
rows = int(dataset.Rows)
cols = int(dataset.Columns)
print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
rows=rows, cols=cols, size=len(dataset.PixelData)))
if 'PixelSpacing' in dataset:
print("Pixel spacing....:", dataset.PixelSpacing)
plt.imshow(dataset.pixel_array[215])
plt.show()
print(dataset.pixel_array.shape)
Robin Scheel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.