{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "3ab16025-6e5c-2eb7-1d5d-b33cdaaf226a"
      },
      "source": [
        "Just poking around to see how to load images and different ways of finding features"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ddd16573-d6e4-b796-4a59-c9a473661f6b"
      },
      "outputs": [],
      "source": [
        "#Load libraries\n",
        "\n",
        "%matplotlib inline\n",
        "\n",
        "import numpy as np \n",
        "import pandas as pd \n",
        "import matplotlib.pyplot as plt\n",
        "import shapely.wkt as wkt\n",
        "import shapely.affinity\n",
        "import cv2\n",
        "import csv\n",
        "import sys\n",
        "import tifffile as tiff\n",
        "\n",
        "from matplotlib.patches import Polygon as pltPoly\n",
        "from collections import defaultdict\n",
        "from shapely.geometry import MultiPolygon, Polygon"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ee3d2809-cc97-13eb-c61d-dd89e57cdb43"
      },
      "outputs": [],
      "source": [
        "csv.field_size_limit(sys.maxsize) #set field size to max to avoid error later"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "20346d22-a13a-58ca-3aae-6e574ca062bd"
      },
      "outputs": [],
      "source": [
        "df = pd.read_csv('../input/train_wkt_v2.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "16d9c665-d907-cefe-22b3-936d73a391c5"
      },
      "outputs": [],
      "source": [
        "df['ImageId'].unique() #what images are we working with?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5595f4d3-a751-6bdc-c18a-e4a10105dee7"
      },
      "outputs": [],
      "source": [
        "polylist = {}\n",
        "img = df[df.ImageId == '6170_2_4']\n",
        "for c in img['ClassType'].unique():\n",
        "    print(c)\n",
        "    polylist[c] = wkt.loads(img[img['ClassType']==c].MultipolygonWKT.values[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a3a1deae-3ddb-3a88-e234-cefb7ca7e9ba"
      },
      "outputs": [],
      "source": [
        "polylist"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0d5bb4e9-8bfd-a247-f957-dbff3d78750a"
      },
      "outputs": [],
      "source": [
        "#get the different classes and stack them to see how the polygons come together\n",
        "fig, ax = plt.subplots(figsize=(10, 10))\n",
        "\n",
        "for p in polylist:\n",
        "    for polygon in polylist[p]:\n",
        "        mpl_poly = pltPoly(np.array(polygon.exterior), color=plt.cm.Set1(p*10), lw=0, alpha=0.3)\n",
        "        ax.add_patch(mpl_poly)\n",
        "\n",
        "ax.relim()\n",
        "ax.autoscale_view()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "442c1d0b-d362-01d6-0eda-a8ff7a2bdb88"
      },
      "outputs": [],
      "source": [
        "IMG_ID = '6170_2_4'\n",
        "CLASS = '5' #Let's look at some trees"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1198926f-4da9-51c7-e98d-e84b90904b79"
      },
      "outputs": [],
      "source": [
        "# Load grid size\n",
        "x_max = y_min = None\n",
        "for _im_id, _x, _y in csv.reader(open('../input/grid_sizes.csv')):\n",
        "    if _im_id == IMG_ID:\n",
        "        x_max, y_min = float(_x), float(_y)\n",
        "        break\n",
        "\n",
        "# Load train poly with shapely\n",
        "train_polygons = None\n",
        "for _im_id, _poly_type, _poly in csv.reader(open('../input/train_wkt_v4.csv')):\n",
        "    if _im_id == IMG_ID and _poly_type == CLASS:\n",
        "        train_polygons = shapely.wkt.loads(_poly)\n",
        "        break\n",
        "\n",
        "# Read image with tiff\n",
        "im_rgb = tiff.imread('../input/three_band/{}.tif'.format(IMG_ID)).transpose([1, 2, 0])\n",
        "im_size = im_rgb.shape[:2]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8a6389b9-869a-40ab-267b-9693006e9e4e"
      },
      "outputs": [],
      "source": [
        "def get_scalers():\n",
        "    h, w = im_size  # they are flipped so that mask_for_polygons works correctly\n",
        "    w_ = w * (w / (w + 1))\n",
        "    h_ = h * (h / (h + 1))\n",
        "    return w_ / x_max, h_ / y_min\n",
        "\n",
        "x_scaler, y_scaler = get_scalers()\n",
        "\n",
        "train_polygons_scaled = shapely.affinity.scale(\n",
        "    train_polygons, xfact=x_scaler, yfact=y_scaler, origin=(0, 0, 0))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cd7ff1e8-1dc0-ed0c-398d-4d0a4759c037"
      },
      "outputs": [],
      "source": [
        "def mask_for_polygons(polygons):\n",
        "    img_mask = np.zeros(im_size, np.uint8)\n",
        "    if not polygons:\n",
        "        return img_mask\n",
        "    int_coords = lambda x: np.array(x).round().astype(np.int32)\n",
        "    exteriors = [int_coords(poly.exterior.coords) for poly in polygons]\n",
        "    interiors = [int_coords(pi.coords) for poly in polygons\n",
        "                 for pi in poly.interiors]\n",
        "    cv2.fillPoly(img_mask, exteriors, 1)\n",
        "    cv2.fillPoly(img_mask, interiors, 0)\n",
        "    return img_mask\n",
        "\n",
        "train_mask = mask_for_polygons(train_polygons_scaled)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2342566f-9869-0a6d-9759-35c935fd841d"
      },
      "outputs": [],
      "source": [
        "def scale_percentile(matrix):\n",
        "    w, h, d = matrix.shape\n",
        "    matrix = np.reshape(matrix, [w * h, d]).astype(np.float64)\n",
        "    # Get 2nd and 98th percentile\n",
        "    mins = np.percentile(matrix, 1, axis=0)\n",
        "    maxs = np.percentile(matrix, 99, axis=0) - mins\n",
        "    matrix = (matrix - mins[None, :]) / maxs[None, :]\n",
        "    matrix = np.reshape(matrix, [w, h, d])\n",
        "    matrix = matrix.clip(0, 1)\n",
        "    return matrix"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8b5551aa-ec9d-13ba-ad72-c640d05cf97c"
      },
      "outputs": [],
      "source": [
        "tiff.imshow(255 * scale_percentile(im_rgb[2900:3200,2000:2300]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "95c1cbaf-be4f-c575-8dc4-93a8a82e8f53"
      },
      "outputs": [],
      "source": [
        "#Show a black/white image of the selected feature type\n",
        "def show_mask(m):\n",
        "    # hack for nice display\n",
        "    tiff.imshow(255 * np.stack([m, m, m]));\n",
        "show_mask(train_mask[2900:3200,2000:2300])"
      ]
    }
  ],
  "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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}