{"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":"This helper notebook is a first step in the process of realigning CT scan data in order to get similar papyrus data on same layers of voxel space. Ideally this should help to move from 3d space to 2d space (with few channels) in CNN models which is significantly less computationally expensive.","metadata":{"papermill":{"duration":0.004278,"end_time":"2023-05-17T05:47:32.879468","exception":false,"start_time":"2023-05-17T05:47:32.87519","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nimport glob\nimport numpy as np\nimport PIL.Image as Image\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom scipy.ndimage import distance_transform_edt, gaussian_filter\nimport tifffile\n\nIN_PREFIX = '/kaggle/input/vesuvius-challenge-ink-detection/train/'\nOUT_PREFIX = '/kaggle/working/train/'\nSIGMA = 200\n","metadata":{"papermill":{"duration":3.062464,"end_time":"2023-05-17T05:47:35.945878","exception":false,"start_time":"2023-05-17T05:47:32.883414","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-28T21:27:30.826867Z","iopub.execute_input":"2023-05-28T21:27:30.827649Z","iopub.status.idle":"2023-05-28T21:27:30.834504Z","shell.execute_reply.started":"2023-05-28T21:27:30.827603Z","shell.execute_reply":"2023-05-28T21:27:30.833096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for sample in ['2/']:\n    mask = np.array(Image.open(f'{IN_PREFIX}{sample}mask.png').convert('1'))\n    inklabels = (np.array(Image.open(f'{IN_PREFIX}{sample}inklabels.png')) > 0).astype(np.float32)\n\n    # Weight matrix that can be used in loss/fitness calculations\n    # Pixels that are closer to labelled pixels receive higher (but still negative) weights.\n    #\n    # Weights are in range (-1,1], where:\n    # 0 - masked area,\n    # 1 - pixels labelled as ink\n    # <0 - pixels not labelled as ink\n    #\n    # Both mask and inklabels can be inferred from weight matrix as follows:\n    # mask = weight == 0\n    # inklabels = weight == 1\n    #\n    distance_to_ink = distance_transform_edt(inklabels == 0.)\n    fitness_weight = 2. / (1. + distance_to_ink)**2 - 1.\n    fitness_weight *= mask\n\n    plt.imshow(fitness_weight,cmap=\"gray\")\n    plt.show()\n\n    # Create output folder structure if does not exist\n    if not os.path.exists(f'{OUT_PREFIX}{sample}light/'):\n        os.makedirs(f'{OUT_PREFIX}{sample}light/')\n    \n    # Save weights\n    tifffile.imwrite(f'{OUT_PREFIX}{sample}weight.tif', fitness_weight)\n\n    # Weight of pixels with data\n    W = np.maximum(gaussian_filter(1.*mask, SIGMA), 1e-9)\n\n    for filename in tqdm(sorted(glob.glob(f'{IN_PREFIX}{sample}surface_volume/*.tif'))):\n        image = np.array(Image.open(filename))\n\n        # wide gausian filter to leave only average brightness\n        light = np.where(mask, gaussian_filter(image, SIGMA) / np.maximum(W, 1e-9), 0)\n        light = np.around(light).astype(np.uint16)\n\n        # Save light map\n        filename = filename.split('/')[-1]\n        tifffile.imwrite(f'{OUT_PREFIX}{sample}light/{filename}', light)\n        \n        # Verify saved file\n        light = tifffile.imread(f'{OUT_PREFIX}{sample}light/{filename}')\n        plt.imshow(light,cmap=\"gray\")\n        plt.show()","metadata":{"papermill":{"duration":1.169647,"end_time":"2023-05-17T05:47:37.119688","exception":false,"start_time":"2023-05-17T05:47:35.950041","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-28T21:27:30.836424Z","iopub.execute_input":"2023-05-28T21:27:30.836842Z","iopub.status.idle":"2023-05-28T21:35:59.730825Z","shell.execute_reply.started":"2023-05-28T21:27:30.836811Z","shell.execute_reply":"2023-05-28T21:35:59.729238Z"},"trusted":true},"execution_count":null,"outputs":[]}]}