{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.insert(0, os.path.abspath('..'))\n",
    "\n",
    "from autocnet.examples import get_path\n",
    "from autocnet.graph.network import CandidateGraph\n",
    "from autocnet.graph.edge import Edge\n",
    "from autocnet.matcher.feature import FlannMatcher\n",
    "from autocnet.matcher import ciratefi\n",
    "\n",
    "\n",
    "from autocnet.matcher import subpixel as sp \n",
    "from scipy.misc import imresize\n",
    "import math\n",
    "import warnings\n",
    "import cv2\n",
    "\n",
    "from bisect import bisect_left\n",
    "\n",
    "from scipy.ndimage.interpolation import rotate\n",
    "\n",
    "from IPython.display import display\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "%matplotlib inline\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Basic Structures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#Point to the adjacency Graph\n",
    "adjacency = get_path('three_image_adjacency.json')\n",
    "basepath = get_path('Apollo15')\n",
    "cg = CandidateGraph.from_adjacency(adjacency, basepath=basepath)\n",
    "\n",
    "#Apply SIFT to extract features\n",
    "cg.extract_features(method='sift', extractor_parameters={'nfeatures':300})\n",
    "\n",
    "#Match\n",
    "cg.match_features()\n",
    "\n",
    "# Perform the symmetry check\n",
    "cg.symmetry_checks()\n",
    "# Perform the ratio check\n",
    "cg.ratio_checks(clean_keys = ['symmetry'])\n",
    "# Create fundamental matrix\n",
    "cg.compute_fundamental_matrices(clean_keys = ['symmetry', 'ratio'])\n",
    "\n",
    "# Step: Compute the homographies and apply RANSAC\n",
    "cg.compute_homographies(clean_keys=['symmetry', 'ratio'])\n",
    "\n",
    "# Step: Compute subpixel offsets for candidate points\n",
    "cg.subpixel_register(clean_keys=['ransac'])\n",
    "\n",
    "cg.edge[0][2].plot(clean_keys=['symmetry', 'ratio'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Do Stuff "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from scipy.ndimage.interpolation import zoom\n",
    "from scipy.stats.stats import pearsonr \n",
    "\n",
    "figsize(10,10)\n",
    "e = cg.edge[0][2]\n",
    "matches = e.matches\n",
    "clean_keys = ['ratio', 'symmetry']\n",
    "\n",
    "full_offsets = np.zeros((len(matches), 3))\n",
    "\n",
    "if clean_keys:\n",
    "    matches, mask = e.clean(clean_keys)\n",
    "\n",
    "# Preallocate the numpy array to avoid appending and type conversion\n",
    "edge_offsets = np.empty((len(matches),3))\n",
    "\n",
    "# for each edge, calculate this for each keypoint pair\n",
    "for i, (idx, row) in enumerate(matches.iterrows()):\n",
    "    s_idx = int(row['source_idx'])\n",
    "    d_idx = int(row['destination_idx'])\n",
    "    s_kps = e.source.get_keypoints().iloc[s_idx]\n",
    "    d_kps = e.destination.get_keypoints().iloc[d_idx]\n",
    "\n",
    "    s_keypoint = e.source.get_keypoints().iloc[s_idx][['x', 'y']].values\n",
    "    d_keypoint = e.destination.get_keypoints().iloc[d_idx][['x', 'y']].values\n",
    "    \n",
    "    # Get the template and search windows\n",
    "    s_template = sp.clip_roi(e.source.geodata, s_keypoint, 9)\n",
    "    s_template = rotate(s_template, 0)\n",
    "    s_template = imresize(s_template, 1.)\n",
    "\n",
    "    d_search = sp.clip_roi(e.destination.geodata, d_keypoint, 21)\n",
    "    d_search = rotate(d_search, 0)\n",
    "    d_search = imresize(d_search, 1.)\n",
    "\n",
    "    print(sp.subpixel_offset(s_template, d_search, method='ciratefi', upsampling=16, alpha=math.pi/4, \n",
    "                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,\n",
    "                     use_percentile=True, radii=list(range(1,5))))\n",
    "    \n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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
}
