Setting up IPython Notebook on Windows

Update (Feb 2016): These instructions are now outdated.
The recommended way to install Jupyter (new name for IPython Notebook) is now using Anaconda.

IPython Notebook is a great tool for prototyping algorithms and analysing data interactively, in particular in combination with powerful numerical libraries such as NumPy, SciPy, matplotlib and OpenCV. But one step after the other.

The IPython install page mentions Python distributions such as Anaconda or Enthought Canopy that come with many packages pre-installed, but I prefer to install all bits from scratch, so I know what’s installed.

So let’s get started on how to install IPython Notebook on Windows 7. Obviously we need admin rights for all of this.

  1. Install Python
    A late 2-series Python, such as 2.7.5, provides the best compatibility with most packages, although support for Python 3 is continuously improving. I also stick to the Win32 (x86) version of Python as a few packages are not available in x64 versions, although the major packages do support this. My favourite Python distribution for Windows is ActivePython. Note that its default install path is C:\Python27\, which should be changed, e.g. to C:\Program Files (x86)\ActivePython 2.7.5\.
  2. Install IPython
    The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd, shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.
  3. Install libraries
    Download and run the official installers for the latest stable versions of NumPy (numerical routines), SciPy (scientific computing) and matplotlib (graphing library). Make sure you pick the version that fits your Python distribution (e.g. win32 python2.7). Note that matplotlib requires additional dependencies that can be installed using easy_install python-dateutil pyparsing .
  4. Install OpenCV bindings (optional)
    If one works with image processing or computer vision, the Python bindings of OpenCV can be really useful. The python bindings are included in the main OpenCV installer under the path ./build/python/, but as of OpenCV 2.4.8 only Python 2.7 is included as precompiled Python binary (but for both 32 and 64-bit versions). The file “cv2.pyd” needs to be manually copied to the “site-packages” folder of the Python distribution, e.g. at C:\Program Files (x86)\ActivePython 2.7.5\Lib\site-packages\.
  5. Start IPython Notebook
    Run ipython notebook in a command line. If you’re new to IPython Notebook, get started by looking at the example collection and reading the documentation.
  6. Install MathJax locally (optional)
    Typesetting LaTeX within a notebook uses MathJax, which can be installed locally by running the following in Python:

    from IPython.external.mathjax import install_mathjax
    install_mathjax()

As of March 2014, the latest versions of libraries were: IPython 1.2.1, NumPy 1.8.0, SciPy 0.13.3, matplotlib 1.3.1, OpenCV 2.4.8.

Moving a Qt installation directory

After adding a new drive to my machine and re-installing Windows, the path of my custom-built Qt directory changed from D:\Qt\4.8.3 to E:\Qt\4.8.3. CMake cannot use it in the new location, because it calls qmake -query which returns the old paths which are hard-coded into the Qt binaries.

The solution is simple: create a qt.conf file in the same directory as the qmake binary containing:

[Paths]
Prefix = E:/Qt/4.8.3

The corresponding Qt documentation page has more details.

OpenCV with C++11 on OS X 10.8

The new C++11 standard includes many language and library features that make programming in C++ more enjoyable, such as lambdas, the auto keyword and smart pointers (in the STL). Visual Studio 2010 already supports some of these features out of the box, and Visual Studio 2012 implements even more (detailed list of features implemented by different compilers).

Here is how it works with Xcode 4.5 under OS X 10.8 Mountain Lion. All one needs to do in Xcode is:

  1. Enable the C++11 language features in the “build settings” pane. Under “Apple LLVM compiler 4.1 – Language” (select “All” instead of “Basic” settings first), set “C++ Language Dialect” to “C++11 [-std=c++11]”.
  2. Select ‘libc++’ as the standard library by setting “C++ Standard Library” to “libc++ (LLVM C++ standard library with C++11 support)” in the same section.

