~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/doc/tutorials/ml/introduction_to_pca/introduction_to_pca.markdown

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Introduction to Principal Component Analysis (PCA) {#tutorial_introduction_to_pca}
 
2
=======================================
 
3
 
 
4
Goal
 
5
----
 
6
 
 
7
In this tutorial you will learn how to:
 
8
 
 
9
-   Use the OpenCV class @ref cv::PCA to calculate the orientation of an object.
 
10
 
 
11
What is PCA?
 
12
--------------
 
13
 
 
14
Principal Component Analysis (PCA) is a statistical procedure that extracts the most important features of a dataset.
 
15
 
 
16
![](images/pca_line.png)
 
17
 
 
18
Consider that you have a set of 2D points as it is shown in the figure above. Each dimension corresponds to a feature you are interested in. Here some could argue that the points are set in a random order. However, if you have a better look you will see that there is a linear pattern (indicated by the blue line) which is hard to dismiss. A key point of PCA is the Dimensionality Reduction. Dimensionality Reduction is the process of reducing the number of the dimensions of the given dataset. For example, in the above case it is possible to approximate the set of points to a single line and therefore, reduce the dimensionality of the given points from 2D to 1D.
 
19
 
 
20
Moreover, you could also see that the points vary the most along the blue line, more than they vary along the Feature 1 or Feature 2 axes. This means that if you know the position of a point along the blue line you have more information about the point than if you only knew where it was on Feature 1 axis or Feature 2 axis.
 
21
 
 
22
Hence, PCA allows us to find the direction along which our data varies the most. In fact, the result of running PCA on the set of points in the diagram consist of 2 vectors called _eigenvectors_ which are the _principal components_ of the data set.
 
23
 
 
24
![](images/pca_eigen.png)
 
25
 
 
26
The size of each eigenvector is encoded in the corresponding eigenvalue and indicates how much the data vary along the principal component. The beginning of the eigenvectors is the center of all points in the data set. Applying PCA to N-dimensional data set yields N N-dimensional eigenvectors, N eigenvalues and 1 N-dimensional center point. Enough theory, let’s see how we can put these ideas into code.
 
27
 
 
28
How are the eigenvectors and eigenvalues computed?
 
29
--------------------------------------------------
 
30
 
 
31
The goal is to transform a given data set __X__ of dimension _p_ to an alternative data set __Y__ of smaller dimension _L_. Equivalently, we are seeking to find the matrix __Y__, where __Y__ is the _Karhunen–Loève transform_ (KLT) of matrix __X__:
 
32
 
 
33
\f[ \mathbf{Y} = \mathbb{K} \mathbb{L} \mathbb{T} \{\mathbf{X}\} \f]
 
34
 
 
35
__Organize the data set__
 
36
 
 
37
Suppose you have data comprising a set of observations of _p_ variables, and you want to reduce the data so that each observation can be described with only _L_ variables, _L_ < _p_. Suppose further, that the data are arranged as a set of _n_ data vectors \f$ x_1...x_n \f$ with each \f$ x_i \f$  representing a single grouped observation of the _p_ variables.
 
38
 
 
39
- Write \f$ x_1...x_n \f$ as row vectors, each of which has _p_ columns.
 
40
- Place the row vectors into a single matrix __X__ of dimensions \f$ n\times p \f$.
 
41
 
 
42
__Calculate the empirical mean__
 
43
 
 
44
- Find the empirical mean along each dimension \f$ j = 1, ..., p \f$.
 
45
 
 
46
- Place the calculated mean values into an empirical mean vector __u__ of dimensions \f$ p\times 1 \f$.
 
47
 
 
48
  \f[ \mathbf{u[j]} = \frac{1}{n}\sum_{i=1}^{n}\mathbf{X[i,j]} \f]
 
49
 
 
50
__Calculate the deviations from the mean__
 
51
 
 
52
Mean subtraction is an integral part of the solution towards finding a principal component basis that minimizes the mean square error of approximating the data. Hence, we proceed by centering the data as follows:
 
53
 
 
54
- Subtract the empirical mean vector __u__ from each row of the data matrix __X__.
 
55
 
 
56
- Store mean-subtracted data in the \f$ n\times p \f$ matrix __B__.
 
57
 
 
58
  \f[ \mathbf{B} = \mathbf{X} - \mathbf{h}\mathbf{u^{T}} \f]
 
59
 
 
60
  where __h__ is an \f$ n\times 1 \f$ column vector of all 1s:
 
61
 
 
62
  \f[ h[i] = 1, i = 1, ..., n \f]
 
63
 
 
64
__Find the covariance matrix__
 
65
 
 
66
- Find the \f$ p\times p \f$ empirical covariance matrix __C__ from the outer product of matrix __B__ with itself:
 
67
 
 
68
  \f[ \mathbf{C} = \frac{1}{n-1} \mathbf{B^{*}} \cdot \mathbf{B} \f]
 
69
 
 
70
  where * is the conjugate transpose operator. Note that if B consists entirely of real numbers, which is the case in many applications, the "conjugate transpose" is the same as the regular transpose.
 
71
 
 
72
__Find the eigenvectors and eigenvalues of the covariance matrix__
 
73
 
 
74
- Compute the matrix __V__ of eigenvectors which diagonalizes the covariance matrix __C__:
 
75
 
 
76
  \f[ \mathbf{V^{-1}} \mathbf{C} \mathbf{V} = \mathbf{D} \f]
 
77
 
 
78
  where __D__ is the diagonal matrix of eigenvalues of __C__.
 
79
 
 
80
- Matrix __D__ will take the form of an \f$ p \times p \f$ diagonal matrix:
 
81
 
 
82
  \f[ D[k,l] = \left\{\begin{matrix} \lambda_k, k = l \\ 0, k \neq l \end{matrix}\right. \f]
 
83
 
 
84
  here, \f$ \lambda_j \f$ is the _j_-th eigenvalue of the covariance matrix __C__
 
85
 
 
86
- Matrix __V__, also of dimension _p_ x _p_, contains _p_ column vectors, each of length _p_, which represent the _p_ eigenvectors of the covariance matrix __C__.
 
87
- The eigenvalues and eigenvectors are ordered and paired. The _j_ th eigenvalue corresponds to the _j_ th eigenvector.
 
88
 
 
89
@note sources [[1]](https://robospace.wordpress.com/2013/10/09/object-orientation-principal-component-analysis-opencv/), [[2]](http://en.wikipedia.org/wiki/Principal_component_analysis) and special thanks to Svetlin Penkov for the original tutorial.
 
90
 
 
91
Source Code
 
92
-----------
 
93
 
 
94
This tutorial code's is shown lines below. You can also download it from
 
95
    [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp).
 
96
@include cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp
 
97
 
 
98
@note Another example using PCA for dimensionality reduction while maintaining an amount of variance can be found at [opencv_source_code/samples/cpp/pca.cpp](https://github.com/Itseez/opencv/tree/master/samples/cpp/pca.cpp)
 
99
 
 
100
Explanation
 
101
-----------
 
102
 
 
103
-#  __Read image and convert it to binary__
 
104
 
 
105
    Here we apply the necessary pre-processing procedures in order to be able to detect the objects of interest.
 
106
    @snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pre-process
 
107
 
 
108
-#  __Extract objects of interest__
 
109
 
 
110
    Then find and filter contours by size and obtain the orientation of the remaining ones.
 
111
    @snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp contours
 
112
 
 
113
-#  __Extract orientation__
 
114
 
 
115
    Orientation is extracted by the call of getOrientation() function, which performs all the PCA procedure.
 
116
    @snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pca
 
117
 
 
118
    First the data need to be arranged in a matrix with size n x 2, where n is the number of data points we have. Then we can perform that PCA analysis. The calculated mean (i.e. center of mass) is stored in the _cntr_ variable and the eigenvectors and eigenvalues are stored in the corresponding std::vector’s.
 
119
 
 
120
-#  __Visualize result__
 
121
 
 
122
    The final result is visualized through the drawAxis() function, where the principal components are drawn in lines, and each eigenvector is multiplied by its eigenvalue and translated to the mean position.
 
123
    @snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization
 
124
    @snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization1
 
125
 
 
126
Results
 
127
-------
 
128
 
 
129
The code opens an image, finds the orientation of the detected objects of interest and then visualizes the result by drawing the contours of the detected objects of interest, the center point, and the x-axis, y-axis regarding the extracted orientation.
 
130
 
 
131
![](images/pca_test1.jpg)
 
132
 
 
133
![](images/output.png)