{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9c727837-44c0-a282-0adf-55a0c9d3a833"},"outputs":[],"source":"import numpy as np\nimport pandas as pd\nfrom shapely import wkt\nimport tifffile\nimport cv2\nfrom matplotlib import pyplot as plt\n\nwkt_df = pd.read_csv('../input/train_wkt_v4.csv', index_col=[0, 1])\ngs_df = pd.read_csv('../input/grid_sizes.csv', index_col=0)\n\nimage_id = '6040_2_2'\nclass_id = 4\n\nmpoly = wkt.loads(wkt_df.ix[(image_id, class_id), 'MultipolygonWKT'])\nsize = gs_df.ix[image_id, ['Xmax', 'Ymin']]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fe156233-12e4-1e42-62dd-9a40f560b1e2"},"outputs":[],"source":"exteriors = []\ninteriors = []\nfor poly in mpoly:\n    exteriors.append(np.asarray(poly.exterior.coords))\n    interiors.extend(np.asarray(hole.coords) for hole in poly.interiors)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ca8ab506-281d-3536-e51f-449971f28246"},"outputs":[],"source":"mask = np.zeros((512, 512), dtype=np.uint8)\nscale = np.asarray(mask.shape).astype(np.float)\nscale /= np.asarray(size)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d017ff92-0bfa-8095-e0d2-972c7c8c9017"},"outputs":[],"source":"exteriors = [np.round(x*scale).astype(np.int32) for x in exteriors]\ninteriors = [np.round(x*scale).astype(np.int32) for x in interiors]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f3a82d19-09d2-1b4e-6cbd-19b3f5d400df"},"outputs":[],"source":"cv2.fillPoly(mask, exteriors, 255)\ncv2.fillPoly(mask, interiors, 128)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"dcaffbba-c37a-d3c1-b5e7-44985e055b4f"},"outputs":[],"source":"plt.imshow(mask, cmap='gray')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e935e106-a2d7-b3a5-97a6-b51e39ece022"},"outputs":[],"source":"img = tifffile.imread('../input/three_band/{}.tif'.format(image_id))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4b4a8528-15c5-904b-ab83-3e2f8c99aeee"},"outputs":[],"source":"plt.imshow(img[2, 2000:, 0:1500], cmap='gray')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"26655685-9f26-cc8a-9be4-ff5641dc0c9f"},"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}