简体   繁体   中英

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.

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.

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.

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:

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

Or, if you want them saved as JPEGs in a directory called grayscale , use:

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.

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. I think you want dicomfile instead of dicom_file.pixel_array

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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