Last active
August 28, 2020 08:19
-
-
Save shijianjian/39db03b38929c592655162549242be98 to your computer and use it in GitHub Desktop.
Extract images from Dicoms of Heidelberg OCT machine.
This file contains hidden or 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
""" | |
Image extractor for Heidelberg OCT B-SCANS. | |
Author: Jian Shi | |
Date created: 28/08/2020 | |
""" | |
from multiprocessing import Pool | |
import matplotlib.pyplot as plt | |
import pydicom | |
import os | |
import logging | |
__author__ = "Jian Shi" | |
__license__ = "MIT" | |
__version__ = "0.1" | |
__maintainer__ = "Jian Shi" | |
__email__ = "[email protected]" | |
logging.root.setLevel(logging.NOTSET) | |
def dcm_to_png(fp): | |
fn = fp.replace('.dcm', '.png') | |
# 16 bits to 8 bits | |
img = pydicom.read_file(fp).pixel_array ** (1 / 2) | |
plt.imsave(fn, 255 - img, cmap='gray') | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='Extract images from Dicoms of Heidelberg OCT machine.') | |
parser.add_argument('--folder', required=True, help='Target folder path.') | |
parser.add_argument('--process', metavar='N', type=int, default=20, | |
help='Number of processes can be used.') | |
args = parser.parse_args() | |
process = args.process | |
pt = args.folder | |
p = Pool(process) | |
fp_list = [] | |
logging.info("Reading dcms.") | |
for root, _, files in os.walk(pt): | |
for f in files: | |
if f.endswith('.dcm'): | |
fp_list.append(os.path.join(root, f)) | |
logging.info(f"Get {len(fp_list)} files. Start processing with {process} processes.") | |
res = p.imap(dcm_to_png, fp_list) | |
for i, x in enumerate(res): | |
progress = i / len(fp_list) * 100 | |
print(f'Finished {progress:.2f}%.', end='\r') | |
logging.info("Finished.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment