[英]How to divide in half pydicom files (image) using python?
我有很多圖像(pydicom文件)。 我想分成兩半。 從1張圖片,我想要2張圖片:左邊和右邊。
輸入:1000x1000輸出:500x1000(寬x高)。
目前,我只能讀取一個文件。
ds = pydicom.read_file(image_fps[0]) # read dicom image from filepath
第一部分,我想把一半放在一個文件夾中,另一半放到第二個。 這就是我所擁有的: 在此處輸入圖像描述這就是我想要的: 在此處輸入圖像描述
我使用Mask-RCNN來對象定位問題。 我想裁剪50%的圖像大小(pydicom文件)。
EDIT1:
import SimpleITK as sitk
filtered_image = sitk.GetImageFromArray(left_part)
sitk.WriteImage(filtered_image, '/home/wojtek/Mask/nnna.dcm', True)
我有dicom文件,但我無法顯示它。
this transfer syntax JPEG 2000 Image Compression (Lossless Only), can not be read because Pillow lacks the jpeg 2000 decoder plugin
執行pydicom.dcm_read()
您的像素數據可在ds.pixel_array
。 您可以只切片所需的數據並使用任何合適的庫保存。 在這個例子中,我將使用matplotlib,因為我也用它來驗證我的切片是否正確。 顯然需要調整您的需求,您需要做的一件事就是生成正確的路徑/文件名以便保存。 玩得開心! (此腳本假定文件路徑在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)
如果要保存DICOM文件,請記住,如果不更新相應的數據,它們可能不是有效的DICOM。 例如,技術上不允許SOP實例UID與原始DICOM文件或任何其他SOP實例UID相同。 這有多重要,取決於你。
使用下面的腳本,您可以定義命名切片,並將在提供的路徑中找到的任何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.