{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# A struture from motion example" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "skip" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n", " warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n" ] } ], "source": [ "import cv2\n", "\n", "%matplotlib notebook\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "import seaborn as sns\n", "sns.set_style(\"dark\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "The last example in this tutorial combines different packages to solve a structure from motion problem on 2 images. It starts using OpenCV to detect SIFT features and their descriptors. Scikit-learn’s implementation of PCA is employed to reduce the dimensionality of the descriptors and the KD-Trees in the SciPy library are then used to efficiently find features matches. From the found matches, the fundamental matrix between the pair of images is computed using OpenCV, and linear algebra procedures from SciPy are employed to compute the essential matrix and retrieve valid projection matrices. Finally, SVD decomposition, implemented in scipy.linalg, is used to perform triangulations and estimate a 3D point for each pair of matching features. The code and comments are available\n", "online 11 as an IPython notebook." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Take two images input images from a static object\n", "- Compute features and matches between the images\n", "- Recover the fundamental and the essential matrices $\\mathrm{\\tt F}$ and $\\mathrm{\\tt E}$\n", "- Extract a pair of projective matrices from $\\mathrm{\\tt E}$\n", "- Triangulate to get the 3D points $\\mathbf{X}_i$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Input: the *Temple Ring* dataset\n", "\n", "- The [Temple Ring dataset](http://vision.middlebury.edu/mview/data/) was created by [Seitz *et al.*](http://vision.middlebury.edu/mview/seitz_mview_cvpr06.pdf)\n", "- We will load **two images**\n", "- And detect SIFT features and their descriptors" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "T1 = cv2.imread('data/templeRing/templeR0034.png', cv2.IMREAD_GRAYSCALE).T\n", "sift = cv2.xfeatures2d.SIFT_create(nfeatures=5000)\n", "kpts1, D_i = sift.detectAndCompute(T1, mask=None)\n", "K1 = np.array([[k.pt[0], k.pt[1]] for k in kpts1])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "T2 = cv2.imread('data/templeRing/templeR0036.png', cv2.IMREAD_GRAYSCALE).T\n", "sift = cv2.xfeatures2d.SIFT_create(nfeatures=5000)\n", "kpts2, D_j = sift.detectAndCompute(T2, mask=None)\n", "K2 = np.array([[k.pt[0], k.pt[1]] for k in kpts2])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " this.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width);\n", " canvas.attr('height', height);\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " // select the cell after this one\n", " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_matches(m)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Recovering structure\n", "\n", "Find the fundamental matrix $\\mathrm{\\tt F}$" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "xi = K1[m[:,0],:]\n", "xj = K2[m[:,1],:]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "F, status = cv2.findFundamentalMat(xi, xj, cv2.FM_RANSAC, 0.5, 0.9)\n", "from scipy import linalg as la\n", "assert(la.det(F) < 1.e-7)\n", "is_inlier = np.array(status == 1).reshape(-1)\n", "\n", "inlier_i = xi[is_inlier]\n", "inlier_j = xj[is_inlier]" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "hg = lambda x : np.array([x[0], x[1], 1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Find epipolar matrix $\\mathrm{\\tt E}$\n", "\n", "- The epipolar matrix $\\mathrm{\\tt E}$ is defined as $\\mathrm{\\tt E} = \\mathrm{\\tt K}^\\intercal \\mathrm{\\tt F} \\mathrm{\\tt K}$, \n", "- $\\mathrm{\\tt K}$ is the *calibration matrix* (*camera intrinsics*)\n", "- The values used here are provided by the [Temple Ring dataset](http://vision.middlebury.edu/mview/data/)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1.52040000e+03, 0.00000000e+00, 3.02320000e+02],\n", " [ 0.00000000e+00, 1.52590000e+03, 2.46870000e+02],\n", " [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K = np.array([[1520.4, 0., 302.32],\n", " [0, 1525.9, 246.87],\n", " [0, 0, 1]])\n", "K" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "E = np.dot(K.T, np.dot(F, K))\n", "U, s, VT = la.svd(E)\n", "\n", "if la.det(np.dot(U, VT)) < 0:\n", " VT = -VT \n", "E = np.dot(U, np.dot(np.diag([1,1,0]), VT)) \n", "V = VT.T\n", " \n", "# Let's check Nistér (2004) Theorem 3 constraint:\n", "assert(la.det(U) > 0)\n", "assert(la.det(V) > 0)\n", "# Nistér (2004) Theorem 2 (\"Essential Condition\")\n", "assert sum(np.dot(E, np.dot(E.T, E)) - 0.5 * np.trace(np.dot(E, E.T)) * E)[0] < 1.0e-10" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Direct Linear Transform-based triangulation\n", "\n", "Given two corresponding homogeneous points $\\mathbf{x}_i$ and $\\mathbf{x}_j$, observed in images $I_i$ and $I_j$ respectively, and the projection matrices $\\mathrm{\\tt P}_i$ and $\\mathrm{\\tt P}_j$, estimate the 3D points $\\mathbf{X}$ in the scene associated to the pair $\\mathbf{x}_i \\leftrightarrow \\mathbf{x}_j$. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- Rays back-projection\n", " - Imperfect measures $\\mathbf{x}_i$ and $\\mathbf{x}_j$ avoids rays intersection\n", "- Alternative\n", " - Linear least-square formulation\n", " - Build a system $\\mathrm{\\tt A}\\mathbf{X}$\n", " - Solve using SVD" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "The point $\\mathbf{X}$ could be estimated by rays back-projection in the 3D space. However, in general $\\mathbf{x}_i$ and $\\mathbf{x}_j$ are imperfect measures and the back-projected rays will not perfectly\n", "intersect in $\\mathbf{X}$. Such a problem can be formalized as a linear least-square problem in the system of equations $\\mathrm{\\tt A}\\mathbf{X}$, where $\\mathrm{\\tt A}$ is defined over $\\mathbf{x}_i$, $\\mathbf{x}_j$, $\\mathrm{\\tt P}_i$ and $\\mathrm{\\tt P}_j$ (see Section~12.2 of [Hartley and Zisserman's book](http://www.robots.ox.ac.uk/~vgg/hzbook/)). The following code uses the SVD implementation in SciPy to perform the minimization, finding the best estimation of $\\mathbf{X}$. Note the last column of $\\mathrm{\\tt V}$ can be indexed by $-1$." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def dlt_triangulation(ui, Pi, uj, Pj):\n", " \"\"\"Hartley & Zisserman, 12.2\"\"\"\n", " ui /= ui[2]\n", " xi, yi = ui[0], ui[1]\n", " \n", " uj /= uj[2]\n", " xj, yj = uj[0], uj[1]\n", " \n", " a0 = xi * Pi[2,:] - Pi[0,:]\n", " a1 = yi * Pi[2,:] - Pi[1,:]\n", " a2 = xj * Pj[2,:] - Pj[0,:]\n", " a3 = yj * Pj[2,:] - Pj[1,:]\n", " \n", " A = np.vstack((a0, a1, a2, a3)) \n", " U, s, VT = la.svd(A)\n", " V = VT.T \n", " \n", " X3d = V[:,-1] \n", " \n", " return X3d/X3d[3]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Depth of points\n", "\n", "Result 6.1 in [Hartley and Zisserman's book](http://www.robots.ox.ac.uk/~vgg/hzbook/)) says:\n", "\n", "> Let $\\mathbf{X} = (X, Y, Z, T)^\\intercal$ be a 3D point and $\\mathrm{\\tt P} = [\\mathrm{\\tt M} | \\mathbf{p}_4]$\n", "> be a camera matrix for a finite camera. Suppose $\\mathrm{\\tt P}(X, Y, Z, T)^\\intercal = w(x,y, 1)^\\intercal$.\n", "> Then\n", ">\n", "> $\\mathrm{depth}(\\mathbf{X}, \\mathrm{\\tt P}) = \\frac{\\mathrm{sign}(\\det \\mathrm{\\tt M})w}{T\\|\\mathbf{m}^3\\|}$\n", ">\n", "> is the depth of the point $\\mathbf{X}$ in front of the principal plane of the camera.\n", "\n", "In the result above, $\\mathrm{\\tt M}$ corresponds to a view on the three first columns of $\\mathrm{\\tt P}$ and $\\mathbf{m}^3$ is the last row of $\\mathrm{\\tt M}$. The function below takes $\\mathbf{X}$ and $\\mathrm{\\tt P}$ and applies this result to compute the depth of $\\mathbf{X}$." ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def depth(X, P):\n", " T = X[3]\n", " M = P[:,0:3]\n", " p4 = P[:,3]\n", " m3 = M[2,:]\n", " \n", " x = np.dot(P, X)\n", " w = x[2]\n", " X = X/w\n", " return (np.sign(la.det(M)) * w) / (T*la.norm(m3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Getting the projection matrices from $\\mathrm{\\tt E}$\n", "\n", "The code below implements the method described by Nistér (see *[An efficient solution to the five-point relative pose problem](http://dx.doi.org/10.1109/TPAMI.2004.17)*, section 3.1)." ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def get_proj_matrices(E, K, xi, xj):\n", " hg = lambda x : np.array([x[0], x[1], 1])\n", " W = np.array([[0., -1., 0.],\n", " [1., 0., 0.],\n", " [0., 0., 1.]])\n", "\n", " Pi = np.dot(K, np.hstack( (np.identity(3), np.zeros((3,1))) ))\n", "\n", " U, s, VT = la.svd(E)\n", " u3 = U[:,2].reshape(3,1)\n", "\n", " # Candidates\n", " Pa = np.dot(K, np.hstack((np.dot(U, np.dot(W ,VT)), u3)))\n", " Pb = np.dot(K, np.hstack((np.dot(U, np.dot(W ,VT)), -u3)))\n", " Pc = np.dot(K, np.hstack((np.dot(U, np.dot(W.T ,VT)), u3)))\n", " Pd = np.dot(K, np.hstack((np.dot(U, np.dot(W.T ,VT)), -u3)))\n", "\n", " # Find the camera for which the 3D points are *in front*\n", " xxi, xxj = hg(xi[0]), hg(xj[0])\n", "\n", " Pj = None\n", " for Pk in [Pa, Pb, Pc, Pd]:\n", " Q = dlt_triangulation(xxi, Pi, xxj, Pk) \n", " if depth(Q, Pi) > 0 and depth(Q, Pk) > 0:\n", " Pj = Pk\n", " break\n", "\n", " assert(Pj is not None)\n", " \n", " return Pi, Pj" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "P1, P2 = get_proj_matrices(E, K, inlier_i, inlier_j)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1.52040000e+03 0.00000000e+00 3.02320000e+02 0.00000000e+00]\n", " [ 0.00000000e+00 1.52590000e+03 2.46870000e+02 0.00000000e+00]\n", " [ 0.00000000e+00 0.00000000e+00 1.00000000e+00 0.00000000e+00]]\n" ] } ], "source": [ "print P1" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ -1.42505476e+03 4.70752545e+01 -6.08289727e+02 1.52545806e+03]\n", " [ 7.78012848e+00 -1.52389286e+03 -2.58854432e+02 1.99731458e+02]\n", " [ 2.05743507e-01 5.27826740e-03 -9.78591717e-01 3.50422051e-01]]\n" ] } ], "source": [ "print P2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Recovering the 3D points using DLT triangulation" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "X = []\n", " \n", "for xxi, xxj in zip(inlier_i, inlier_j):\n", " X_k = dlt_triangulation(hg(xxi), P1, hg(xxj), P2)\n", " X.append(X_k)\n", "X = np.array(X)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 3D plotting with Matplotlib" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "num_pix = X.shape[0]\n", "pix_color = [rcolor() for k in range(num_pix)]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "pix = np.dot(P2, X.T).T\n", "pix = np.divide(pix, pix[:,2].reshape(num_pix, -1))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The 3D plot below is *static* because of the inline plotting. Other Matplotlib's backends allow us to rotate the 3D plot, letting us to inspect the results." ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " this.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width);\n", " canvas.attr('height', height);\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('