简体   繁体   English

多个 dicom 图像到灰度

[英]multiple dicom images to grayscale

I m working on image segmentation using region based for this i need to convert my dicom files to grayscale, i have performed in one image it works good but for multiple images in gives me an error.我正在使用基于区域的区域进行图像分割,为此我需要将我的 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') 

here is my error这是我的错误

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'

or someone please refer me any link where multiple dicom images are converted into grayscale.或有人请向我推荐任何将多个 dicom 图像转换为灰度的链接。

The last line of your question implies that you are happy to consider other possibilities, so I would suggest ImageMagick which is installed on most Linux distros and is available for macOS and Windows.您问题的最后一行暗示您很乐意考虑其他可能性,因此我建议安装在大多数 Linux 发行版上并且可用于 macOS 和 Windows 的ImageMagick

So, just in Terminal, you can convert all Dicom images to greyscale, and automatically set the brightness levels to the full range, saving as PNG format, with:因此,只需在终端中,您就可以将所有 Dicom 图像转换为灰度,并自动将亮度级别设置为全范围,另存为 PNG 格式,使用:

magick mogrify -format PNG -colorspace gray -auto-level *.dcm

Or, if you want them saved as JPEGs in a directory called grayscale , use:或者,如果您希望它们以 JPEG 格式保存在名为grayscale的目录中,请使用:

mkdir grayscale
magic mogrify -format JPEG -path grayscale -colorspace gray -auto-level *.dcm

If you are using the older v6 ImageMagick , omit the word magick from the commands I showed above.如果您使用的是较旧的 v6 ImageMagick ,请从我上面显示的命令中省略magick一词。

This line is the problem:这一行是问题:

color = yaxpb(dicom_file.pixel_array[i][j],bt,wt)

dicom_file is the path to the file from the for loop, not a numpy array. dicom_file是从for循环到文件的路径,而不是 numpy 数组。 I think you want dicomfile instead of dicom_file.pixel_array我想你想要dicomfile而不是dicom_file.pixel_array

color = yaxpb(dicomfile[i][j],bt,wt)

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

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