简体   繁体   English

我怎样才能让我的 dicom 拆分器/分隔器保存拆分/分割的图像?

[英]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.我在 python 上拼凑了一些代码,尝试处理 dicom 文件的文件夹,将每个图像一分为二。

All my dicom files are X-rays of both the left and right feet, and I need to separate them.我所有的 dicom 文件都是左右脚的 X 光片,我需要将它们分开。

To do this I am adapting some code produced by @g_unit seen here为此,我正在调整此处看到的@g_unit 生成的一些代码

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.它在将文件写入 PNG 或 JPG 时有效,但在写入 dicom 时无效。 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:我试图将像素数组写入 dataset.PixelData - 但这会引发错误:

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!')它将编写一个 DCM,但我无法将此 DCM 加载到任何 dicom 查看器工具中(“解码错误!”)

Is there a way to get this to write the files as DCMs, not PNGs?有没有办法让它把文件写成 DCM,而不是 PNG? Or will the DCMs always bug if the dimensions are incorrect?或者,如果尺寸不正确,DCM 是否总是会出错?

A kind colleague has helped by providing the answer.一位好心的同事通过提供答案提供了帮助。

The issue was that I was saving "dataset", not "left_part".问题是我保存的是“数据集”,而不是“left_part”。

The solution was to create a new pydicom object, deep copying the dcm file, and then modifying the copy.解决方法是新建一个pydicom object,深拷贝dcm文件,然后修改拷贝。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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