{"nbformat_minor": 1, "metadata": {"language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python", "file_extension": ".py", "version": "3.6.4", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat": 4, "cells": [{"execution_count": null, "cell_type": "code", "outputs": [], "metadata": {"_cell_guid": "e47d04c9-d976-49e2-83f2-6fa6854cce53", "_uuid": "7c8fbce05cb31280b61e3915edb0e6d5aced80ad"}, "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", "import random\n", "import skimage.io\n", "import matplotlib.pyplot as plt\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", "# Any results you write to the current directory are saved as output."]}, {"source": ["**Next section reads a random subset of 16 images from the training data set and shows them along with their nuclei mask contours.**"], "cell_type": "markdown", "metadata": {"_cell_guid": "3953def3-5939-4084-8e06-0f7c5b028542", "_uuid": "f3e0b0d7a7af7c0e22f7c46f0b4c54189fcf3e11"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "metadata": {"_cell_guid": "58d4a240-48e6-4996-a0d8-8a2c38c7dd61", "_uuid": "c02d0a22cd8bf8c63f83c03fbd41212db536b141"}, "source": ["from skimage import morphology\n", "\n", "def read_image_labels(image_id):\n", "    # most of the content in this function is taken from 'Example Metric Implementation' kernel \n", "    # by 'William Cukierski'\n", "    image_file = \"../input/stage1_train/{}/images/{}.png\".format(image_id,image_id)\n", "    mask_file = \"../input/stage1_train/{}/masks/*.png\".format(image_id)\n", "    image = skimage.io.imread(image_file)\n", "    masks = skimage.io.imread_collection(mask_file).concatenate()    \n", "    height, width, _ = image.shape\n", "    num_masks = masks.shape[0]\n", "    maxValue = np.max(image)\n", "    for index in range(0, num_masks):\n", "        contour = np.logical_xor(masks[index], morphology.binary_erosion(masks[index]) )\n", "        image[contour > 0] = maxValue\n", "    return image\n", "\n", "def plot_images_masks(image_ids):\n", "    plt.close('all')\n", "    fig, ax = plt.subplots(nrows=len(image_ids),ncols=1, figsize=(256,256))\n", "    for ax_index, image_id in enumerate(image_ids):\n", "        image = read_image_labels(image_id)\n", "        ax[ax_index].imshow(image)\n", "\n", "image_ids = check_output([\"ls\", \"../input/stage1_train/\"]).decode(\"utf8\").split()\n", "print(\"Total Images in Training set: {}\".format(len(image_ids)))\n", "random_image_ids = random.sample(image_ids, 16)\n", "print(\"Randomly Selected Images: {}, their IDs: {}\".format(len(random_image_ids), random_image_ids))\n", "plot_images_masks(random_image_ids)\n", "    "]}, {"execution_count": null, "cell_type": "code", "outputs": [], "metadata": {"collapsed": true, "_cell_guid": "9b32c7ba-9c7c-458a-bf04-0b6259f37cde", "_uuid": "3071e7e780f9a9a17ce0b49b4a662557d99a5049"}, "source": []}]}