{"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\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-05-30T18:51:28.14148Z","iopub.execute_input":"2023-05-30T18:51:28.142632Z","iopub.status.idle":"2023-05-30T18:52:30.110624Z","shell.execute_reply.started":"2023-05-30T18:51:28.142532Z","shell.execute_reply":"2023-05-30T18:52:30.109333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\n\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport json\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:52:30.113271Z","iopub.execute_input":"2023-05-30T18:52:30.11445Z","iopub.status.idle":"2023-05-30T18:52:31.066148Z","shell.execute_reply.started":"2023-05-30T18:52:30.114402Z","shell.execute_reply":"2023-05-30T18:52:31.064873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\nlen(train_images)  # 54706","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:52:31.067736Z","iopub.execute_input":"2023-05-30T18:52:31.068112Z","iopub.status.idle":"2023-05-30T18:54:05.417274Z","shell.execute_reply.started":"2023-05-30T18:52:31.068078Z","shell.execute_reply":"2023-05-30T18:54:05.415804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = train_images[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:05.420778Z","iopub.execute_input":"2023-05-30T18:54:05.421912Z","iopub.status.idle":"2023-05-30T18:54:05.426914Z","shell.execute_reply.started":"2023-05-30T18:54:05.421847Z","shell.execute_reply":"2023-05-30T18:54:05.426048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom = pydicom.dcmread(f)\nimg = dicom.pixel_array\n\nimg = (img - img.min()) / (img.max() - img.min())    \nimg *= 255\nimg = np.uint8(img)\n\nif dicom.PhotometricInterpretation == \"MONOCHROME1\":\n    img = 1 - img\n\nplt.figure(figsize=(5, 5))\nplt.imshow(img, cmap=\"gray\")\nplt.title(f\"original image for {f}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:05.428321Z","iopub.execute_input":"2023-05-30T18:54:05.429139Z","iopub.status.idle":"2023-05-30T18:54:06.835808Z","shell.execute_reply.started":"2023-05-30T18:54:05.429102Z","shell.execute_reply":"2023-05-30T18:54:06.834549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bin_pixels = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY)[1]\nplt.imshow(bin_pixels, cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:06.837218Z","iopub.execute_input":"2023-05-30T18:54:06.837758Z","iopub.status.idle":"2023-05-30T18:54:07.999617Z","shell.execute_reply.started":"2023-05-30T18:54:06.837725Z","shell.execute_reply":"2023-05-30T18:54:07.998325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"contours, _ = cv2.findContours(bin_pixels, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\ncontour = max(contours, key=cv2.contourArea)\ncontour","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:08.00113Z","iopub.execute_input":"2023-05-30T18:54:08.00148Z","iopub.status.idle":"2023-05-30T18:54:08.026143Z","shell.execute_reply.started":"2023-05-30T18:54:08.001449Z","shell.execute_reply":"2023-05-30T18:54:08.024969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.zeros(img.shape, np.uint8)\nplt.imshow(mask, cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:08.028062Z","iopub.execute_input":"2023-05-30T18:54:08.028426Z","iopub.status.idle":"2023-05-30T18:54:09.115437Z","shell.execute_reply.started":"2023-05-30T18:54:08.028393Z","shell.execute_reply":"2023-05-30T18:54:09.114121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.drawContours(mask, [contour], -1, 255, cv2.FILLED)","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:09.116943Z","iopub.execute_input":"2023-05-30T18:54:09.117295Z","iopub.status.idle":"2023-05-30T18:54:09.131689Z","shell.execute_reply.started":"2023-05-30T18:54:09.117264Z","shell.execute_reply":"2023-05-30T18:54:09.130253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = cv2.bitwise_and(img, mask)\nplt.imshow(out, cmap = \"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:09.136198Z","iopub.execute_input":"2023-05-30T18:54:09.136592Z","iopub.status.idle":"2023-05-30T18:54:10.228501Z","shell.execute_reply.started":"2023-05-30T18:54:09.136556Z","shell.execute_reply":"2023-05-30T18:54:10.227136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1, y2 = np.min(contour[:, :, 1]), np.max(contour[:, :, 1])\nx1, x2 = np.min(contour[:, :, 0]), np.max(contour[:, :, 0])\n\n# x1 = int(0.99 * x1)\n# x2 = int(1.01 * x2)\n# y1 = int(0.99 * y1)\n# y2 = int(1.01 * y2)\n# 0 0 1402 2619\nprint(x1, y1, x2, y2)","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:10.231073Z","iopub.execute_input":"2023-05-30T18:54:10.231469Z","iopub.status.idle":"2023-05-30T18:54:10.240565Z","shell.execute_reply.started":"2023-05-30T18:54:10.231435Z","shell.execute_reply":"2023-05-30T18:54:10.239328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(out[y1:y2, x1:x2], cmap=\"gray\") ; ","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:10.242282Z","iopub.execute_input":"2023-05-30T18:54:10.242944Z","iopub.status.idle":"2023-05-30T18:54:10.92235Z","shell.execute_reply.started":"2023-05-30T18:54:10.242908Z","shell.execute_reply":"2023-05-30T18:54:10.921102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y1, y2 = np.min(contour[:, :, 1]), np.max(contour[:, :, 1])\n# x1, x2 = np.min(contour[:, :, 0]), np.max(contour[:, :, 0])\n\nx1 = int(0.99 * x1)\nx2 = int(1.01 * x2)\ny1 = int(0.99 * y1)\ny2 = int(1.01 * y2)\nprint(x1, y1, x2, y2)","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:10.924542Z","iopub.execute_input":"2023-05-30T18:54:10.925049Z","iopub.status.idle":"2023-05-30T18:54:10.93249Z","shell.execute_reply.started":"2023-05-30T18:54:10.925002Z","shell.execute_reply":"2023-05-30T18:54:10.931584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(out[y1:y2, x1:x2], cmap=\"gray\") ;","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:10.933796Z","iopub.execute_input":"2023-05-30T18:54:10.934844Z","iopub.status.idle":"2023-05-30T18:54:11.653484Z","shell.execute_reply.started":"2023-05-30T18:54:10.934807Z","shell.execute_reply":"2023-05-30T18:54:11.652231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resize = out[y1:y2, x1:x2]","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:11.655121Z","iopub.execute_input":"2023-05-30T18:54:11.65632Z","iopub.status.idle":"2023-05-30T18:54:11.662244Z","shell.execute_reply.started":"2023-05-30T18:54:11.656275Z","shell.execute_reply":"2023-05-30T18:54:11.660878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_image = cv2.resize(img_resize, (512, 512))\nplt.imshow(resized_image, cmap = \"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-05-30T18:54:45.645897Z","iopub.execute_input":"2023-05-30T18:54:45.647143Z","iopub.status.idle":"2023-05-30T18:54:45.954809Z","shell.execute_reply.started":"2023-05-30T18:54:45.647031Z","shell.execute_reply":"2023-05-30T18:54:45.953386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}