{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import os\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.signal import argrelmax\n",
    "import numpy as np\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def scan_axis(v, axis):\n",
    "    argmax = argrelmax(v, order=int(np.sqrt(v.shape[axis])),\n",
    "                       axis=axis)\n",
    "\n",
    "    return set([(i[0], i[1]) for i in np.column_stack(argmax)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dir = dl.data.get_data_dir()\n",
    "path = os.path.join(dir, 'night-927168_640.jpg')\n",
    "base = 'https://pixabay.com/static/uploads/photo/2015/09/06/10/19/'\n",
    "url = base + 'night-927168_640.jpg'\n",
    "\n",
    "if not dl.conf.file_exists(path):\n",
    "    dl.data.download(url, path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(path)\n",
    "hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "\n",
    "h, s, v = cv2.split(hsv)\n",
    "\n",
    "# Transform for normalization\n",
    "v = v.astype(np.uint16) ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('searching_stars')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.ax.hist(v.ravel(), normed=True)\n",
    "sp.label()\n",
    "\n",
    "dl.plotting.hist_norm_pdf(sp.next_ax(), v.mean(axis=0))\n",
    "sp.label()\n",
    "\n",
    "dl.plotting.hist_norm_pdf(sp.next_ax(), v.mean(axis=1))\n",
    "sp.label()\n",
    "\n",
    "points = scan_axis(v, 0).intersection(scan_axis(v, 1))\n",
    "\n",
    "limit = np.percentile(np.unique(v.ravel()), 95)\n",
    "\n",
    "kp = [cv2.KeyPoint(p[1], p[0], 1) for p in points\n",
    "      if v[p[0], p[1]] > limit]\n",
    "with_kp = cv2.drawKeypoints(img, kp, None, (255, 0, 0))\n",
    "\n",
    "dl.plotting.img_show(sp.next_ax(), with_kp)\n",
    "sp.label()\n",
    "\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
