{"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 filters","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:21.650958Z","iopub.execute_input":"2023-01-18T17:07:21.651489Z","iopub.status.idle":"2023-01-18T17:07:33.88306Z","shell.execute_reply.started":"2023-01-18T17:07:21.651449Z","shell.execute_reply":"2023-01-18T17:07:33.88162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys, tarfile\nfrom torchvision.io import read_image\nimport matplotlib.pyplot as plt\nimport pydicom\nimport torch\nimport numpy as np\nfrom skimage.io import imread\nfrom skimage.draw import polygon\nimport numpy as np\nimport plistlib\nimport cv2 \nimport scipy\nfrom skimage import filters\nfrom collections import Counter\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:33.885212Z","iopub.execute_input":"2023-01-18T17:07:33.885621Z","iopub.status.idle":"2023-01-18T17:07:33.901583Z","shell.execute_reply.started":"2023-01-18T17:07:33.885587Z","shell.execute_reply":"2023-01-18T17:07:33.899652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:33.903808Z","iopub.execute_input":"2023-01-18T17:07:33.904308Z","iopub.status.idle":"2023-01-18T17:07:33.912059Z","shell.execute_reply.started":"2023-01-18T17:07:33.904269Z","shell.execute_reply":"2023-01-18T17:07:33.910432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir 10008\nmkdir a","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:33.915206Z","iopub.execute_input":"2023-01-18T17:07:33.915702Z","iopub.status.idle":"2023-01-18T17:07:35.094339Z","shell.execute_reply.started":"2023-01-18T17:07:33.915652Z","shell.execute_reply":"2023-01-18T17:07:35.092762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/rsna-breast-cancer-detection/test_images/10008/1591370361.dcm","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:35.097461Z","iopub.execute_input":"2023-01-18T17:07:35.098684Z","iopub.status.idle":"2023-01-18T17:07:35.106822Z","shell.execute_reply.started":"2023-01-18T17:07:35.098537Z","shell.execute_reply":"2023-01-18T17:07:35.105097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\nlst_path=[str(df['patient_id'][i]) + '/' + str(df['image_id'][i]) + '.dcm' for i in range(len(df))]\nlst_path","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:35.109492Z","iopub.execute_input":"2023-01-18T17:07:35.110033Z","iopub.status.idle":"2023-01-18T17:07:35.136272Z","shell.execute_reply.started":"2023-01-18T17:07:35.109984Z","shell.execute_reply":"2023-01-18T17:07:35.13462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_masks_and_sizes_of_connected_components(img_mask):\n    \"\"\"\n    Finds the connected components from the mask of the image\n    \"\"\"\n    mask, num_labels = scipy.ndimage.label(img_mask)\n\n    mask_pixels_dict = {}\n    for i in range(num_labels+1):\n        this_mask = (mask == i)\n        if img_mask[this_mask][0] != 0:\n            # Exclude the 0-valued mask\n            mask_pixels_dict[i] = np.sum(this_mask)\n        \n    return mask, mask_pixels_dict\n\n\ndef get_mask_of_largest_connected_component(img_mask):\n    \"\"\"\n    Finds the largest connected component from the mask of the image\n    \"\"\"\n    mask, mask_pixels_dict = get_masks_and_sizes_of_connected_components(img_mask)\n    largest_mask_index = pd.Series(mask_pixels_dict).idxmax()\n    largest_mask = mask == largest_mask_index\n    return largest_mask\ndef super_procescing(img):\n    clahe_img = np.copy(img)\n    threshold = filters.threshold_isodata(clahe_img)\n    clahe = cv2.createCLAHE(clipLimit =threshold)\n    clahe_img = clahe.apply(clahe_img) -(threshold+3)\n    bin_img = (clahe_img > threshold)\n    kernel = np.ones((5, 5), np.uint8)\n    bin_img = bin_img.astype('uint8')\n    bin_img = cv2.erode(bin_img, kernel)\n    zeros=np.zeros(bin_img.shape)\n    bin_img=(bin_img==zeros)\n    img_mask = get_mask_of_largest_connected_component(bin_img)\n    #crop_image\n\n    farest_pixel = np.max(list(zip(*np.where(img_mask == 1))), axis=0)\n    nearest_pixel = np.min(list(zip(*np.where(img_mask == 1))), axis=0)\n    croped =  img[nearest_pixel[0]:farest_pixel[0], nearest_pixel[1]:farest_pixel[1]]\n    return(croped)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:07:35.138226Z","iopub.execute_input":"2023-01-18T17:07:35.139349Z","iopub.status.idle":"2023-01-18T17:07:35.456908Z","shell.execute_reply.started":"2023-01-18T17:07:35.139291Z","shell.execute_reply":"2023-01-18T17:07:35.455406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dcm_to_png(dcm_image):\n    image = dcm_image - np.min(dcm_image)\n    image = (image/np.max(image))*255\n    return image\n\ndef convert_folder(lst_path):\n    for path in lst_path:\n        img = cv2.imread('/kaggle/working/a'+path.replace('dcm', 'png').split('/')[1], 0)\n        name = path.split('.')[0]\n        img_crop = super_procescing(img)\n#         img_crop = dcm_to_png(img)\n#         plt.imshow(img_crop)\n        cv2.imwrite('/kaggle/working/'+name+'.png', img_crop)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:09:22.439407Z","iopub.execute_input":"2023-01-18T17:09:22.439968Z","iopub.status.idle":"2023-01-18T17:09:22.449916Z","shell.execute_reply.started":"2023-01-18T17:09:22.439934Z","shell.execute_reply":"2023-01-18T17:09:22.448721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convert_folder(tqdm(lst_path))","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:09:22.67626Z","iopub.execute_input":"2023-01-18T17:09:22.676798Z","iopub.status.idle":"2023-01-18T17:10:20.499953Z","shell.execute_reply.started":"2023-01-18T17:09:22.676756Z","shell.execute_reply":"2023-01-18T17:10:20.498176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls /kaggle/working/'10008'","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:10:20.502457Z","iopub.execute_input":"2023-01-18T17:10:20.503661Z","iopub.status.idle":"2023-01-18T17:10:21.617522Z","shell.execute_reply.started":"2023-01-18T17:10:20.503592Z","shell.execute_reply":"2023-01-18T17:10:21.616026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/10008/361203119.png\",0)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T17:10:21.619848Z","iopub.execute_input":"2023-01-18T17:10:21.621478Z","iopub.status.idle":"2023-01-18T17:10:22.431646Z","shell.execute_reply.started":"2023-01-18T17:10:21.621411Z","shell.execute_reply":"2023-01-18T17:10:22.429932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}