Working with dicom image
eri820503 opened this issue · comments
eri820503 commented
Hi,
I want to try my dicom image(mammogram) to the model. But I can't find the procedure to transfer dicom image to png file.
I try some procedure to produce the png file:
First, I load dicom image as an array and use Histogram Equalization
algorithm(in open cv) to make the contrast as expected.
Then, I save the array as png file with int32 byte.
Finally, I run your code without any error, but I'm not sure the result is correct or not?
Thanks for your contribution!
Jungkyu (JP) Park commented
The way we save png files from dicom images is as shown in the README file:
import png
import pydicom
def save_dicom_image_as_png(dicom_filename, png_filename, bitdepth=12):
"""
Save 12-bit mammogram from dicom as rescaled 16-bit png file.
:param dicom_filename: path to input dicom file.
:param png_filename: path to output png file.
:param bitdepth: bit depth of the input image. Set it to 12 for 12-bit mammograms.
"""
image = pydicom.read_file(dicom_filename).pixel_array
with open(png_filename, 'wb') as f:
writer = png.Writer(height=image.shape[0], width=image.shape[1], bitdepth=bitdepth, greyscale=True)
writer.write(f, image.tolist())