{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"fd9a3a33-032a-4e53-b37e-539c5dbfa565"},"source":"## Reflectance Index for Water Way [0.095 LB] ##"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e52a9d4a-11d2-783e-6c3c-dde3ce532877"},"outputs":[],"source":"# libs\nfrom __future__ import division\nimport numpy as np\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom skimage.transform import resize\nfrom collections import defaultdict\nimport pandas as pd\nimport cv2\nimport os\nimport shapely\nfrom shapely.geometry import MultiPolygon, Polygon"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6f163403-f5d6-a715-8a46-0606a18a9b54"},"outputs":[],"source":"def stretch_8bit(bands, lower_percent=2, higher_percent=98):\n    out = np.zeros_like(bands).astype(np.float32)\n    for i in range(3):\n        a = 0 \n        b = 1 \n        c = np.percentile(bands[:,:,i], lower_percent)\n        d = np.percentile(bands[:,:,i], higher_percent)        \n        t = a + (bands[:,:,i] - c) * (b - a) / (d - c)    \n        t[t<a] = a\n        t[t>b] = b\n        out[:,:,i] =t\n    return out.astype(np.float32)\n\ndef CCCI_index(m, rgb):\n    RE  = resize(m[5,:,:], (rgb.shape[0], rgb.shape[1])) \n    MIR = resize(m[7,:,:], (rgb.shape[0], rgb.shape[1])) \n    R = rgb[:,:,0]\n    # canopy chloropyll content index\n    CCCI = (MIR-RE)/(MIR+RE)*(MIR-R)/(MIR+R)\n    return CCCI    "},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fbcf3243-453b-590a-52b3-97af11ed40ba"},"outputs":[],"source":"data = pd.read_csv('../input/train_wkt_v4.csv')\ndata = data[data.MultipolygonWKT != 'MULTIPOLYGON EMPTY']\ngrid_sizes_fname = '../input/grid_sizes.csv'\nwkt_fname = '../input/train_wkt_v4.csv'\nimage_fname = '../input/three_band/'"},{"cell_type":"markdown","metadata":{"_cell_guid":"5761a769-334a-42d3-d024-bcdb4e0a31e9"},"source":"## Train images ##"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4e6606cc-c773-a6e3-af1d-79a41ec6b2cb"},"outputs":[],"source":"for IM_ID in data[data.ClassType == 7].ImageId:\n    # read rgb and m bands\n    rgb = tiff.imread('../input/three_band/{}.tif'.format(IM_ID))\n    rgb = np.rollaxis(rgb, 0, 3)\n    m = tiff.imread('../input/sixteen_band/{}_M.tif'.format(IM_ID))\n    \n    # get our index\n    CCCI = CCCI_index(m, rgb) \n    \n    # you can look on histogram and pick your favorite threshold value(0.11 is my best)\n    binary = (CCCI > 0.11).astype(np.float32)\n    \n    fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(10, 10))\n    ax = axes.ravel()\n    ax[0].imshow(stretch_8bit(rgb))\n    ax[0].set_title('Image')\n    ax[0].axis('off')\n    ax[1].imshow(binary)\n    ax[1].set_title('Binary')\n    ax[1].axis('off')\n    plt.tight_layout()\n    plt.show()"},{"cell_type":"markdown","metadata":{"_cell_guid":"6ff59c73-99a4-9309-5c34-a225e2a8355c"},"source":"## Test images ##"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"69eaeb98-52cb-d607-9f88-8f50e99cbb2a"},"outputs":[],"source":"# take some pictures from test \nwaterway_test = ['6080_4_3','6080_4_0',\n                 '6080_1_3', '6080_1_1',\n                 '6150_3_4', '6050_2_1']\n\nfor IM_ID in waterway_test:\n    # read rgb and m bands\n    rgb = tiff.imread('../input/three_band/{}.tif'.format(IM_ID))\n    rgb = np.rollaxis(rgb, 0, 3)\n    m = tiff.imread('../input/sixteen_band/{}_M.tif'.format(IM_ID))\n    \n    # get our index\n    CCCI = CCCI_index(m, rgb) \n    \n    # you can look on histogram and pick your favorite threshold value(0.11 is my best)\n    binary = (CCCI > 0.11).astype(np.float32)\n    \n    fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(10, 10))\n    ax = axes.ravel()\n    ax[0].imshow(stretch_8bit(rgb))\n    ax[0].set_title('Image')\n    ax[0].axis('off')\n    ax[1].imshow(binary)\n    ax[1].set_title('Binary')\n    ax[1].axis('off')\n    plt.tight_layout()\n    plt.show()"},{"cell_type":"markdown","metadata":{"_cell_guid":"57ea83f8-3b05-dbf6-857e-be654fa48970"},"source":"## How to get 0.095 for WaterWay ##"},{"cell_type":"markdown","metadata":{"_cell_guid":"2093f1c4-f33a-c46d-bcdc-8b89e1f52948"},"source":"- Use this idea with CCCI index\n- Convert binary mask to polygons (take functions from some top kernels)\n- Use threshold for number of pixels per image for filtering (> 500k pixels should work)\n- Get your 0.095"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}