{"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":"#importing important Libraries\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","metadata":{"execution":{"iopub.status.busy":"2021-05-31T05:20:12.742449Z","iopub.execute_input":"2021-05-31T05:20:12.742938Z","iopub.status.idle":"2021-05-31T05:20:12.903911Z","shell.execute_reply.started":"2021-05-31T05:20:12.742823Z","shell.execute_reply":"2021-05-31T05:20:12.902819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('../input/cotton-imgs/Cotton_Images/Cotton_img1.jpg')\nplt.imshow(image, cmap='Greys')","metadata":{"execution":{"iopub.status.busy":"2021-05-31T05:20:15.252015Z","iopub.execute_input":"2021-05-31T05:20:15.252374Z","iopub.status.idle":"2021-05-31T05:20:15.574186Z","shell.execute_reply.started":"2021-05-31T05:20:15.252342Z","shell.execute_reply":"2021-05-31T05:20:15.573452Z"},"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":"2021-05-31T05:20:22.932044Z","iopub.execute_input":"2021-05-31T05:20:22.93236Z","iopub.status.idle":"2021-05-31T05:20:23.150492Z","shell.execute_reply.started":"2021-05-31T05:20:22.932332Z","shell.execute_reply":"2021-05-31T05:20:23.149577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Discover the first point \nfor i,j in enumerate(img):\n    if  j.all() == 255:\n        start_point = (i, j)\n        print(start_point, value)\n        break\n    else:\n        continue\n    break\n","metadata":{"execution":{"iopub.status.busy":"2021-05-31T01:45:26.996319Z","iopub.execute_input":"2021-05-31T01:45:26.99697Z","iopub.status.idle":"2021-05-31T01:45:27.01307Z","shell.execute_reply.started":"2021-05-31T01:45:26.996917Z","shell.execute_reply":"2021-05-31T01:45:27.012056Z"},"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":"2021-05-31T01:36:28.717867Z","iopub.execute_input":"2021-05-31T01:36:28.718233Z","iopub.status.idle":"2021-05-31T01:36:28.739173Z","shell.execute_reply.started":"2021-05-31T01:36:28.718198Z","shell.execute_reply":"2021-05-31T01:36:28.737216Z"},"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":"2021-05-31T05:20:50.615589Z","iopub.execute_input":"2021-05-31T05:20:50.616135Z","iopub.status.idle":"2021-05-31T05:20:50.624562Z","shell.execute_reply.started":"2021-05-31T05:20:50.616093Z","shell.execute_reply":"2021-05-31T05:20:50.623732Z"},"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","collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(count)\nprint(chain)","metadata":{"_uuid":"4d58519a983e89e538cbc91ff11dc800a3bbecc0","_cell_guid":"90bdec05-c56b-4d7c-8168-af0a600281a3"},"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_count":null,"outputs":[]}]}