Created
March 31, 2023 15:47
-
-
Save rahulremanan/fd4b636ee553a03306ba1c4a1e8b9168 to your computer and use it in GitHub Desktop.
A DICOM reader helper function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_dicom(inp): | |
dicom = pydicom.dcmread(file_path) | |
image = dicom.pixel_array | |
if image.max() - image.min() > 0: | |
image = (image - image.min()) / (image.max() - image.min()) | |
if dicom.PhotometricInterpretation == 'MONOCHROME1': | |
image = 1 - image | |
image = (image * 255).astype(np.uint8) | |
image = np.nan_to_num(image) | |
image_arr[:,:,0] = image//3 | |
image_arr[:,:,1] = image//3 | |
image_arr[:,:,2] = image//3 | |
del image | |
image_arr = np.asarray(image_arr, dtype=np.uint8) | |
return image_arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment