简体   繁体   中英

How can I get my dicom splitter/divider to save split/divided images?

I have cobbled together some code on python, to try and work through a folder of dicom files, splitting each image in two.

All my dicom files are X-rays of both the left and right feet, and I need to separate them.

To do this I am adapting some code produced by @g_unit seen here

Unfortunately - this attempt results in two unaltered copies of the original file - unsplit. It does work when writing the files as PNG or JPG, but not when writing as dicoms. My test image in the console also looks good.

In my below example, I am using a folder with only one file in it. I will adapt to write the new files and filenames after I get my single sample to work.

import matplotlib.pyplot as plt
import pydicom
import pydicom as pd
import os


def main():
    path = 'C:/.../test_block_out/'
    

    # iterate through the names of contents of the folder
    for file in os.listdir(path):

        # create the full input path and read the file
        input_path = os.path.join(path, file)
        dataset = pd.dcmread(input_path)

        
        
        
        shape = dataset.pixel_array.shape
        # get the half of the x dimension. For the y dimension use shape[0]
        half_x = int(shape[1] / 2)

        # slice the halves
        # [first_axis, second_axis] so [:,:half_x] means slice all from first axis, slice 0 to half_x from second axis
        left_part  = dataset.pixel_array[:, :half_x].tobytes()
        right_part = dataset.pixel_array[:,half_x:].tobytes()
        
                
        #Save halves 
        path_to_left_image = 'C:.../test_file/left.dcm'
        path_to_right_image = 'C:.../test_file/right.dcm'
        dataset.save_as(path_to_left_image, left_part)
        dataset.save_as(path_to_right_image, right_part)
        
        
        

        #print test image
        plt.imshow(dataset.pixel_array[:, :half_x]) 
        #plt.imshow(dataset.pixel_array[:,half_x:])
        

if __name__ == '__main__':
    main()

I have tried to write the pixel array to dataset.PixelData - but this throws the error:

ValueError: The length of the pixel data in the dataset (5120000 bytes) doesn't match the expected length (10240000 bytes). The dataset may be corrupted or there may be an issue with the pixel data handler.

Which makes sense, since its half my original dimensions. It will write a DCM, but I cannot load this DCM into any dicom viewer tools ('Decode error!')

Is there a way to get this to write the files as DCMs, not PNGs? Or will the DCMs always bug if the dimensions are incorrect?

A kind colleague has helped by providing the answer.

The issue was that I was saving "dataset", not "left_part".

The solution was to create a new pydicom object, deep copying the dcm file, and then modifying the copy. Code below:

# iterate through the names of contents of the folder
for file in os.listdir(path):

    # create the full input path and read the file
    input_path = os.path.join(path, file)
    dataset = pd.dcmread(input_path)
    left_part = copy.deepcopy(dataset)
    right_part = copy.deepcopy(dataset)
    
    shape = dataset.pixel_array.shape
    # get the half of the x dimension. For the y dimension use shape[0]
    half_x = int(shape[1] / 2)

    # slice the halves
    # [first_axis, second_axis] so [:,:half_x] means slice all from first axis, slice 0 to half_x from second axis
    left_part.PixelData  = dataset.pixel_array[:, :half_x].tobytes()
    left_part['Columns'].value=half_x
    right_part.PixelData = dataset.pixel_array[:,half_x:].tobytes()
    right_part['Columns'].value=shape[1]-half_x
    
    #Save halves 
    path_to_left_image = os.path.join(path, 'left_'+file)
    path_to_right_image = os.path.join(path, 'right_'+file)
    left_part.save_as(path_to_left_image)
    right_part.save_as(path_to_right_image)

    #print test image
    plt.imshow(left_part.pixel_array)
    plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM