Search code examples
pythonopencvnested-listspydicommedical-imaging

Modifying a large number of DICOM (.dcm) files.


bit of a simple question perhaps but I am not making progress and would appreciate help.

I have a list of size 422. Within index 0 there are 135 file paths to .dcm images. For example '~/images/0001.dcm','~/images/0135.dcm' Within index 1, there are 112 image paths, index 2 has 110, etc.

All images are of size 512 x 512. I am looking to re-size them to be 64 x 64.

This is my first time working with both image and .dcm data so I'm very unsure about how to resize. I am also unsure how to access and modify the files within the 'inner' list, if you will.

Is something like this way off the mark?

IMG_PX_SIZE = 64 
result = []
for i in test_list:
    result_inner_list = []        
    for image in i:
    # resize all images at index position i and store in list
        new_img = cv2.resize(np.array(image.pixel_array (IMG_PX_SIZE,IMG_PX_SIZE))
        result_inner_list.append(new_img)
    # Once all images at index point i are modified, append them these to a master list.
    result.append(result_inner_list)

Solution

  • You seem to be struggling with two issues:

    • accessing the file paths
    • resize

    For you to win, better separate these two tasks, sample code below

    IMG_PX_SIZE = 64 
    
    def resize(image):
        # your resize code here similar to:        
        # return v2.resize(np.array(image.pixel_array(IMG_PX_SIZE,IMG_PX_SIZE))
        pass
    
    def read(path):
        # your file read operation here
        pass
    
    
    big_list = [['~/images/0001.dcm','~/images/0135.dcm'],
                ['~/images/0002.dcm','~/images/0136.dcm']]
    
    resized_images = [[resize(read(path)) for path in paths] for paths in big_list]