This second step is necessary because the new C++11 features depend on library support in the standard library, and the default standard library ‘libstdc++’ does not implement them, but ‘libc++’ does.

For a standalone project, all is well now. However, real projects have external dependencies, which is where it gets complicated. When libraries such as OpenCV are compiled from scratch with default settings, they link to the default standard library ‘libstdc++’. This causes linking errors when one wants to use it in a project that uses the new C++11 features and links to ‘libc++’.

Compiling OpenCV with libc++

The solution is to compile OpenCV with the same standard library (that is ‘libc++’). To do this, first download the latest version of OpenCV from opencv.org (I used OpenCV 2.4.2) and uncompress it.

Next, one could run CMake as usual to create Xcode projects, and then change the project settings mentioned above manually. However, one would need to redo this every time CMake is run, because it overwrites the Xcode project files.

A better approach is thus to apply these settings inside of the root CMakeLists.txt file of OpenCV. Find the following line (at line 348 in OpenCV 2.4.2):

add_definitions(-DHAVE_ALLOCA -DHAVE_ALLOCA_H -DHAVE_LIBPTHREAD -DHAVE_UNISTD_H)

and insert the following after it:

 message("Setting up Xcode for C++11 with libc++.")
 set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
 set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")

This sets up the projects for C++11 with the libc++ standard library.

Now configure OpenCV using CMake: choose the “Xcode” generator with the default native compilers.

Unfortunately, building the projects in Xcode fails in project “opencv_ts” (related OpenCV bug):

OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:1657:13: 'tr1/tuple' file not found
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:9801:34: No member named 'tr1' in namespace 'std'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:9801:44: Expected ')'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:9802:16: Use of undeclared identifier 't'
[...]

It turns out that these problems are caused by Google Test which is not compatible with C++11 yet. Sylvain Pointeau has a simple workaround for that (adding the line #define GTEST_USE_OWN_TR1_TUPLE 1 to the top of ts_gtest.h). However, this doesn’t help with subsequent problems in “opencv_perf_imgproc” and “opencv_perf_video”, which confuse C++11’s std::tuple with the built-in std::tr1::tuple:

OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:10847:24: No matching conversion for static_cast from 'const std::__1::tuple<double, double>' to 'std::tr1::tuple<double, double, void, void, void, void, void, void, void, void>'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:10847:45: No matching conversion for static_cast from 'const std::__1::tuple<double, double>' to 'std::tr1::tuple<double, double, void, void, void, void, void, void, void, void>'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:10848:9: No matching conversion for static_cast from 'const std::__1::tuple<double, double>' to 'std::tr1::tuple<double, double, void, void, void, void, void, void, void, void>'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:10828:24: No matching conversion for static_cast from 'const std::__1::tuple<int, int>' to 'std::tr1::tuple<int, int, void, void, void, void, void, void, void, void>'
OpenCV-2.4.2/modules/ts/include/opencv2/ts/ts_gtest.h:10828:45: No matching conversion for static_cast from 'const std::__1::tuple<int, int>' to 'std::tr1::tuple<int, int, void, void, void, void, void, void, void, void>'

Instead, it is better to patch Google Test for libc++-compatibility. A simplified version of this patch is to replace the following line (in line 1657 in OpenCV 2.4.2):

#   include <tr1/tuple>  // NOLINT

with

// C++11 puts its tuple into the ::std namespace rather than ::std::tr1.
// gtest expects tuple to live in ::std::tr1, so put it there.
#include <tuple>  // NOLINT
namespace std {
    namespace tr1 {
        using ::std::get;
        using ::std::make_tuple;
        using ::std::tuple;
        using ::std::tuple_element;
        using ::std::tuple_size;
    }
}

This leaves one more compilation error:

OpenCV-2.4.2/modules/flann/include/opencv2/flann/lsh_table.h:196:14: Use of undeclared identifier 'use_speed_'

The suggested solution is to just delete the if (!use_speed_), which works fine.

Everything else should now compile and link cleanly.