0

I have saved the Preprocessed DICOM in a folder total of 300 .dcm files, but when I open this DICOM folder path in RadiANT DICOM Viewer only One slice is displayed, here is my code is attached, Can you please help me how to display the whole scan. I think the main problem in Image Position and Slice location


import os
import numpy as np
import matplotlib.pyplot as plt
import pydicom
from pydicom.encaps import encapsulate
from pydicom.uid import JPEG2000
from imagecodecs import jpeg2k_encode
basepath="/home/hammad/AssementTask/DICOM/"
des_path="/home/hammad/AssementTask/g/"
file_list = [f.path for f in os.scandir(basepath)]
ds = pydicom.dcmread(file_list[0])
for i in range(imgs_after_resamp.shape[0]):
  
        out = imgs_after_resamp[i,:,:]
        #Need to copy() to meet jpeg2k_encodes C contiguous requirement
        arr_crop = out.copy() 
        out = out.astype(np.int16)
        # jpeg2k_encode to perform JPEG2000 compression
        arr_jpeg2k = jpeg2k_encode(arr_crop)
        # convert from bytearray to bytes before saving to PixelData
        arr_jpeg2k = bytes(arr_jpeg2k)
        ds.Rows = arr_crop.shape[0]
        ds.Columns = arr_crop.shape[1]
        ds[0x0018, 0x0050].value=np.round(spacing[0])
        ds[0x0028, 0x0030].value=[np.round(spacing[1]),np.round(spacing[2])]
        ds.InstanceNumber = i
        ds.PixelData = encapsulate([arr_jpeg2k])
        ds.save_as((des_path + str(i) + '.dcm'.format(i)))

1 Answers1

2

I am not familiar with python. But some things seem to be obvious to me, so I will try an answer:

ds = pydicom.dcmread(file_list[0])
    for i in range(imgs_after_resamp.shape[0]):
       [...]

You are reading one file and use it as a template for all the resampled files. At minimum, you will have to create a new SOP Instance UID (0008,0018) for each file that you save. This is very likely the reason why the viewer only displays one image. The SOP Instance UID uniquely identifies the image. If all your resampled images have the same SOP Instance UID, this will tell the viewer that the same image is loaded over and over again. I.e. the newly loaded image is considered a duplicate.

And yes, to update the geometry information, further attributes need to be set to appropriate values. This partially depends on the type of image (SOP Class UID, 0008,0016). But here are the main suspects:

  • Image Position Patient (0020,0032)
  • Image Orientation Patient (0020,0037)
  • Slice Location (0020,1041)

Furthermore, make sure that the Frame Of Reference UID (0020,0052) is only kept from the original images if both image sets are using the same coordinate system (i.e. an Image Position Patient in your resampled stack must refer to the same origin as in the original images). In case of doubt, assign a new FOR-UID. Must be identical for all images in your stack.

Last point: This depends on the SOP Class even more, so I can just give you a general hint. The resampling is a derivation in terms of DICOM, so the Image Type (0008,0008), must be "DERIVED" in the second component. This unleashes a phletora of other requirements, depending on the SOP Class. Usually, you have to describe the type of derivation and reference the images from which you derived the resampled image.

Not everything of this will be necessary to have the images properly displayed in a viewer. But if you intend to write your implementation in product quality, you need to consider them. Look into the module table for your IOD in DICOM Part 3 as a starting point for updating the header information.

Markus Sabin
  • 3,916
  • 14
  • 32