{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport cv2\nimport skimage as ski\nimport os\nTRAIN_FOLD, TEST_FOLD = '../input/understanding_cloud_organization/train_images', '../input/understanding_cloud_organization/test_images'\ntrain = pd.read_csv('../input/understanding_cloud_organization/train.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loading train data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Taking one image as an example"},{"metadata":{"trusted":true},"cell_type":"code","source":"image_name = os.listdir(TRAIN_FOLD)[0]\ntest_image = ski.io.imread(os.path.join(TRAIN_FOLD, image_name))\nprint(f'Image : {image_name}')\nplt.imshow(test_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Finding corresponding rows in dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_image_regions = train[train['Image_Label'].str.contains(image_name)]\ntest_image_regions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mask_to_image_decoding(image_shape, mask_string):\n    if str(mask_string) == 'nan':\n        return np.zeros(image_shape).astype(np.uint8)\n    if not ((isinstance(image_shape, np.ndarray) and image_name.ndim != 2) or len(image_name) != 2):\n        raise ValueError('Expected 2D image size')\n\n    pairs = list(map(int, mask_string.split(' ')))\n    mask_pairs = [\n        (x, y) for x, y in zip(pairs[::2], pairs[1::2]) \n    ]\n    mask = np.zeros(image_shape)\n    \n    for start, length in mask_pairs:\n        mask[np.unravel_index(list(range(start, start + length)), image_shape, order='F')] = 255\n        \n    return mask.astype(np.uint8)\n   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_image_and_mask(image, string_mask):\n    shp = list(image.shape)[:-1]\n    mask = mask_to_image_decoding(shp, string_mask)\n    contours = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n\n    img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n    cv2.drawContours(img_gray, contours[0], -1, (255), 8)\n    to_display = np.hstack([img_gray, mask])\n    plt.figure(figsize=(12, 6))\n    plt.imshow(to_display, cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_and_mask(test_image, test_image_regions['EncodedPixels'].iloc[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_and_mask(test_image, test_image_regions['EncodedPixels'].iloc[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_and_mask(test_image, test_image_regions['EncodedPixels'].iloc[2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_and_mask(test_image, test_image_regions['EncodedPixels'].iloc[3])","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}