Академический Документы
Профессиональный Документы
Культура Документы
Marco Moltisanti
Image Processing Lab
Dipartimento di Matematica e Informatica
Universit`
a degli Studi di Catania
moltisanti@dmi.unict.it
Intro
Resources
What?
M. Moltisanti OpenCV
2/27
Intro
Resources
What?
M. Moltisanti OpenCV
2/27
Intro
Resources
What?
M. Moltisanti OpenCV
2/27
Intro
Resources
What?
M. Moltisanti OpenCV
2/27
Intro
Resources
Where?
Download;
M. Moltisanti OpenCV
3/27
Intro
Resources
Where?
Download;
Documentation;
M. Moltisanti OpenCV
3/27
Intro
Resources
Where?
Download;
Documentation;
Tutorials.
M. Moltisanti OpenCV
3/27
Intro
Resources
Other Resources
M. Moltisanti OpenCV
4/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
M. Moltisanti OpenCV
5/27
Library structure
Main Packages
core Basic functionalities and data structures;
imgproc Image processing functions (blurring, histograms,
registration, tracking, detection);
highgui High-level Graphical User Interface;
calib3d Camera calibration and 3D Reconstruction;
features2d Features detection and description;
objdetect Object detection;
ml Machine Learning and Pattern Recognition tools (e.g.
k-means, SVM, knn)
Data structures and functions belong to the namespace cv; therefore, to
access this functionality from your code, use the cv:: specifier or using
namespace cv; directive
M. Moltisanti OpenCV
5/27
Library structure
Directory structure
Let CVHOME be the folder where you have installed OpenCV.
CVHOME
bin
doc
include
opencv.....................old versions C/C++ header files
opencv2...................new versions C/C++ header files
lib
opencv {package name } {version }d.lib........Debug libs
opencv {package name } {version }.lib.......Release libs
Figure : OpenCV directory tree
M. Moltisanti OpenCV
6/27
Library structure
Example
Example
If you have installed OpenCV 2.4.2, the core functions and data
structures are declared in include/opencv2/core/core.hpp and can be
found in the library opencv core 242d.lib (Debug) and
opencv core 242.lib (Release)
M. Moltisanti OpenCV
7/27
Library structure
M. Moltisanti OpenCV
8/27
Library structure
Local method
(1) Create a new, blank, C++ Project;
(2) Open the Property Manager ;
(3) Right click on Debug, then click on Add New Property Sheet;
(4) Apply the Good old method and save the property sheet;
(5) Do steps 2 and 3 for Release;
(6) Next time you want to create a new OpenCV-based project, just
click on Add Existing Property Sheet.
M. Moltisanti OpenCV
9/27
Library structure
M. Moltisanti OpenCV
10/27
Library structure
Basic classes
M. Moltisanti OpenCV
11/27
Library structure
Basic classes
M. Moltisanti OpenCV
11/27
Library structure
Basic classes
11/27
Library structure
Basic classes
11/27
Library structure
Basic classes
...
P o i n t p1 ( 1 0 , 1 0 ) ;
P o i n t 2 i p2 ( 1 1 , 1 1 ) ;
P o i n t 2 d p3 ( 1 . 0 , 1 . 0 ) ;
P o i n t 2 f p4 ( 2 . 0 f , 2 . 0 f ) ;
6
7
8
9
P o i n t 3 i p5 ( 1 0 , 1 0 , 1 0 ) ;
P o i n t 3 d p6 ( 1 . 0 , 1 . 0 , 1 . 0 ) ;
P o i n t 3 f p7 ( 1 . 0 f , 1 . 0 f , 1 . 0 f ) ;
10
11
12
13
14
R e c t <i n t > r 1 ( 1 , 1 , 1 0 0 , 1 0 0 ) ;
R e c t <i n t > r 2 ( p1 , p2 ) ;
R e c t <d o u b l e > r 3 ( p3 , p3 1 0 . 0 ) ;
...
Listing 1: Ex01.cpp
M. Moltisanti OpenCV
12/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
a filter;
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
a filter;
an image;
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
a filter;
an image;
a set of vectors (e.g. descriptors);
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
a filter;
an image;
a set of vectors (e.g. descriptors);
etc.
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Class Mat
Mat is the fundamental class for doing everything in OpenCV. Mat can
represent:
a matrix;
a filter;
an image;
a set of vectors (e.g. descriptors);
etc.
A Mat object should always be released before exiting the program using
the Mat::release() function!
M. Moltisanti OpenCV
13/27
Library structure
Basic classes
Mat as an image
void loadImage () {
3
4
5
6
7
Listing 2: Ex02-Img.cpp
M. Moltisanti OpenCV
14/27
Library structure
Basic classes
Mat as a matrix
...
#i n c l u d e <o p e n c v 2 / i m g p r o c / i m g p r o c . hpp>
3 . . .
4 void rotationMatrix () {
5
Mat r o t ;
6
rot = getRotationMatrix2D ( Point2f (0.0 f , 0.0 f ) , 30.0 ,
1.0) ;
1
2
8
9
10
11
12
13
14
Listing 3: Ex02-Mat.cpp
M. Moltisanti OpenCV
15/27
Library structure
Basic classes
Mat as a filter
...
#i n c l u d e <o p e n c v 2 / i m g p r o c / i m g p r o c . hpp>
3 . . .
4 void
c r e a t e F i l t e r () {
1
2
Mat f l t ;
f l t = getGaussianKernel (5 , 2.0) ;
6
7
8
9
10
11
12
13
14
Listing 4: Ex02-Flt.cpp
M. Moltisanti OpenCV
16/27
Library structure
Basic classes
Mat as a set
1
2
3
4
5
void createObservationsSet () {
Mat d a t a ( 1 0 , 5 , CV 32S ) ;
f o r ( i n t i =0; i <10; i ++) {
f o r ( i n t j =0; j < 5 ; j ++)
d a t a . at<i n t >( i , j ) = r a n d ( ) 2 5 6 ;
7
8
9
10
11
12
13
14
Listing 5: Ex02-Set.cpp
M. Moltisanti OpenCV
17/27
Library structure
Basic classes
M. Moltisanti OpenCV
18/27
M. Moltisanti OpenCV
19/27
M. Moltisanti OpenCV
19/27
M. Moltisanti OpenCV
19/27
M. Moltisanti OpenCV
19/27
M. Moltisanti OpenCV
19/27
//Show t h e image
cv : : imshow ( Lena , l e n a ) ;
7 cv : : w a i t K e y ( 0 ) ;
5
6
// S e t params
s t d : : v e c t o r <i n t > j p g p a r a m ;
11 j p g p a r a m . p u s h b a c k ( CV IMWRITE JPEG QUALITY ) ;
12 j p g p a r a m . p u s h b a c k ( 5 0 ) ;
9
10
13
// W r i t e t h e image
cv : : i m w r i t e ( l e n a . j p g , l e n a , j p g p a r a m ) ;
16 . . .
14
15
Listing 6: Ex03.cpp
M. Moltisanti OpenCV
19/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
M. Moltisanti OpenCV
20/27
Image Processing
4
5
6
//Show t h e o r i g i n a l image
imshow ( Lena o r i g i n a l , l e n a ) ;
7
8
9
// C r e a t e t h e o u t p u t image
Mat l e n a c a n n y = l e n a . c l o n e ( ) ;
10
11
...
12
// C r e a t e and a p p l y t h e f i l t e r
Canny ( l e n a , l e n a c a n n y , m[ 0 ] 0 . 6 6 , m[ 0 ] 1 . 3 3 ) ;
15 . . .
13
14
Listing 7: Ex04.cpp
M. Moltisanti OpenCV
20/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
11
12
13
14
15
i f ( r e s == q )
break ;
}
vc . r e l e a s e ( ) ;
Listing 8: Ex05-In.cpp
M. Moltisanti OpenCV
21/27
Image Processing
Video Handling
The difference between using a video from file and a video from a device is
only in the constructor. Instead of VideoCapture(id) function, we will
use the overloaded version VideoCapure(filename), where filename is
the path to a video file.
Example
VideoCapture vc(video.mp4)
M. Moltisanti OpenCV
22/27
Image Processing
Video Handling
Save a video
M. Moltisanti OpenCV
23/27
Image Processing
Video Handling
Save a video
M. Moltisanti OpenCV
23/27
Image Processing
Video Handling
Save a video
M. Moltisanti OpenCV
23/27
Image Processing
Video Handling
Save a video
1
3
4
while (1) {
...
f l i p ( frame , newFrame , 1 ) ;
vw << newFrame ;
imshow ( I t s s o m e t h i n g ! , newFrame ) ;
r e s = ( char ) waitKey (1000/ f p s ) ;
...
6
7
8
9
10
11
12
Listing 9: Ex05-Out.cpp
M. Moltisanti OpenCV
23/27
Machine Learning
Available methods
Statistical Models;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
Boosting;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
Boosting;
Gradient Boosted Trees;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
Boosting;
Gradient Boosted Trees;
Random Trees;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
Boosting;
Gradient Boosted Trees;
Random Trees;
EM algorithm;
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Statistical Models;
Normal Bayes Classifier;
K-NN;
SVM;
Decision Trees;
Boosting;
Gradient Boosted Trees;
Random Trees;
EM algorithm;
Neural Networks.
M. Moltisanti OpenCV
24/27
Machine Learning
Available methods
Include Directive
Include path: <opencv2\ml\ml.hpp>
Documentation is fundamental using ml functions!
M. Moltisanti OpenCV
24/27
In conclusion...
Tip #3
Dont try to copy!
M. Moltisanti OpenCV
25/27
In conclusion...
M. Moltisanti OpenCV
26/27
In conclusion...
Question time!
M. Moltisanti OpenCV
27/27