{
  "metadata": {
    "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,
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6103e146-6903-b0f0-0073-122d4a9d2ca5",
        "_active": true,
        "collapsed": false
      },
      "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\nimport numpy as np # linear algebra\nimport 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\nimport tifffile\nimport cv2\n\npp = tifffile.imread('../input/three_band/6120_2_2.tif').transpose((1,2,0)).astype(np.float32)\ngray = tifffile.imread('../input/sixteen_band/6120_2_2_P.tif').astype(np.float32)\nmm = tifffile.imread('../input/sixteen_band/6120_2_2_M.tif').transpose((1,2,0)).astype(np.float32)\n\npp.shape, mm.shape\n\nmm2 = cv2.resize(mm,(pp.shape[1],pp.shape[0]),interpolation=cv2.INTER_CUBIC)\nwarp_mode = cv2.MOTION_EUCLIDEAN\nwarp_matrix = np.eye(2, 3, dtype=np.float32)\ncriteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 1000,  1e-7)\n\n#tifffile.imshow(mm[:,:,[4,2,1]])\nimg_orig = np.stack([mm2[:-2, :-4, 4], pp[2:, 4:, 1],pp[2:, 4:, 2]], axis=-1)\n\ndef stretch2(band, lower_percent=2, higher_percent=98):\n    a = 0 #np.min(band)\n    b = 255  #np.max(band)\n    c = np.percentile(band, lower_percent)\n    d = np.percentile(band, higher_percent)        \n    out = a + (band - c) * (b - a) / (d - c)    \n    out[out<a] = a\n    out[out>b] = b\n    return out\n\ndef adjust_contrast(x):    \n    for i in range(3):\n        x[:,:,i] = stretch2(x[:,:,i])\n    return x.astype(np.uint8)\n#tifffile.imshow(adjust_contrast(img_orig)[2000:2500,3000:])\n#tifffile.imshow(adjust_contrast(pp)[2000:2500,3000:])\n\ncv2.findTransformECC(pp[300:1900,300:2200,2], mm2[300:1900,300:2200,1], warp_matrix, warp_mode, criteria)\n",
      "execution_state": "idle"
    }
  ]
}