python - 多个dicom图像到灰度
问题描述
我正在使用基于区域的图像分割为此我需要将我的 dicom 文件转换为灰度,我已经在一个图像中执行它效果很好但是对于多个图像给我一个错误。
def DicomtoRGB(dicomfile,bt,wt):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((dicomfile.shape[0], dicomfile.shape[1], 3), np.uint8)
#loops on image height and width
i=0
j=0
while i<dicomfile.shape[0]:
j=0
while j<dicomfile.shape[1]:
color = yaxpb(dicom_file.pixel_array[i][j],bt,wt) #linear transformation to be adapted
image[i][j] = (color,color,color)## same R,G, B value to obtain greyscale
j=j+1
i=i+1
return image
def yaxpb(pxvalue,bt,wt):
if pxvalue < bt:
y=0
elif pxvalue > wt:
y=255
else:
y=pxvalue*255/(wt-bt)-255*bt/(wt-bt)
return y
for filename in os.listdir(path):
dicom_file = os.path.join(path,filename)
exists = os.path.isfile(dicom_file)
print(filename)
ds = dicom.read_file(dicom_file)
dcm_sample=ds.pixel_array*128
print(type(dcm_sample))
image=DicomtoRGB(dcm_sample,bt=0,wt=1400)
print(type(image))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = rgb2gray(image)
plt.imshow(gray, cmap='gray')
这是我的错误
AttributeError Traceback (most recent call last)
<ipython-input-22-575cc8822b54> in <module>
7 print(type(dcm_sample))
8
----> 9 image=DicomtoRGB(dcm_sample,bt=0,wt=1400)
10 print(type(image))
11 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
<ipython-input-20-9c7a51460e87> in DicomtoRGB(dicomfile, bt, wt)
14 while j<dicomfile.shape[1]:
15 print(type(dicom_file))
---> 16 color = yaxpb(dicom_file.pxvalue[i][j],bt,wt) #linear transformation to be adapted
17 image[i][j] = (color,color,color)## same R,G, B value to obtain greyscale
18 j=j+1
AttributeError: 'str' object has no attribute 'pixel_array'
或者有人请向我推荐任何将多个 dicom 图像转换为灰度的链接。
解决方案
你问题的最后一行暗示你很乐意考虑其他可能性,所以我建议ImageMagick安装在大多数 Linux 发行版上,可用于 macOS 和 Windows。
因此,只需在终端中,您就可以将所有 Dicom 图像转换为灰度,并自动将亮度级别设置为全范围,保存为 PNG 格式,其中:
magick mogrify -format PNG -colorspace gray -auto-level *.dcm
或者,如果您希望它们在名为 的目录中保存为 JPEG grayscale
,请使用:
mkdir grayscale
magic mogrify -format JPEG -path grayscale -colorspace gray -auto-level *.dcm
如果您使用的是较旧的 v6 ImageMagick,请省略magick
我上面显示的命令中的单词。
推荐阅读
- php - 数组中的查找键在存在时找不到文本
- go - 如何在 Go/Golang 中使用 map[string]string 或自定义结构?
- docker - docker-compose:找不到命令詹金斯管道
- android - 如何使用子片段管理 Backstack?
- single-sign-on - 使用 Google-Account 登录 Alfresco
- javascript - 在 Grape Js 中为子元素添加属性?
- linux - 在 Linux 系统调用序列中区分 execve() 的解释器
- swift - Swift 4 正则表达式自定义验证
- c# - 针对以“flashplayer”或“flashplayer_X_sa”开头的所有进程名称
- r - 时空中的随机点