简体   繁体   English

如何使用python划分半个pydicom文件(图像)?

[英]How to divide in half pydicom files (image) using python?

I have a lot of images (pydicom files). 我有很多图像(pydicom文件)。 I would like to divide in half. 我想分成两半。 From 1 image, I would like 2 images: part left and part right. 从1张图片,我想要2张图片:左边和右边。

Input: 1000x1000 Output: 500x1000 (width x height). 输入:1000x1000输出:500x1000(宽x高)。

Currently, I can only read a file. 目前,我只能读取一个文件。

ds = pydicom.read_file(image_fps[0]) # read dicom image from filepath 

First part, I would like to put half in one folder and the other half to second. 第一部分,我想把一半放在一个文件夹中,另一半放到第二个。 This is what I have: enter image description here This is what I want: enter image description here 这就是我所拥有的: 在此处输入图像描述这就是我想要的: 在此处输入图像描述

I use Mask-RCNN to object localization problem. 我使用Mask-RCNN来对象定位问题。 I would like crop 50% of image size (pydicom file). 我想裁剪50%的图像大小(pydicom文件)。

EDIT1: EDIT1:

import SimpleITK as sitk
    filtered_image = sitk.GetImageFromArray(left_part)
    sitk.WriteImage(filtered_image, '/home/wojtek/Mask/nnna.dcm', True)

I have dicom file, but I can't display it. 我有dicom文件,但我无法显示它。

this transfer syntax JPEG 2000 Image Compression (Lossless Only), can not be read because Pillow lacks the jpeg 2000 decoder plugin

Once you have executed pydicom.dcm_read() your pixel data is available at ds.pixel_array . 执行pydicom.dcm_read()您的像素数据可在ds.pixel_array You can just slice the data you want and save it with any suitable library. 您可以只切片所需的数据并使用任何合适的库保存。 In this example I will be using matplotlib as I also use that for verifying whether my slicing is correct. 在这个例子中,我将使用matplotlib,因为我也用它来验证我的切片是否正确。 Adjust to your needs obviously, one thing you need to do is generate the correct path/filenames for saving. 显然需要调整您的需求,您需要做的一件事就是生成正确的路径/文件名以便保存。 Have fun! 玩得开心! (this script assumes the filepaths are available in a paths variable) (此脚本假定文件路径在paths变量中可用)

import pydicom
import matplotlib

# for testing if the slice is correct
from matplotlib import pyplot as plt

for path in paths:
    # read the dicom file
    ds = pydicom.dcmread(path)

    # find the shape of your pixel data
    shape = ds.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  = ds.pixel_array[:, :half_x]
    right_part = ds.pixel_array[:,half_x:]

    # to check whether the slices are correct, matplotlib can be convenient
    # plt.imshow(left_part); do not do this in the loop

    # save the files, see the documentation for matplotlib if you want a different format
    # bmp, png are surely supported

    path_to_left_image = 'generate\the\path\and\filename\for\the\left\image.bmp'
    path_to_right_image = 'generate\the\path\and\filename\for\the\right\image.bmp'
    matplotlib.image.imsave(path_to_left_image, left_part)
    matplotlib.image.imsave(path_to_right_image, right_part)


If you want to save the DICOM files keep in mind that they may not be valid DICOM if you do not update the appropriate data. 如果要保存DICOM文件,请记住,如果不更新相应的数据,它们可能不是有效的DICOM。 For instance the SOP Instance UID is technically not allowed to be the same as in the original DICOM file, or any other SOP Instance UID for that matter. 例如,技术上不允许SOP实例UID与原始DICOM文件或任何其他SOP实例UID相同。 How important that is, is up to you. 这有多重要,取决于你。

With a script like below you can define named slices and split any dicom image file it finds in the supplied path into the appropriate slices. 使用下面的脚本,您可以定义命名切片,并将在提供的路径中找到的任何dicom图像文件拆分到相应的切片中。

import os
import pydicom
import numpy as np

def save_partials(parts, path_to_directory):
    """
    parts: list of tuples, each tuple specifying a name and a list of four slice offsets
    path_to_directory: path to directory containing dicom files
    any file with a .dcm extension will have its image data split into the specified slices and saved accordingly. 
    original file will not be modified
    """

    dir_content = [os.path.join(path_to_directory, item) for item in os.listdir(path_to_directory)]
    files = [i for i in dir_content if os.path.isfile(os.path.join(path_to_directory, i))]
    for file in files:
        root, extension = os.path.splitext(file)
        if extension.lower() != '.dcm':
            # not a .dcm file, continue with next iteration of loop
            continue
        for part in parts:
            ds = pydicom.read_file(file)
            if not isinstance(ds.pixel_array, np.ndarray):
                # no image data available
                continue
            part_name = part[0] 
            p = part[1] # slice list
            ds.PixelData = ds.pixel_array[p[0]:p[1], p[2]:p[3]].tobytes()
            ds.Rows = p[1] - p[0]
            ds.Columns = p[3] - p[2]
            ##
            ## Here you can modify any tags using ds.KeyWord
            ##
            new_file_name = "{r}-{pn}{ext}".format(r=root, pn=part_name, ext=extension)
            ds.save_as(new_file_name)
            print('saved {}'.format(new_file_name))


dir_path = '/home/wojtek/Mask'
parts = [('left', [0,512,0,256]),
         ('right', [0,512,256,512])]

save_partials(parts, dir_path)

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

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