{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5fe4424f-99d2-0270-916a-47591330c623"},"outputs":[],"source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport tifffile as tiff\nfrom shapely.wkt import loads\nfrom shapely import affinity\n\n# living dangerously\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nSVEHICLE_TYPE = 10\nSVEHICLE_IMAGES = [ '6120_2_2',\n    '6100_1_3', '6140_3_1','6110_3_1','6100_2_3',\n    '6140_1_2','6120_2_0','6100_2_2','6110_1_2',\n    '6070_2_3','6110_4_0','6090_2_0','6060_2_3'\n]\n\nPADDING = 10\nW = 3396\nH = 3348\n\ndef P(image_id):\n    filename = os.path.join('..', 'input', 'sixteen_band', '{}_P.tif'.format(image_id))\n    img = tiff.imread(filename)    \n    return img\n\ndef RGB(image_id):\n    filename = os.path.join('..', 'input', 'three_band', '{}.tif'.format(image_id))\n    img = tiff.imread(filename)\n    img = np.rollaxis(img, 0, 3)    \n    return img\n    \ndef M(image_id):\n    filename = os.path.join('..', 'input', 'sixteen_band', '{}_M.tif'.format(image_id))\n    img = tiff.imread(filename)    \n    img = np.rollaxis(img, 0, 3)\n    return img\n\ndef stretch2(band, lower_percent=2, higher_percent=98):\n    a = 0 #np.min(band)\n    b = 255  #np.max(band)\n    c = np.percentile(band, lower_percent)\n    d = np.percentile(band, higher_percent)        \n    out = a + (band - c) * (b - a) / (d - c)    \n    out[out<a] = a\n    out[out>b] = b\n    return out\n\ndef adjust_contrast(x):    \n    for i in range(3):\n        x[:,:,i] = stretch2(x[:,:,i])\n    return x.astype(np.uint8)\n    \ndef truth_polys(image_id, class_id):\n    x = pd.read_csv('../input/train_wkt_v4.csv')\n    rows = x.loc[(x.ImageId==image_id) & (x.ClassType==class_id), 'MultipolygonWKT']\n    mp = loads(rows.values[0])\n    \n    grid_sizes = pd.read_csv('../input/grid_sizes.csv')\n    xmax, ymin = [(row[1], row[2]) for row in grid_sizes.values if row[0] == image_id][0]    \n    W_ = W * (W/(W+1))\n    H_ = H * (H/(H+1))\n    x_scaler = W_ / xmax\n    y_scaler = H_ / ymin\n    return affinity.scale(mp, xfact = x_scaler, yfact= y_scaler, origin=(0,0,0))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e8940304-9922-cc7a-e16f-b6f32e1dfb1d"},"outputs":[],"source":"  "},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7533a80e-69b4-e4a3-6ec6-ce18233a1c25"},"outputs":[],"source":"  "}],"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.5.2"}},"nbformat":4,"nbformat_minor":0}