简体   繁体   English

如何在 python 中替换 DICOM 中的图像?

[英]How to replace an image in DICOM in python?

I need to edit medical image saved as dicom.我需要编辑保存为 dicom 的医学图像。 My target is editing, doing threshhold and saving a new image as black and white.我的目标是编辑,做阈值并将新图像保存为黑白。

ds = pydicom.dcmread(filename)
png = Image.fromarray(ds.pixel_array)

I can edit by ImageMagic and save an image.我可以通过 ImageMagic 编辑并保存图像。 Next I have to replace ds.PixelData that contains an image as bytes.接下来,我必须将包含图像的 ds.PixelData 替换为字节。 That is why I did:这就是我这样做的原因:

imgAsBytes = png.tobytes()
ds.PixelData = imgAsBytes
plt.imshow(ds.pixel_array , cmap=plt.cm.bone)

Here I received an error:在这里我收到一个错误:

The length of the pixel data in the dataset (237568 bytes) doesn't match the expected length (475136 bytes).数据集中像素数据的长度(237568 字节)与预期长度(475136 字节)不匹配。 The dataset may be corrupted or there may be an issue with the pixel data handler.数据集可能已损坏或像素数据处理程序可能存在问题。

So I added:所以我补充说:

imgAsByte+= img.tobytes()

Result is:结果是:

在此处输入图像描述

The image in.dcm is four times duplicated and isn't white and black. .dcm 中的图像被复制了四次,不是黑白的。 Why my image saved as dicom has diffrent from.png?为什么我保存为 dicom 的图像与 from.png 不同? What I tried to resolve this issue:我试图解决这个问题的方法:

Info abount the original image in.dcm:关于.dcm中原始图像的信息:

  • len(ds.PixelData) = 475136 len(ds.PixelData) = 475136
  • len(ds.pixel_array[0]) = 464 len(ds.pixel_array[0]) = 464
  • len(ds.pixel_array) = 512 len(ds.pixel_array) = 512

Info about the image in.png before saving as.dcm:在另存为.dcm 之前,有关.png 中的图像的信息:

  • img.size = (464, 512) img.size = (464, 512)
  • img.mode = L img.mode = L
  • len(imgAsByte) * 2 = 475136 # after multipied 2 I have the same length like in an original image len(imgAsByte) * 2 = 475136 # 乘以 2 后,我的长度与原始图像相同

Most grayscale DICOM images (like MR and CT images) use 2 bytes per pixel (this can be seen in the BitsAllocated tag, that is usually 16).大多数灰度 DICOM 图像(如 MR 和 CT 图像)每个像素使用 2 个字节(这可以在BitsAllocated标记中看到,通常为 16)。 Your image manipulation seems to create 1 byte per pixel for these images - so you have to adapt the respective DICOM tags, at least BitsAllocated and BitsStored , that both have to be set to 8 in this case:您的图像操作似乎为这些图像创建了每个像素 1 个字节 - 因此您必须调整各自的 DICOM 标签,至少BitsAllocatedBitsStored ,在这种情况下,它们都必须设置为 8:

ds.BitsAllocated = 8
ds.BitsStored = 8

You may have to adapt other tags as well, depending on your data and your use case.您可能还需要调整其他标签,具体取决于您的数据和用例。 If you have compressed images (which seems not to be the case in your example) you have to compress the data yourself (this is error prone, so I wouldn't do it), or change the encoding in the DICOM image.如果您有压缩图像(在您的示例中似乎不是这种情况),您必须自己压缩数据(这很容易出错,所以我不会这样做),或者更改 DICOM 图像中的编码。

If you want to store the data back in a PACS, you have to do more to avoid writing illegal DICOM, for example generating a new SOP Instance UID, probably changing the SOP Class to Secondary Capture, change Image Type and a few other tags - this is handled in other questions on SO, so I won't go into it here.如果您想将数据存储回 PACS,您必须采取更多措施来避免写入非法 DICOM,例如生成新的 SOP Instance UID,可能将 SOP Class 更改为 Secondary Capture,更改 Image Type 和其他一些标签 -这是在 SO 的其他问题中处理的,所以我不会在这里 go 进入它。

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

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