Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to...

16
Introduction to OpenCV and Python Antonino Furnari [email protected] www.dmi.unict.it/~furnari/ Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Transcript of Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to...

Page 1: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Introduction toOpenCV and Python

Antonino Furnari

[email protected]

www.dmi.unict.it/~furnari/

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 2: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

OpenCV – Overview

● Initially launched in 1999 as an Intel Researchinitiative to advance CPU-intensive applications,later supported by Willow Garage and nowsupported by Itseez;

● Cross platform, free for use and released under theBDS licence (releasing the application with an opensource licence is not mandatory!);

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 3: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

OpenCV – Goals

➢ Advance vision research by providing open andoptimized cod for basic vision. No morereinventing the wheel!

➢ Provide a common infrastructure for developers;

➢ Advance vision-based commercial applications bymaking portable, performance-optimized codeavailable for free.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 4: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

OpenCV – Platforms

➢ Supports Windows, Linux, macOS, FreeBSD,NetBSD, OpenBSD, Android, iOS, Maemo,BlackBerry 10;

➢ Written in C++ with bindings for Python, Java,MATLAB/OCTAVE, C#, Perl, Ch, Haskell and Ruby;

➢ CUDA and OpenCL based GPU interfaces are inprogress.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 5: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

OpenCV – Main Modules

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

core basic data structures and basic functions used by all other modules

imgproc image filtering, geometrical transformations, color space conversion

imgcodecs Image file reading and writing

videoio Media Input/Output

highgui high level user interface

video video analysis, motion estimation, background subtraction, tracking

calib3d single and stereo camera calibration, 3D reconstruction

features2d feature extraction, descrition and matching

objdetect object detection

ml machine learning and pattern recognition (e.g., k-means, SVM, knn)

superres super resolution

stitching images stitching

superres super resolution

videostab video stabilization

Page 6: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Prototyping vs Developing

In the past, Computer Vision researchers used MATLABfor prototyping and C++/OpenCV for deployment.MATLAB is excellent to rapidly test new ideas:

✓fast to code and debug;✓extensive library for (practically) everything;X slow to execute;X makes it hard to organize your code and is not great for big

projects.

C++/OpenCV is great to build optimize software:✓offers optimized implementation for many CV algorithms;✓allows to easily scale to big projects;X slow to codeX makes debugging very hard

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 7: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Python + OpenCV(one ring to rule them all)

We will use Python + OpenCV to get the best of bothworlds.

Python is good for both prototyping anddevelopment:

• easy to learn, easy to write and read code;

• with IPython and scipy, it is powerful for scientificcalculus (like MATLAB!);

• can be used for scripting, prototyping, scientificcomputation and development in a very natural way;

• Libraries (practically) for everything;

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 8: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Note about versions

You can install different versions of Python and OpenCV. You are free to experiment, but we willconsider the following setup:

• Python 2.7: is the most «mature» branch of Python;

• OpenCV 2.4.11: is the OpenCV version with the best documentation: http://docs.opencv.org/2.4/.

• You will also need the scipy stack including:• numpy for scientific calculus (matrix operations etc.);

• matplotlib to plot data and show images;

• IPython provides an interactive shell for prototyping.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 9: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Scipy + OpenCV installation

The easiest way to install the scipy stack is to choose aPython distribution. We will consider Anaconda,provided by continuum analytics:

• Download Anaconda 4.3 (Python 2.7 version) fromhttps://www.continuum.io/;

• Install OpenCV 2.4.11 from anaconda prompt:

conda install -c menpo opencv=2.4.11

This will install a number of tools, including:• Anaconda prompt;• IPython console;• Jupyter notebooks;• Spyder;

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 10: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

IPython

The core of a scipy distribution is IPython. It providesa powerful interactive shell in which you can do everything Python can do.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

However, it is usuallyconvenient to use more sophisticated user interfaces such asan IDE.

Page 11: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Python: two paths you can go by(but in the long run there’s still time to change the road you’re on)

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

full featured IDE Jupyter notebooks

Page 12: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Full featured IDE

An IDE, similar to MATLAB,such as Spyder (installedwith Anaconda by default).Other choices are alsoavailable.

The workflow is similar tothose of all interpretedlanguages: write your .pyfile, execute, debug etc.

Good for development.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 13: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Jupyter Notebooks

A web interface whichallows you to write«notebooks», i.e., filescontaining both the code,the obtained results andformatted text.

Powerful and flexible.

Very good for learning andexperimenting.

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 14: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

IDE vs Notebook

• IDEs are good for:• big projects with many different modules and classes;

• interactive software (e.g., processing a video stream inrealtime);

• Notebooks are good for:• experimenting new ideas;

• documenting them;

• practical sessions;

• assigments;

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 15: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Getting Started with Python + OpenCV

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania

Page 16: Introduction to OpenCV and Python - Unict · MATLAB/OCTAVE, C#, Perl, Ch, ... Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania. Scipy

Resources

• IPython documentation: http://ipython.org/ipython-doc/stable/index.html

• Numpy quickstart tutorial: https://docs.scipy.org/doc/numpy/user/quickstart.html

• Matplotlib documentation: http://matplotlib.org/contents.html

• OpenCV documentation: http://docs.opencv.org/2.4/index.html

Introduction to OpenCV and Python - Computer Vision 2016/2017 - IPLAB - University of Catania