{"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":"%matplotlib inline","metadata":{"_uuid":"e69b6263fe2e71c7ded8b808bebed50aa9739fa1","_cell_guid":"cfc48a98-3703-45d5-b2f7-9076f4b6a2a5","execution":{"iopub.status.busy":"2022-06-28T07:12:52.953672Z","iopub.execute_input":"2022-06-28T07:12:52.954281Z","iopub.status.idle":"2022-06-28T07:12:52.987896Z","shell.execute_reply.started":"2022-06-28T07:12:52.954133Z","shell.execute_reply":"2022-06-28T07:12:52.98649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \n# This code is based on http://www.cs.unca.edu/~reiser/imaging/chaincode.html\n\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nfrom math import sqrt\nfrom matplotlib import pyplot as plt\nfrom itertools import chain\n\n%matplotlib inline\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\ntrain = pd.read_csv(\"../input/train.csv\")\n\ntrain[:3]\n# Any results you write to the current directory are saved as output.\nlabels = train['label']\ntrain_images = train.drop('label', axis=1)\ntrain_images.head()\nimage = np.reshape(train_images[9:10].as_matrix(), (-1, 28)).astype(np.uint8)\nplt.imshow(image, cmap='Greys')\n","metadata":{"_uuid":"c1b7c75672ef4d13c5ac453ac589a0b93e5a3beb","_cell_guid":"ee12a380-f6bf-47b0-9ba7-434223421a6f","execution":{"iopub.status.busy":"2022-06-28T07:12:52.989712Z","iopub.execute_input":"2022-06-28T07:12:52.990621Z","iopub.status.idle":"2022-06-28T07:12:56.395213Z","shell.execute_reply.started":"2022-06-28T07:12:52.990578Z","shell.execute_reply":"2022-06-28T07:12:56.393635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ret,img = cv2.threshold(image,70,255,0)\nplt.imshow(img, cmap='Greys')","metadata":{"_uuid":"8131b8e73b77ba1bafc79a6e22f8b9be1f6500c5","_cell_guid":"d73398b8-efea-4d9a-9755-33520beacb64","execution":{"iopub.status.busy":"2022-06-28T07:12:56.396153Z","iopub.status.idle":"2022-06-28T07:12:56.396988Z","shell.execute_reply.started":"2022-06-28T07:12:56.396776Z","shell.execute_reply":"2022-06-28T07:12:56.396799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Discover the first point \nfor i, row in enumerate(img):\n    for j, value in enumerate(row):\n        if value == 255:\n            start_point = (i, j)\n            print(start_point, value)\n            break\n    else:\n        continue\n    break","metadata":{"_uuid":"45eb27b2d73c4471787ec65ad622698df8e0e67a","_cell_guid":"572e44a5-1692-42ad-9a49-966ce49cdfd0","execution":{"iopub.status.busy":"2022-06-28T07:12:56.398496Z","iopub.status.idle":"2022-06-28T07:12:56.399436Z","shell.execute_reply.started":"2022-06-28T07:12:56.399124Z","shell.execute_reply":"2022-06-28T07:12:56.399152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img[3:6, 19:22]","metadata":{"_uuid":"2edcd37ca1d6cf978c0fe9e863ed754dbaa338c0","_cell_guid":"6a43eb17-3fb2-4f30-ba1c-4cf203ca6943","execution":{"iopub.status.busy":"2022-06-28T07:12:56.401362Z","iopub.status.idle":"2022-06-28T07:12:56.402285Z","shell.execute_reply.started":"2022-06-28T07:12:56.401974Z","shell.execute_reply":"2022-06-28T07:12:56.402001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"directions = [ 0,  1,  2,\n               7,      3,\n               6,  5,  4]\ndir2idx = dict(zip(directions, range(len(directions))))\n\nchange_j =   [-1,  0,  1, # x or columns\n              -1,      1,\n              -1,  0,  1]\n\nchange_i =   [-1, -1, -1, # y or rows\n               0,      0,\n               1,  1,  1]\n\nborder = []\nchain = []\ncurr_point = start_point\nfor direction in directions:\n    idx = dir2idx[direction]\n    new_point = (start_point[0]+change_i[idx], start_point[1]+change_j[idx])\n    if img[new_point] != 0: # if is ROI\n        border.append(new_point)\n        chain.append(direction)\n        curr_point = new_point\n        break\n\ncount = 0\nwhile curr_point != start_point:\n    #figure direction to start search\n    b_direction = (direction + 5) % 8 \n    dirs_1 = range(b_direction, 8)\n    dirs_2 = range(0, b_direction)\n    dirs = []\n    dirs.extend(dirs_1)\n    dirs.extend(dirs_2)\n    for direction in dirs:\n        idx = dir2idx[direction]\n        new_point = (curr_point[0]+change_i[idx], curr_point[1]+change_j[idx])\n        if image[new_point] != 0: # if is ROI\n            border.append(new_point)\n            chain.append(direction)\n            curr_point = new_point\n            break\n    if count == 1000: break\n    count += 1\n    ","metadata":{"_uuid":"e3989a26a36caac27288ef1b94443a249e950a6f","_cell_guid":"32c7523b-8646-4a8d-b268-acc38c88c91f","execution":{"iopub.status.busy":"2022-06-28T07:12:56.40351Z","iopub.status.idle":"2022-06-28T07:12:56.404805Z","shell.execute_reply.started":"2022-06-28T07:12:56.404512Z","shell.execute_reply":"2022-06-28T07:12:56.40455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(count)\nprint(chain)","metadata":{"_uuid":"4d58519a983e89e538cbc91ff11dc800a3bbecc0","_cell_guid":"90bdec05-c56b-4d7c-8168-af0a600281a3","execution":{"iopub.status.busy":"2022-06-28T07:12:56.406668Z","iopub.status.idle":"2022-06-28T07:12:56.407672Z","shell.execute_reply.started":"2022-06-28T07:12:56.407372Z","shell.execute_reply":"2022-06-28T07:12:56.4074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img, cmap='Greys')\nplt.plot([i[1] for i in border], [i[0] for i in border])\n\n","metadata":{"_uuid":"7086f95bc87c753ed38a5e3432f592b8c4569e58","_cell_guid":"0d7db5c8-10c4-4c76-b980-6a15f5ccbac7","execution":{"iopub.status.busy":"2022-06-28T07:12:56.408853Z","iopub.status.idle":"2022-06-28T07:12:56.410073Z","shell.execute_reply.started":"2022-06-28T07:12:56.409774Z","shell.execute_reply":"2022-06-28T07:12:56.409804Z"},"trusted":true},"execution_count":null,"outputs":[]}]}