{"nbformat_minor": 1, "cells": [{"cell_type": "code", "outputs": [], "source": ["# This Python 3 environment comes with many helpful analytics libraries installed\n", "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n", "# For example, here's several helpful packages to load in \n", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "import skimage.io\n", "import skimage.segmentation\n", "import matplotlib.pyplot as plt\n", "from glob import glob\n", "%matplotlib inline\n", "plt.rcParams['figure.figsize']=10,10\n", "# Any results you write to the current directory are saved as output."], "metadata": {"_cell_guid": "f546372b-a324-4982-b61c-ed76c8b79cc5", "_uuid": "45e96c5c040bd5c423600e6d1825c862eda085e6"}, "execution_count": 98}, {"cell_type": "markdown", "source": ["## Test Mask Visualization"], "metadata": {}}, {"cell_type": "code", "outputs": [], "source": ["# Load a single image and its associated masks\n", "id = '0a7d30b252359a10fd298b638b90cb9ada3acced4e0c0e5a3692013f432ee4e9'\n", "file = \"../input/stage1_train/{}/images/{}.png\".format(id,id)\n", "masks = \"../input/stage1_train/{}/masks/*.png\".format(id)\n", "image = skimage.io.imread(file)\n", "masks = skimage.io.imread_collection(masks).concatenate()\n", "height, width, _ = image.shape\n", "num_masks = masks.shape[0]\n", "\n", "# Make a ground truth label image (pixel value is index of object label)\n", "labels = np.zeros((height, width), np.uint16)\n", "for index in range(0, num_masks):\n", "    labels[masks[index] > 0] = index + 1\n", "    \n", "plt.imshow(image)\n", "plt.imshow(labels,alpha=0.5)"], "metadata": {}, "execution_count": 99}, {"cell_type": "markdown", "source": ["## Visualize Binary Contours"], "metadata": {}}, {"cell_type": "code", "outputs": [], "source": ["def getContour(mask):\n", "    ### generate bound\n", "    mask_pad=np.pad(mask,((1,1),(1,1)),'constant')\n", "    h,w=mask.shape\n", "    contour=np.zeros((h,w),dtype=np.bool)\n", "    for i in range(3):\n", "        for j in range(3):\n", "            if i==j==1:\n", "                continue\n", "            edge=(np.float32(mask)-np.float32(mask_pad[i:i+h,j:j+w]))>0\n", "            contour=np.logical_or(contour,edge)\n", "    return contour\n", "\n", "def showContour(image,contours):\n", "    vis=np.copy(image)\n", "    for contour in contours:\n", "        vis[:,:,0]^=np.uint8(contour*255)\n", "    plt.imshow(vis)\n", "    \n", "contours = [getContour(mask) for mask in masks]\n", "showContour(image,contours)"], "metadata": {}, "execution_count": 104}, {"cell_type": "markdown", "source": ["## Play With Per-Mask Distance Transform"], "metadata": {}}, {"cell_type": "code", "outputs": [], "source": ["from scipy.ndimage.morphology import distance_transform_edt\n", "\n", "edts=[distance_transform_edt(mask) for mask in masks]\n", "plt.imshow(np.sum(edts,axis=0))"], "metadata": {}, "execution_count": 114}, {"cell_type": "code", "outputs": [], "source": ["## contour finding with distance transform\n", "plt.imshow(np.sum(edts,axis=0)==1)"], "metadata": {}, "execution_count": 121}], "metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"name": "python", "mimetype": "text/x-python", "version": "3.6.4", "codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "pygments_lexer": "ipython3", "nbconvert_exporter": "python"}}, "nbformat": 4}