Beruflich Dokumente
Kultur Dokumente
• OpenCV was designed for computational efficiency and with a strong focus on real-time
applications. It is written in optimized C++ and can take advantage of multicore
processors. => IT IS FAST
• The library is written in C and C++ and runs under Linux, Windows, and Mac OS X. There
is active development on interfaces for Python, Java, MATLAB, and other languages,
including porting the library to Android and iOS for mobile applications. => GOOD
PORTABILITY
• One of OpenCV’s goals is to provide a simple-to-use computer vision infrastructure that
helps people build fairly sophisticated vision applications quickly. The OpenCV library
contains over 500 functions that span many areas in vision, including factory product
inspection, medical imaging, security, user interface, camera calibration, stereo vision,
and robotics. => DIVERSE USAGE and provides good starting points in each area
• Functions are well documented, but they lack in example diversity.
• OpenCV is not great if you just want to manipulate images.
• OpenCV is great for teaching the computer how to see something!
Learning OpenCV x.x – A.Kaehler, G. Bradski
• The intent of this book is tutorial
• Supplemental material (code examples, exercises, etc.) is available for download
at https://github.com/oreillymedia/Learning-OpenCV-3_examples.
• The purpose of this book is to:
• Comprehensively document OpenCV by detailing what function calling conventions really
mean and how to use them correctly
• Give the reader an intuitive understanding of how the vision algorithms work
• Give the reader some sense of what algorithm to use and when to use it
• Give the reader a boost in implementing computer vision and machine learning algorithms by
providing many working code examples to start from
• Suggest ways to fix some of the more advanced routines when something goes wrong
OpenCV Block Diagram
• OpenCV is built in layers. At the top is the OS
under which OpenCV operates.
• Next comes the language bindings and sample
applications.
• Below that is the contributed code in
opencv_contrib, which contains mostly higher-
level functionality.
• After that is the core of OpenCV, and at the
bottom are the various hardware
optimizations in the hardware acceleration
layer (HAL).
OpenCV contrib
• Face Recognition
• Object Tracking
• DNN – Deep Neural Networks – Using Caffe framework models
• SFM – Structure From Motion
• Structured Light
• Multi-camera Calibration
• Stereo Matching
• ...
Installing OpenCV 3.2
• Using prebuilt OpenCV binaries [Win]
• tutorial from docs.opencv.org - it is made for v2.4 but works for all newer
• In order to use them in Visual Studio include files and libraries need to be included/linked
• libraries “as is”, if we want some additional function we need to rebuild sources
• NuGet
• nuget.org
• package manager for the Microsoft development platform
• works only for VS14 and older
• very simple and fast OpenCV installation, but not flexible
• recommendation: install opencv3.1 package
• Building OpenCV from source
• same tutorial from above
• rather complicated on Windows, linux provides much easier interface
• download OCV source code -> install Cmake
• install additional libraries and download opencv-contrib
• use cmake to configure and generate build, then VS to compile
Installing OpenCV
• Using prebuilt OpenCV binaries [Win]
• Sourceforge page -> 3.2.0 -> -vc14.exe
• run the .exe, select path for extraction (suggested E:/MS1MV/opencv)
• set the environment variable: OPENCV_DIR
E:\MS1MV\opencv\build\x64\vc14
• add bin folder to system path: %OPENCV_DIR%\bin
• lib – static libraries
• dll – dynamic link libraries – loaded only on demand during runtime
Building applications with OCV inside the
Microsoft Visual Studio
• To build an application with OpenCV you need to do two things:
• Tell to the compiler how the OpenCV library looks. You do this by showing it
the header files.
• Tell to the linker from where to get the functions or data structures of
OpenCV, when they are needed.
• VS -> New Project - > Win32 Console Application -> give name -> OK -
> Next -> Empty Project -> Finish
• View -> Other Windows -> Property Manager
• open Property Manager view and select Debug|x64 -> Add New
Project Property Sheet
Building applications with OCV inside the
Microsoft Visual Studio
• created Property Sheet -> Properties
• OpenCV include directory (*.h and *.hpp files)
• C++ -> General -> Additional Include Directories =
$(OPENCV_DIR)\..\..\include (make sure to press Enter and Apply)
• Libraries – we want to link them
• static (*.lib)
• Linker -> General -> Additional Library Directories -> $(OPENCV_DIR)\lib
• Linker -> Input -> Additional Dependencies -> we need to write down all debug libraries
(when creating release property page, then release libraries).
• dynamic (*.dll)
• program will look for the .dll’s during runtime and if we have correctly added their path
to path environment variable it should successfully find them
Run some code!
• Create new .cpp file in Source Files
• Copy the code
#include <opencv2/opencv.hpp> //Include file for every supported OpenCV function
• cv::DataType<>
Accessing Array Elements Individually
• Through iteration
• use the iterator mechanism built into cv::Mat
• cv::MatConstIterator<> -> The cv::Mat methods begin() and end() return objects of
this type.
• This method of iteration is convenient because the iterators are smart enough to
handle the continuous packing and noncontinuous packing cases automatically, as ell
as handling any number of dimensions in the array.
Accessing Array Elements by Block
• member functions of the cv::Mat class
• row()
• col()
• rowRange() -> rowRange(i0, i1) == rowRange(cv::Range(i0,i1))
• colRange()
• m(cv::Range(i0,i1), cv::Range(j0,j1)) -> Array corresponding to the
subrectangle of matrix m with one corner at i0, j0 and the opposite corner at
(i1-1, j1-1)
• m( cv::Rect(i0,i1,w,h) ); Array corresponding to the subrectangle of matrix m
with one corner at i0, j0 and the opposite corner at (i0+w-1, j0+h-1)
Back to Sobel
• Perform Sobel edge detection step by step by accessing each
individual pixel and its neighborhood.
• S = [-1, -2, -1;
0, 0, 0;
1, 2, 1]; //horizontal edges
Algebra and cv::Mat
• Operations available for matrix expressions
• m0 + m1, m0 – m1;
• m0 + s; m0 – s; s + m0, s – m1;
• -m0;
• s * m0; m0 * s;
• m0.mul( m1 ); m0/m1; // per element
• m0 * m1; // matrix multiplication
• m0>m1; m0>=m1; m0==m1; m0<=m1; m0<m1; // per element comparison
• m0&m1; m0|m1; m0^m1; ~m0;
m0&s; s&m0; m0|s; s|m0; m0^s; s^m0 ; //bitwise logical operations
More Things an Array Can Do
• m1 = m0.clone();
• m0.copyTo( m1 );
• m0.copyTo( m1, mask ); //only entries indicated in the array mask are
copied
• m0.convertTo(m1, type, scale, offset); // Convert elements of m0 to
type (e.g., CV_32F) and write to m1 after scaling by scale (default 1.0)
and adding offset (default 0.0)
• m0.reshape( chan, rows);
• m0.empty();
Final goal – act like cats
1. Detect red circle in the image
2. Extract it from the background
3. extract its center position
4. Display the path of the circle
through the given image set