同时使用Python将dicom转换为png,没有得到任何错误。输出文件是完整的黑色,尽管图像数组具有变量值。
代码:
import pydicom
import cv2
import os
dicom = pydicom.dcmread(inputdir + f)
# print("dicom", dicom)
img = dicom.pixel_array
print("img", img)
cv2.imwrite(outdir + f.replace('.dcm','.png'),img)
镜像数组(供参考):
[[585 585 570 ... 570 572 570]
[585 585 585 ... 572 575 572]
[585 585 585 ... 553 568 575]
...
[854 854 854 ... 778 783 787]
[854 854 856 ... 783 785 787]
[854 856 856 ... 785 790 759]]
发布于 2021-07-28 17:11:31
根据docs.opencv.org的说法,cv2.imwrite通常“首选”图像数据为8位表示形式(即,值的范围仅为0到255 )。
在一般情况下,只有8位单通道或3通道(具有'BGR‘通道顺序)图像可以保存使用此功能...
我注意到您的图像数据超过8位,因此您需要(a)缩放它然后将其转换为np.uint8
,或者(b)将位表示减少到8位。
(a)缩放的示例:
import numpy as np # assuming that you have numpy installed
img = np.array(img, dtype = float)
img = (img - img.min()) / (img.max() - img.min()) * 255.0
img = img.astype(np.uint8)
cv2.imwrite(outdir + f.replace('.dcm','.png'),img)
或者,(b)位移位到8位的例子:
bit_depth = 10 # assuming you know the bit representation
img = np.array(img, dtype = np.uint16)
img = img >> (bit_depth - 8)
img = img.astype(np.uint8)
cv2.imwrite(outdir + f.replace('.dcm','.png'),img)
但是既然你是用PNG格式保存你的图片,这里有一个更简单的方法...
,但有以下例外:
格式中
您可以将您的图像转换为np.uint16
:)
img = img.astype(np.uint16)
cv2.imwrite(outdir + f.replace('.dcm','.png'),img)
https://stackoverflow.com/questions/68556710
复制相似问题