{"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":"# Fragment Height\nWorking backwards form the top, find the first value above a certain threshold - this is the \"height\"\nSome letters are visible in the 3D relief.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport gc\nimport glob\nimport json\nfrom pathlib import Path\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport PIL.Image as Image\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-10T14:23:08.408378Z","iopub.execute_input":"2023-04-10T14:23:08.408977Z","iopub.status.idle":"2023-04-10T14:23:08.448889Z","shell.execute_reply.started":"2023-04-10T14:23:08.408913Z","shell.execute_reply":"2023-04-10T14:23:08.447106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = Path(\"/kaggle/input/vesuvius-challenge-ink-detection\")\ntrain_path = base_path / \"train\"\ntrain_fragments = sorted([train_path  / f.name for f in train_path.iterdir()])\ntest_path = base_path / \"test\"\ntest_fragments = sorted([test_path  / f.name for f in test_path.iterdir()])\n\nallFragments = train_fragments + test_fragments\n\nallFragments","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:23:08.451428Z","iopub.execute_input":"2023-04-10T14:23:08.451896Z","iopub.status.idle":"2023-04-10T14:23:08.468761Z","shell.execute_reply.started":"2023-04-10T14:23:08.451850Z","shell.execute_reply":"2023-04-10T14:23:08.467539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ScanData:\n    def __init__(self, baseDir):\n        self.dir = baseDir.resolve()\n        maskName = str(baseDir / \"mask.png\")\n        mask = np.array(Image.open(maskName).convert(\"1\"))        \n        self.mask = mask\n        (self.h,self.w) = mask.shape\n\n        labelName = str(baseDir / \"inklabels.png\" )\n        try:\n            self.label = np.array(Image.open(labelName).convert(\"1\"))        \n        except:\n            self.label = None\n\n        self.sliceNames = sorted( (baseDir / \"surface_volume\").rglob(\"*.tif\") )\n        dim = len(self.sliceNames)\n        self.dim = dim\n        self.height = None\n            \n    def get_height(self, thresh=28125):\n        if self.height is None:\n            img = np.zeros( (self.h, self.w, self.dim + 1), dtype=bool)\n\n            for idx, filename in enumerate(tqdm(self.sliceNames)):\n                fname = str(filename)\n                # Load in reverse order starting from 1 while dim[0] stays zero so we can tell\n                #  the difference between all 0 and all 1\n                img[:,:,self.dim-idx] = np.array(Image.open(fname)) > thresh\n            # Argmax will find he first 1\n            top = np.argmax(img,axis=-1)\n            # in the case of no matches, argmax will be zero\n            height = (self.dim-top) * self.mask * (top>0)\n            self.height = height.astype(int)\n        return self.height\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:23:08.471107Z","iopub.execute_input":"2023-04-10T14:23:08.471913Z","iopub.status.idle":"2023-04-10T14:23:08.484042Z","shell.execute_reply.started":"2023-04-10T14:23:08.471866Z","shell.execute_reply":"2023-04-10T14:23:08.482703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"allScans = [ScanData(f) for f in allFragments]","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:23:08.487182Z","iopub.execute_input":"2023-04-10T14:23:08.488160Z","iopub.status.idle":"2023-04-10T14:23:11.807665Z","shell.execute_reply.started":"2023-04-10T14:23:08.488065Z","shell.execute_reply":"2023-04-10T14:23:11.806027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scan = allScans[0]\nheight = scan.get_height()\nplt.imshow(height, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:23:11.809531Z","iopub.execute_input":"2023-04-10T14:23:11.809974Z","iopub.status.idle":"2023-04-10T14:25:28.122512Z","shell.execute_reply.started":"2023-04-10T14:23:11.809915Z","shell.execute_reply":"2023-04-10T14:25:28.120938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(1,2, figsize=(20,10))\nfor a in ax: a.axis('off')\nax[0].imshow(scan.height[3500:4500, 1000:2000],cmap='gray')\nax[1].imshow(scan.label[3500:4500, 1000:2000] )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:25:28.124468Z","iopub.execute_input":"2023-04-10T14:25:28.126028Z","iopub.status.idle":"2023-04-10T14:25:28.789804Z","shell.execute_reply.started":"2023-04-10T14:25:28.125917Z","shell.execute_reply":"2023-04-10T14:25:28.788056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for scan in allScans:\n    height = scan.get_height()\n    plt.figure(figsize=(16,16))\n    plt.imshow(height, cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T14:25:28.791726Z","iopub.execute_input":"2023-04-10T14:25:28.792207Z","iopub.status.idle":"2023-04-10T14:36:42.327579Z","shell.execute_reply.started":"2023-04-10T14:25:28.792158Z","shell.execute_reply":"2023-04-10T14:36:42.326013Z"},"trusted":true},"execution_count":null,"outputs":[]}]}