Python convert .dcm to .png, images are too bright
Asked Answered
N

4

10

I have to convert some files which come by default as .dcm to .png, I've found some code samples to achieve that around here but the end results are too bright. Could anybody have a look at this, please?

 def convert_to_png(file):
    ds = pydicom.dcmread(file)

    shape = ds.pixel_array.shape

    # Convert to float to avoid overflow or underflow losses.
    image_2d = ds.pixel_array.astype(float)

    # Rescaling grey scale between 0-255
    image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

    # Convert to uint
    image_2d_scaled = np.uint8(image_2d_scaled)

    # Write the PNG file
    with open(f'{file.strip(".dcm")}.png', 'wb') as png_file:
        w = png.Writer(shape[1], shape[0], greyscale=True)
        w.write(png_file, image_2d_scaled)

I've tweaked around the code but nothing seems to work.

This is how the actual thing looks like as dicom and on the right side is the result of running this code enter image description here

Nikaniki answered 14/2, 2020 at 3:34 Comment(0)
A
17

Some DICOM datasets require window center/width rescaling of the original pixel intensities (via the (0028,1050) Window Center and (0028,1051) Window Width elements in the VOI LUT Module) in order to reproduce the way they were "viewed".

pydicom has a function apply_voi_lut() for applying this windowing:

from pydicom import dcmread
from pydicom.pixel_data_handlers.util import apply_voi_lut

ds = dcmread(file)
if 'WindowWidth' in ds:
    print('Dataset has windowing')

windowed = apply_voi_lut(ds.pixel_array, ds)

# Add code for rescaling to 8-bit...

Depending on the dataset type you may need to use apply_modality_lut() beforehand.

Asbestos answered 14/2, 2020 at 5:30 Comment(1)
FWIW, MRs generated by Phillips regularly have a modality LUT as far as I remember, so it may be worth to use that, too.Epiphragm
U
1

A .dcm image appears to have a range of both brightness and contrast when analyzing a particular image. The reason it may look a bit bright in your case is that you only selected a particular view of the image.

To make the image darker, it looks like you'd just need to increase your denominator value:

threshold = 500 # Adjust as needed
image_2d_scaled = (np.maximum(image_2d, 0) / (np.amax(image_2d) + threshold)) * 255.0

This would ensure that some pixels aren't glaring bright.

Unmitigated answered 14/2, 2020 at 4:50 Comment(0)
R
0

Well I too had the same problem, you can use exposure.equalize_adapthist() from Scikit Image library.

filename = "sample.dcm"
ds = pydicom.read_file(filename)
image = ds.pixel_array
image = exposure.equalize_adapthist(image)

cv2.imshow("dicom", image)
cv2.waitKey(0)
Rejoinder answered 22/11, 2020 at 15:18 Comment(0)
O
0

you can try below method

image = image - np.min(image)
image = (image/np.max(image))*255

In this methods you lose minimum information while converting from dicom to png/jpg

Oily answered 10/9, 2021 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.