{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\nimport glob\n\nimport cv2\nimport pandas as pd\nimport numpy as np\nfrom skimage.morphology import closing, square\nfrom skimage.segmentation import clear_border\nfrom skimage.measure import label, regionprops\nfrom skimage.filters import threshold_otsu\nfrom skimage import data\nfrom skimage.color import label2rgb\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"image_path = '../input/test/01973eba5.jpg'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d317d8a129c2352b7547c95023b05989a19cbcb2"},"cell_type":"code","source":"image = cv2.imread(image_path)\nimage = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\nplt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4516b5e3f3ff994b9998db0be30585c54b88bd3c"},"cell_type":"markdown","source":"## Threshold"},{"metadata":{"trusted":true,"_uuid":"8b74ebc444d55632943fea69fde3499f131c058c"},"cell_type":"code","source":"thsld = threshold_otsu(image)\nif thsld:\n    bw = closing(image > thsld, square(3))\nelse:\n    ret, bw = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)\n    \nprint(bw)\nplt.imshow(bw)\nprint(thsld)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4284cfe5eaf8bc7cbee25e3e9b3416c4cbec8b3a","collapsed":true},"cell_type":"code","source":"def rle_encoding(x):\n    '''\n    x: numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns run length as list\n    '''\n    dots = np.where(x.T.flatten()==1)[0] # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b+1, 0))\n        run_lengths[-1] += 1\n        prev = b\n        print(run_lengths)\n    return run_lengths","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3fece3b272f307552039f6451e6ae9873f6a6c3c"},"cell_type":"markdown","source":"## Label images"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"beab1f7abfe908c869c8fc4cc099da52bbd09e7e"},"cell_type":"code","source":"label_image = label(bw)\nplt.imshow(label_image)\nimag_label_overlay = label2rgb(label_image, image=image)\n\nfig, ax = plt.subplots(figsize=(10, 6))\nax.imshow(imag_label_overlay)\n\nfor region in regionprops(label_image):\n    # take regions with large enough areas\n    temp_image_null = np.zeros(label_image.shape)\n    for coord in region.coords:\n        temp_image_null[coord[0]][coord[1]] = 1\n    plt.imshow(temp_image_null)\n    plt.show()\n    rle_encoding(temp_image_null)\n        \nax.set_axis_off()\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8a38d5986822327f57077520fad766541be7a620"},"cell_type":"markdown","source":"## Stay tuned! More to come..."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}