{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0f245602-1b68-4fb9-f25b-959673f80fa6"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import numpy as np\n",
        "import tifffile as tiff\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "\n",
        "def stretch_8bit(bands, lower_percent=2, higher_percent=98):\n",
        "    out = np.zeros_like(bands)\n",
        "    for i in range(3):\n",
        "        a = 0 \n",
        "        b = 255 \n",
        "        c = np.percentile(bands[:,:,i], lower_percent)\n",
        "        d = np.percentile(bands[:,:,i], higher_percent)        \n",
        "        t = a + (bands[:,:,i] - c) * (b - a) / (d - c)    \n",
        "        t[t<a] = a\n",
        "        t[t>b] = b\n",
        "        out[:,:,i] =t\n",
        "    return out.astype(np.uint8)    \n",
        "    \n",
        "def M(image_id):\n",
        "    filename = os.path.join('..', 'input', 'sixteen_band', '{}_M.tif'.format(image_id))\n",
        "    img = tiff.imread(filename)    \n",
        "    img = np.rollaxis(img, 0, 3)\n",
        "    return img\n",
        "\n",
        "image_id = '6120_2_2'\n",
        "m = M(image_id)\n",
        "img = np.zeros((837,851,3))\n",
        "img[:,:,0] = m[:,:,4] #red\n",
        "img[:,:,1] = m[:,:,2] #green\n",
        "img[:,:,2] = m[:,:,1] #blue\n",
        "plt.imshow(stretch_8bit(img))\n",
        "\n"
      ]
    }
  ],
  "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
}