{"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":"markdown","source":"# *In this notebook, I will convert the dicom file image to a png image and save it in 2 folders left, right*","metadata":{}},{"cell_type":"markdown","source":"# **Check data**","metadata":{}},{"cell_type":"markdown","source":"To avoid the error of missing data when putting in 2 directories, we will run the code below","metadata":{}},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg\n!pip install -U pylibjpeg-libjpeg -v\n!pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg\n!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:05.771819Z","iopub.execute_input":"2023-01-05T13:59:05.772982Z","iopub.status.idle":"2023-01-05T13:59:52.798827Z","shell.execute_reply.started":"2023-01-05T13:59:05.772927Z","shell.execute_reply":"2023-01-05T13:59:52.797143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import Lib","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport random\nimport cv2\nimport pydicom\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-05T13:59:52.802403Z","iopub.execute_input":"2023-01-05T13:59:52.802979Z","iopub.status.idle":"2023-01-05T13:59:52.811126Z","shell.execute_reply.started":"2023-01-05T13:59:52.802924Z","shell.execute_reply":"2023-01-05T13:59:52.809991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get path","metadata":{}},{"cell_type":"code","source":"path_sample = '/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv'\npath_test = '/kaggle/input/rsna-breast-cancer-detection/test.csv'\npath_train = '/kaggle/input/rsna-breast-cancer-detection/train.csv'","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:52.812238Z","iopub.execute_input":"2023-01-05T13:59:52.812621Z","iopub.status.idle":"2023-01-05T13:59:52.827522Z","shell.execute_reply.started":"2023-01-05T13:59:52.812588Z","shell.execute_reply":"2023-01-05T13:59:52.826233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read files","metadata":{}},{"cell_type":"code","source":"df_sample_submission = pd.read_csv(path_sample)\ndf_test = pd.read_csv(path_test)\ndf_train = pd.read_csv(path_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:52.830745Z","iopub.execute_input":"2023-01-05T13:59:52.831193Z","iopub.status.idle":"2023-01-05T13:59:52.916161Z","shell.execute_reply.started":"2023-01-05T13:59:52.831156Z","shell.execute_reply":"2023-01-05T13:59:52.914843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creater folders","metadata":{}},{"cell_type":"code","source":"!mkdir left_img\n!mkdir right_img","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:52.918064Z","iopub.execute_input":"2023-01-05T13:59:52.918439Z","iopub.status.idle":"2023-01-05T13:59:55.10235Z","shell.execute_reply.started":"2023-01-05T13:59:52.918406Z","shell.execute_reply":"2023-01-05T13:59:55.100582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dicom -> png and save\n","metadata":{}},{"cell_type":"code","source":"RESIZE_TO = (512, 512)\ndef DicomtoPng(index):\n    path_img = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\n    path_img += str(df_train['patient_id'][index])\n    path_img += '/'\n    path_img += str(df_train['image_id'][index])\n    path_img += '.dcm'\n    \n    print(path_img)\n    \n    im = pydicom.dcmread(path_img)\n#  Old type\n#     im = im.pixel_array.astype(float)\n#     rescaled_image = (np.maximum(im, 0) / im.max())*255\n#     final_image = np.uint8(rescaled_image)\n#     final_image = Image.fromarray(final_image) # Creater image from an array\n\n# New type\n    data = im.pixel_array\n    data = (data - data.min()) / (data.max() - data.min())\n    if im.PhotometricInterpretation == \"MONOCHROME1\":\n        data = 1 - data\n    data = cv2.resize(data, RESIZE_TO)\n    data = (data * 255).astype(np.uint8)\n    final_image = Image.fromarray(data) # Creater image from an array\n    \n    if df_train['laterality'][index] == 'L':\n        path_save = '/kaggle/working/left_img' + '/test_img' + str(index) + '.png'\n        final_image.save(path_save)\n    else:\n        path_save = '/kaggle/working/right_img' + '/test_img' + str(index) + '.png'\n        final_image.save(path_save)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:55.105231Z","iopub.execute_input":"2023-01-05T13:59:55.10563Z","iopub.status.idle":"2023-01-05T13:59:55.117898Z","shell.execute_reply.started":"2023-01-05T13:59:55.105594Z","shell.execute_reply":"2023-01-05T13:59:55.116522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:55.119716Z","iopub.execute_input":"2023-01-05T13:59:55.120184Z","iopub.status.idle":"2023-01-05T13:59:55.154962Z","shell.execute_reply.started":"2023-01-05T13:59:55.120149Z","shell.execute_reply":"2023-01-05T13:59:55.154091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test 50 pic","metadata":{}},{"cell_type":"code","source":"numberofpic = 50\nindex = 0\n#number = list(df_train['laterality'])\nfor id in df_train['laterality']:\n    DicomtoPng(index)\n    index += 1\n    numberofpic -= 1\n    if numberofpic == 0:\n        break\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-05T13:59:55.156317Z","iopub.execute_input":"2023-01-05T13:59:55.156616Z","iopub.status.idle":"2023-01-05T14:00:51.886229Z","shell.execute_reply.started":"2023-01-05T13:59:55.156588Z","shell.execute_reply":"2023-01-05T14:00:51.885028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test ","metadata":{}},{"cell_type":"code","source":"a=cv2.imread('/kaggle/working/left_img/test_img829.png')","metadata":{"execution":{"iopub.status.busy":"2023-01-05T14:01:40.280195Z","iopub.execute_input":"2023-01-05T14:01:40.281304Z","iopub.status.idle":"2023-01-05T14:01:40.294471Z","shell.execute_reply.started":"2023-01-05T14:01:40.281257Z","shell.execute_reply":"2023-01-05T14:01:40.293286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(a)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-05T14:01:49.317261Z","iopub.execute_input":"2023-01-05T14:01:49.317925Z","iopub.status.idle":"2023-01-05T14:01:49.598524Z","shell.execute_reply.started":"2023-01-05T14:01:49.317888Z","shell.execute_reply":"2023-01-05T14:01:49.597494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}