gpt4 book ai didi

python - 从多个 jpg 图像创建一个 Dicom

转载 作者:行者123 更新时间:2023-12-05 04:46:05 28 4
gpt4 key购买 nike

我已经用一张图片成功构建了 dicom,但我找不到添加更多图片的方法...我认为问题可能出在我的像素阵列中,谁能帮我解决这个问题?

# Populate required values for file meta information
meta = pydicom.Dataset()
meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian
meta.MediaStorageSOPClassUID = pydicom._storage_sopclass_uids.MRImageStorage
meta.MediaStorageSOPInstanceUID = pydicom.uid.generate_uid()

# build dataset
ds = Dataset()
ds.file_meta = meta
ds.fix_meta_info()

# unknown options
ds.is_little_endian = True
ds.is_implicit_VR = False
ds.SOPClassUID = pydicom._storage_sopclass_uids.MRImageStorage
ds.SeriesInstanceUID = pydicom.uid.generate_uid()
ds.StudyInstanceUID = pydicom.uid.generate_uid()
ds.FrameOfReferenceUID = pydicom.uid.generate_uid()
ds.BitsStored = 16
ds.BitsAllocated = 16
ds.SamplesPerPixel = 1
ds.HighBit = 15
ds.ImagesInAcquisition = "1"
ds.InstanceNumber = 1
ds.ImagePositionPatient = r"0\0\1"
ds.ImageOrientationPatient = r"1\0\0\0\-1\0"
ds.ImageType = r"ORIGINAL\PRIMARY\AXIAL"
ds.RescaleIntercept = "0"
ds.RescaleSlope = "1"
ds.PixelRepresentation = 1

# Case options
ds.PatientName = "Anonymous"
ds.PatientID = "123456"
ds.Modality = "MR"
ds.StudyDate = '20200225'
ds.ContentDate = '20200225'

def ensure_even(stream):
# Very important for some viewers
if len(stream) % 2:
return stream + b"\x00"
return stream

pixel_data_list = []
for root, dir, filenames in walk(folder):
filenames.sort(key=natural_keys)
for filename in filenames:
filename = folder + filename
# convert image to grayscale
img = Image.open(filename).convert('L')
img.save(filename)

# open image, decode and ensure_even stream
with open(filename, 'rb') as f:
arr = decode(f)

pixel_data_list.append(arr.tobytes())

# required for pixel handler
ds.BitsStored = 8
ds.BitsAllocated = 8
ds.HighBit = 7
ds.PixelRepresentation = 0

# grayscale without compression
ds.PhotometricInterpretation = "MONOCHROME2"
ds.SamplesPerPixel = 1 # 1 color = 1 sampleperpixel
ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian
ds.PixelData = array(pixel_data_list)
ds.NumberOfFrames = len(pixel_data_list)

# Image shape
ds['PixelData'].is_undefined_length = False
ds.Columns = img.width
ds.Rows = img.height

# validate and save
pydicom.dataset.validate_file_meta(ds.file_meta, enforce_standard=True)
new_filename = filename.replace('.jpg', name + '.dcm')
ds.save_as(new_filename, write_like_original=False)

似乎也无法压缩图像,只能使用单色(请参阅此处的尝试 https://stackoverflow.com/a/68939321/1827162)...但这不是问题,最重要的是能够添加多个图像。感谢您的指点!

最佳答案

像素数据应该是字节。如果你的传输语法是未压缩的,那么你需要将数据连接在一起,如果连接数据的长度是奇数,那么你必须添加一个尾随填充字节:

# For uncompressed transfer syntaxes only!
pixel_data = b"".join(pixel_data_list)
ds.PixelData = pixel_data + b"\x00" if len(pixel_data) % 2 else pixel_data

For compressed transfer syntaxes you must use encapsulation

关于python - 从多个 jpg 图像创建一个 Dicom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68939302/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com