{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5e31d044-dccb-1e58-3688-4506abf38c2d"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "from shapely.wkt import loads\n",
        "from shapely import affinity\n",
        "from matplotlib.patches import Polygon\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "import tifffile as tiff\n",
        "\n",
        "import glob, os\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6db69645-03df-5f0f-98bb-73b63559cdf8"
      },
      "outputs": [],
      "source": [
        "df = pd.read_csv('../input/train_wkt_v3.csv')\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "30066f84-d524-fb10-b5b5-f5ee25ecc70a"
      },
      "outputs": [],
      "source": [
        "gs = pd.read_csv('../input/grid_sizes.csv', names=['ImageId', 'Xmax', 'Ymin'], skiprows=1)\n",
        "print(gs.head())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d83af87a-9b14-24ae-9dd3-5c97810824c8"
      },
      "outputs": [],
      "source": [
        "# First Image\n",
        "polygonsList ={}\n",
        "image = df[df.ImageId == '6100_1_3']\n",
        "for cType in image.ClassType.unique():\n",
        "    polygonsList[cType] = loads(image[image.ClassType == cType].MultipolygonWKT.values[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f0cd88ce-edec-d8f1-9199-5f3de8e46fb4"
      },
      "outputs": [],
      "source": [
        "# plot using matplotlib\n",
        "fig, ax = plt.subplots(figsize=(8, 8))\n",
        "\n",
        "# plotting, color by class type\n",
        "for p in polygonsList:\n",
        "    for polygon in polygonsList[p]:\n",
        "        mpl_poly = Polygon(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": "d2db9ddf-614e-95f6-d6ba-b69096ff17e8"
      },
      "outputs": [],
      "source": [
        "import tifffile as tiff\n",
        "\n",
        "img_filename_6100_1_3 = '../input/three_band/6100_1_3.tif'\n",
        "\n",
        "image = tiff.imread(img_filename_6100_1_3) \n",
        "tiff.imshow(image)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4b406aab-6968-f4b0-0e41-f2beeb20ba6b"
      },
      "outputs": [],
      "source": [
        "# Dimensions\n",
        "dims = np.shape(image)\n",
        "print(dims)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f1537760-b87b-1519-5b64-f6bc2ecb1c52"
      },
      "outputs": [],
      "source": [
        "# RGB values\n",
        "np.min(image), np.max(image)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8c97d3f-ea87-cabc-a767-639baeb9cce6"
      },
      "outputs": [],
      "source": [
        "# For any image specific classification, clustering, etc. transforms we'll want to \n",
        "# collapse spatial dimensions so that we have a matrix of pixels by color channels.\n",
        "\n",
        "pixel_matrix = np.reshape(image, (dims[1] * dims[2] , dims[0]))\n",
        "print(np.shape(pixel_matrix))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4f6504ad-e625-c0ba-f1a0-5f007bf964c6"
      },
      "outputs": [],
      "source": [
        "# Scatter plots are a go to to look for clusters and separatbility in the data, \n",
        "# but these are busy and don't reveal density well, so we switch to using 2d histograms instead. \n",
        "# The data between bands is really correlated, typical with visible imagery and\n",
        "# why most satellite image analysts prefer to at least have near infrared values.\n",
        "\n",
        "\n",
        "#plt.scatter(pixel_matrix[:,0], pixel_matrix[:,1])\n",
        "_ = plt.hist2d(pixel_matrix[:,1], pixel_matrix[:,2], bins=(50,50))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "04b72ce9-23fb-d8ea-b387-974eb5fe9c66"
      },
      "outputs": [],
      "source": [
        "img_filename_6100_1_4 = '../input/three_band/6110_1_4.tif'\n",
        "\n",
        "image4 = tiff.imread(img_filename_6100_1_4) \n",
        "dims = np.shape(image4)\n",
        "pixel_matrix4 = np.reshape(image4, (dims[1] * dims[2], dims[0]))\n",
        "_ = plt.hist2d(pixel_matrix4[:,1], pixel_matrix4[:,2], bins=(50,50))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "81d1e634-d6d4-6b7b-77a3-f83c448e7f90"
      },
      "outputs": [],
      "source": [
        "# for variations between the images:\n",
        "#_ = plt.hist2d(pixel_matrix[:,2], pixel_matrix4[:,2], bins=(50,50))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "542214ae-b0d9-85ba-6d00-a7833f93afe8"
      },
      "outputs": [],
      "source": [
        "# Rudimentary Transforms, Edge Detection, Texture\n",
        "\n",
        "import skimage\n",
        "from skimage.feature import greycomatrix, greycoprops\n",
        "from skimage.filters import sobel"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f6a0b862-1c2b-d311-bf98-535b7da99d9c"
      },
      "outputs": [],
      "source": [
        "# SOBEL EDGE DETECTION\n",
        "# A Sobel filter is one means of getting a basic edge magnitude/gradient image.\n",
        "# Can be useful to threshold and find prominent linear features, etc. \n",
        "# Several other similar filters in skimage.filters are also good\n",
        "# edge detectors: roberts, scharr, etc. and you can control direction, i.e. use an anisotropic version.\n",
        "\n",
        "# a sobel filter is a basic way to get an edge magnitude/gradient image\n",
        "#fig = plt.figure(figsize=(8, 8))\n",
        "#plt.imshow(sobel(image[:750,:750,2]))\n",
        "tiff.imshow(sobel(image[:750,:750,2]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9bc2c880-8af0-8a1b-5984-1dfd436735e7"
      },
      "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
}