{"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":"# Ink Detection Surface Volume Slide Show","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:51:28.112971Z","iopub.execute_input":"2023-03-18T15:51:28.113364Z","iopub.status.idle":"2023-03-18T15:51:28.347110Z","shell.execute_reply.started":"2023-03-18T15:51:28.113328Z","shell.execute_reply":"2023-03-18T15:51:28.345838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train #1","metadata":{}},{"cell_type":"code","source":"paths=[]\ntpaths=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/vesuvius-challenge-ink-detection/train/1'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths+=[(os.path.join(dirname, filename))]\n        if filename[-4:]=='.tif':\n            tpaths+=[(os.path.join(dirname, filename))]\nprint(paths)\ntpaths=sorted(tpaths)\nprint(tpaths[0:5])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:51:28.350006Z","iopub.execute_input":"2023-03-18T15:51:28.351266Z","iopub.status.idle":"2023-03-18T15:51:28.380213Z","shell.execute_reply.started":"2023-03-18T15:51:28.351209Z","shell.execute_reply":"2023-03-18T15:51:28.378905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs=[]\nfor path in paths:\n    img=cv2.imread(path)\n    imgs+=[img]\n    print(img.dtype)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:51:28.381458Z","iopub.execute_input":"2023-03-18T15:51:28.381802Z","iopub.status.idle":"2023-03-18T15:51:30.131419Z","shell.execute_reply.started":"2023-03-18T15:51:28.381770Z","shell.execute_reply":"2023-03-18T15:51:30.130112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 3, figsize=(10,4))\naxs[0].imshow(imgs[0]) #ir\naxs[1].imshow(imgs[1]) #inklabels\naxs[2].imshow(imgs[2]) #mask\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:51:30.134924Z","iopub.execute_input":"2023-03-18T15:51:30.135394Z","iopub.status.idle":"2023-03-18T15:51:46.240367Z","shell.execute_reply.started":"2023-03-18T15:51:30.135342Z","shell.execute_reply":"2023-03-18T15:51:46.239393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Binarizing of IR image\nFor ir images, it may be possible to make the characters stand out by binarizing.","metadata":{}},{"cell_type":"code","source":"gray_img = imgs[0]\nmax_value = 255\nthreshold_type = cv2.THRESH_BINARY\n_, binary_img0 = cv2.threshold(gray_img, 130, max_value, threshold_type)\n_, binary_img1 = cv2.threshold(gray_img, 135, max_value, threshold_type)\n_, binary_img2 = cv2.threshold(gray_img, 140, max_value, threshold_type)\nfig, axs = plt.subplots(1, 3, figsize=(10,4))\naxs[0].imshow(binary_img0)\naxs[1].imshow(binary_img1)\naxs[2].imshow(binary_img2)\nplt.show()\n# threshold=135 is the best for IR image","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:51:46.241859Z","iopub.execute_input":"2023-03-18T15:51:46.242450Z","iopub.status.idle":"2023-03-18T15:52:02.239748Z","shell.execute_reply.started":"2023-03-18T15:51:46.242412Z","shell.execute_reply":"2023-03-18T15:52:02.238387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Binary Image as Inklabels to RLE","metadata":{}},{"cell_type":"markdown","source":"#### What is RLE?\n\nRLE is an abbreviation for Run-Length Encoding, and is one of the methods of data compression.\n\nRun-length encoding is a method of compressing long consecutive blocks of the same value, each block being called a \"run\". This compression technique can be applied to all kinds of data, such as image processing, audio processing, and text data.","metadata":{}},{"cell_type":"code","source":"def rle(output):\n    flat_img = np.where(output.flatten() > 100, 1, 0).astype(np.uint8)\n    starts = np.array((flat_img[:-1] == 0) & (flat_img[1:] == 1))\n    ends = np.array((flat_img[:-1] == 1) & (flat_img[1:] == 0))\n    starts_ix = np.where(starts)[0] + 2\n    ends_ix = np.where(ends)[0] + 2\n    lengths = ends_ix - starts_ix\n    return \" \".join(map(str, sum(zip(starts_ix, lengths), ())))","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:52:02.241271Z","iopub.execute_input":"2023-03-18T15:52:02.241736Z","iopub.status.idle":"2023-03-18T15:52:02.249262Z","shell.execute_reply.started":"2023-03-18T15:52:02.241698Z","shell.execute_reply":"2023-03-18T15:52:02.248380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# binary_img2 to rle \nprint(binary_img2.shape)\nimg2_rle=rle(binary_img2)\nprint(img2_rle[0:200])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:52:02.250478Z","iopub.execute_input":"2023-03-18T15:52:02.251297Z","iopub.status.idle":"2023-03-18T15:53:29.844511Z","shell.execute_reply.started":"2023-03-18T15:52:02.251259Z","shell.execute_reply":"2023-03-18T15:53:29.843621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read surface volume","metadata":{}},{"cell_type":"code","source":"#read tif files\ntifs=[]\nfor path in tpaths:\n    tifs+=[cv2.imread(path, cv2.IMREAD_UNCHANGED)]\nprint(len(tifs))\nprint(tifs[0].dtype)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:53:29.845838Z","iopub.execute_input":"2023-03-18T15:53:29.846390Z","iopub.status.idle":"2023-03-18T15:55:07.313908Z","shell.execute_reply.started":"2023-03-18T15:53:29.846353Z","shell.execute_reply":"2023-03-18T15:55:07.312669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    fig = plt.figure(figsize=(4, 4))\n    plt.axis('off')\n    im = plt.imshow(ims[0])\n    text = plt.text(0.05, 0.05, f'Slide {0}', transform=fig.transFigure, fontsize=14)\n    plt.close()\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        text.set_text(f'Slide {i}')\n        return [im, text]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000/10)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:55:07.315923Z","iopub.execute_input":"2023-03-18T15:55:07.316434Z","iopub.status.idle":"2023-03-18T15:55:07.325041Z","shell.execute_reply.started":"2023-03-18T15:55:07.316383Z","shell.execute_reply":"2023-03-18T15:55:07.323622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Surface Volume Slide Show","metadata":{}},{"cell_type":"code","source":"create_animation(tifs)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:55:07.329272Z","iopub.execute_input":"2023-03-18T15:55:07.330150Z","iopub.status.idle":"2023-03-18T15:56:55.586204Z","shell.execute_reply.started":"2023-03-18T15:55:07.330093Z","shell.execute_reply":"2023-03-18T15:56:55.583330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Slide 0 with the Threshold Range of (i2,j2)\nCan you find the character?","metadata":{}},{"cell_type":"code","source":"fig, axs = plt.subplots(6,6, figsize=(15,20))\npath=tpaths[0]\ntifi=cv2.imread(path, cv2.IMREAD_UNCHANGED)\ntifi=(tifi/np.max(tifi)*255).astype('uint8')\nfor i in range(6):\n    for j in range(6):\n        i2=i*10+50\n        j2=i2+(j+2)*10\n        mask = np.logical_and(tifi >= i2, tifi <= j2)\n        new_tifi = np.zeros_like(tifi)\n        new_tifi[mask] = tifi[mask]\n        axs[i][j].imshow(new_tifi)\n        axs[i][j].set_title(str((i2,j2)), fontsize=12)\n        axs[i][j].axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T15:56:55.590816Z","iopub.execute_input":"2023-03-18T15:56:55.593106Z","iopub.status.idle":"2023-03-18T15:57:54.129717Z","shell.execute_reply.started":"2023-03-18T15:56:55.593036Z","shell.execute_reply":"2023-03-18T15:57:54.127823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Slide 0\npath=tpaths[0]\ntifi=cv2.imread(path, cv2.IMREAD_UNCHANGED)\ntifi=(tifi/np.max(tifi)*255).astype('uint8')\ni2=110\nj2=140\nmask = np.logical_and(tifi >= i2, tifi <= j2)\nnew_tifi = np.zeros_like(tifi)\nnew_tifi[mask] = tifi[mask]\n\nfig, axs = plt.subplots(1, 3, figsize=(12,8))\naxs[0].imshow(tifi)\naxs[1].imshow(new_tifi)\naxs[2].imshow(binary_img1)\naxs[0].set_title('Original', fontsize=12)\naxs[1].set_title(str((i2,j2)), fontsize=12)\naxs[2].set_title('Binary Image', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T16:49:41.415911Z","iopub.execute_input":"2023-03-18T16:49:41.416301Z","iopub.status.idle":"2023-03-18T16:49:51.734638Z","shell.execute_reply.started":"2023-03-18T16:49:41.416267Z","shell.execute_reply":"2023-03-18T16:49:51.733026Z"},"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":[]}]}