{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-12T08:22:57.806923Z","iopub.execute_input":"2023-01-12T08:22:57.807415Z","iopub.status.idle":"2023-01-12T08:23:13.800994Z","shell.execute_reply.started":"2023-01-12T08:22:57.807324Z","shell.execute_reply":"2023-01-12T08:23:13.79983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2023-01-12T08:23:13.804025Z","iopub.execute_input":"2023-01-12T08:23:13.804538Z","iopub.status.idle":"2023-01-12T08:23:14.930758Z","shell.execute_reply.started":"2023-01-12T08:23:13.80448Z","shell.execute_reply":"2023-01-12T08:23:14.929757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example : Do it for one image\n\npatient = '10006'\nimage = '1459541791'\n\nsize = 512\nimage_input_path = '/kaggle/input/rsna-breast-cancer-detection/train_images' + \\\n            '/' + patient + '/'+ image + '.dcm'\n\noutput_path =  '/kaggle/working/output/rsna_pngs/train_images'\n\n# Create directory if it does not exists\nif not os.path.isdir(output_path+'/'+patient):\n    os.makedirs(output_path+'/'+patient)\n\ndicom_image = pydicom.dcmread(image_input_path)\nimage_array = dicom_image.pixel_array\n\n# fig, ax = plt.subplots()\n# ax.imshow(image_array)\n# ax.set_title('image_dcm')\n# plt.show()\n\nscaled_img = (np.maximum(image_array,0) / image_array.max()) * 255.0\n\nif dicom_image.PhotometricInterpretation == \"MONOCHROME1\":\n    scaled_img = 1 - scaled_img\n    \nimg = scaled_img.astype(np.uint8)\nresized_image = cv2.resize(img, (size, size))\ncv2.imwrite(output_path + '/' + patient + '/' + image +'.png',resized_image)\n\n\n# Plot png image\n#img = mpimg.imread(output_path + '/' + patient + '/' + image +'.png')\n#imgplot = plt.imshow(img)\n#plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T08:23:14.932449Z","iopub.execute_input":"2023-01-12T08:23:14.932817Z","iopub.status.idle":"2023-01-12T08:23:16.963196Z","shell.execute_reply.started":"2023-01-12T08:23:14.932782Z","shell.execute_reply":"2023-01-12T08:23:16.961953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert(patient, size, input_path, output_path):\n    patient_input_path = '/kaggle/input/rsna-breast-cancer-detection/train_images' + \\\n            '/' + patient\n    # Create directory if it does not exists\n    if not os.path.isdir(output_path+'/'+patient):\n        os.makedirs(output_path+'/'+patient)\n    \n    for image in os.listdir(patient_input_path):\n        image_input_path = patient_input_path + '/'+ image\n        dicom_image = pydicom.dcmread(image_input_path)\n        image_array = dicom_image.pixel_array\n        \n        scaled_img = (np.maximum(image_array,0) / image_array.max()) * 255.0\n        if dicom_image.PhotometricInterpretation == \"MONOCHROME1\":\n            scaled_img = 1 - scaled_img\n        img = scaled_img.astype(np.uint8)\n        \n        resized_image = cv2.resize(img, (size, size))\n    \n        # write png image\n        cv2.imwrite(output_path + '/' + patient + '/' + image.replace('.dcm', '.png'),resized_image) \n    \n        #Check if png file exists\n        #print(os.path.isfile(output_path + '/' + patient + '/' + image.replace('.dcm', '.png')))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T08:23:16.965268Z","iopub.execute_input":"2023-01-12T08:23:16.965755Z","iopub.status.idle":"2023-01-12T08:23:16.97541Z","shell.execute_reply.started":"2023-01-12T08:23:16.96571Z","shell.execute_reply":"2023-01-12T08:23:16.973834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image size \nsize = 512\n\n# Input path\ninput_path = '/kaggle/input/rsna-breast-cancer-detection/train_images'\n\n# Output path\noutput_path =  '/kaggle/working/output/rsna_pngs/train_images'\n\n# Parallel loop\n_ = Parallel(n_jobs=4)(\n    delayed(convert)(patient, size, input_path=input_path, output_path=output_path)\n    for patient in tqdm(os.listdir(input_path))\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T08:23:16.978484Z","iopub.execute_input":"2023-01-12T08:23:16.978983Z","iopub.status.idle":"2023-01-12T08:23:31.311938Z","shell.execute_reply.started":"2023-01-12T08:23:16.978937Z","shell.execute_reply":"2023-01-12T08:23:31.309967Z"},"trusted":true},"execution_count":null,"outputs":[]}]}