Python 将多个dicom图像转换为灰度
我正在使用基于区域的方法进行图像分割。为此,我需要将我的dicom文件转换为灰度,我在一张图像中执行了该操作,但对于中的多张图像,这会给我一个错误Python 将多个dicom图像转换为灰度,python,image-processing,jupyter-notebook,grayscale,pydicom,Python,Image Processing,Jupyter Notebook,Grayscale,Pydicom,我正在使用基于区域的方法进行图像分割。为此,我需要将我的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
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')
def DicomtoRGB(dicomfile、bt、wt):
“”“创建用RGB中的特定颜色填充的新图像(numpy数组)”
#创建黑色空白图像
image=np.zero((dicomfile.shape[0],dicomfile.shape[1],3),np.uint8)
#图像高度和宽度上的循环
i=0
j=0
而I9图像=DicomtoRGB(dcm_样本,bt=0,wt=1400)
10打印(类型(图像))
11灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
在DicomtoRGB中(dicomfile、bt、wt)
14而j 16 color=yaxpb(dicom_file.pxvalue[i][j],bt,wt)#要调整的线性变换
17图像[i][j]=(颜色、颜色、颜色)##相同的R、G、B值以获得灰度
18 j=j+1
AttributeError:“str”对象没有“像素数组”属性
或有人请指我将多个DICOM图像转换成灰度级的链接。
< P>最后一行的问题意味着你乐意考虑其他的可能性,所以我建议<强> IVimeGigi> <强> >安装在大多数Linux DistOS上,可用于Mac OS和Windows。< /P> 因此,仅在终端中,您可以将所有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>强> IMAGE(<强)>,从上面所示的命令中省略“代码> MAGICK < /代码>。
< P>你的最后一行暗示你很乐意考虑其他的可能性,因此,我建议将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
如果您使用的是较旧的v6ImageMagick,请从我上面显示的命令中省略单词
magick
。这一行就是问题所在:
color=yaxpb(dicom_文件.pixel_数组[i][j],bt,wt)
dicom_文件
是从for
循环到文件的路径,而不是numpy数组。我想你想要的是dicom文件
而不是dicom\u文件。像素数组
color=yaxpb(dicomfile[i][j],bt,wt)
这一行就是问题所在:
color=yaxpb(dicom_文件.pixel_数组[i][j],bt,wt)
dicom_文件
是从for
循环到文件的路径,而不是numpy数组。我想你想要的是dicom文件
而不是dicom\u文件。像素数组
color=yaxpb(dicomfile[i][j],bt,wt)
我不知道您的代码,但您可以使用opencv传输图像。更简单。我不知道你的代码,但是你可以使用opencv来传输图像。这更容易。