{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport pydicom\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BASE_PATH = '../input/osic-pulmonary-fibrosis-progression-lungs-mask/mask_clear/mask_clear/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mask = np.zeros((1024, 1024))\ncount = 0\nfor k in tqdm(os.listdir(BASE_PATH)):\n    ldir = os.listdir(os.path.join(BASE_PATH, k))\n    norm = max([int(p[:-4]) for p in os.listdir(os.path.join(BASE_PATH, k))])\n    for p in ldir:\n        coeff = int(p[:-4]) / norm\n        if coeff > 0.2 and coeff < 0.8:\n            mask += cv2.resize(cv2.imread(os.path.join(BASE_PATH, k, p), 0), (1024, 1024)) / 255.\n            count += 1\nmask = mask / count","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"maskap = cv2.bilateralFilter(mask.astype('float32'),9,25,25)\nmaskap = (maskap - maskap.min()) / (maskap.max() - maskap.min())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('mask'):\n    os.mkdir('mask') ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Asymetric Mask","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(maskap)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(maskap > 0.35);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"maska = (maskap > 0.35).astype('uint8') \nmaska_r, maska_l = maska.copy(), maska.copy()\n\nmaska_r[:, :512] = 0\nmaska_l[:, 512:] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(maska_r, alpha=0.5)\nplt.imshow(maska_l, alpha=0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cv2.imwrite('mask/left_mask_asymetric.jpg', maska_l)\ncv2.imwrite('mask/right_mask_asymetric.jpg', maska_r)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Symetric Mask","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"masks = (maskap + maskap[:, ::-1] +\n         maskap[::-1, ::-1] + maskap[::-1, :]) / 4.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(masks);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(masks > 0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"masks = (masks > 0.5).astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"masks_r, masks_l = masks.copy(), masks.copy()\n\nmasks_r[:, :512] = 0\nmasks_l[:, 512:] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.imshow(masks_r, alpha=0.5)\nplt.imshow(masks_l, alpha=0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cv2.imwrite('mask/left_mask_simetric.jpg', masks_l)\ncv2.imwrite('mask/right_mask_simetric.jpg', masks_r)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Save in zip","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\n\ndef zip_and_remove(path):\n    ziph = zipfile.ZipFile(f'{path}.zip', 'w', zipfile.ZIP_DEFLATED)\n    \n    for root, dirs, files in os.walk(path):\n        for file in files:\n            file_path = os.path.join(root, file)\n            ziph.write(file_path)\n            os.remove(file_path)\n    \n    ziph.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"zip_and_remove('mask') ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}