@ -0,0 +1,37 @@ |
||||
SET(OPENCV_ANNOTATION_DEPS opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio) |
||||
ocv_check_dependencies(${OPENCV_ANNOTATION_DEPS}) |
||||
|
||||
if(NOT OCV_DEPENDENCIES_FOUND) |
||||
return() |
||||
endif() |
||||
|
||||
project(annotation) |
||||
set(the_target opencv_annotation) |
||||
|
||||
ocv_target_include_directories(${the_target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${OpenCV_SOURCE_DIR}/include/opencv") |
||||
ocv_target_include_modules(${the_target} ${OPENCV_ANNOTATION_DEPS}) |
||||
|
||||
file(GLOB SRCS *.cpp) |
||||
|
||||
set(annotation_files ${SRCS}) |
||||
ocv_add_executable(${the_target} ${annotation_files}) |
||||
ocv_target_link_libraries(${the_target} ${OPENCV_ANNOTATION_DEPS}) |
||||
|
||||
set_target_properties(${the_target} PROPERTIES |
||||
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" |
||||
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} |
||||
RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} |
||||
INSTALL_NAME_DIR lib |
||||
OUTPUT_NAME "opencv_annotation") |
||||
|
||||
if(ENABLE_SOLUTION_FOLDERS) |
||||
set_target_properties(${the_target} PROPERTIES FOLDER "applications") |
||||
endif() |
||||
|
||||
if(INSTALL_CREATE_DISTRIB) |
||||
if(BUILD_SHARED_LIBS) |
||||
install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} CONFIGURATIONS Release COMPONENT dev) |
||||
endif() |
||||
else() |
||||
install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT dev) |
||||
endif() |
@ -0,0 +1,198 @@ |
||||
/*****************************************************************************************************
|
||||
USAGE: |
||||
./opencv_annotation -images <folder location> -annotations <ouput file> |
||||
|
||||
Created by: Puttemans Steven |
||||
*****************************************************************************************************/ |
||||
|
||||
#include <opencv2/core.hpp> |
||||
#include <opencv2/highgui.hpp> |
||||
#include <opencv2/imgcodecs.hpp> |
||||
#include <opencv2/videoio.hpp> |
||||
#include <opencv2/imgproc.hpp> |
||||
|
||||
#include <fstream> |
||||
#include <iostream> |
||||
|
||||
using namespace std; |
||||
using namespace cv; |
||||
|
||||
// Function prototypes
|
||||
void on_mouse(int, int, int, int, void*); |
||||
string int2string(int); |
||||
void get_annotations(Mat, stringstream*); |
||||
|
||||
// Public parameters
|
||||
Mat image; |
||||
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0, num_of_rec = 0; |
||||
bool start_draw = false; |
||||
|
||||
// Window name for visualisation purposes
|
||||
const string window_name="OpenCV Based Annotation Tool"; |
||||
|
||||
// FUNCTION : Mouse response for selecting objects in images
|
||||
// If left button is clicked, start drawing a rectangle as long as mouse moves
|
||||
// Stop drawing once a new left click is detected by the on_mouse function
|
||||
void on_mouse(int event, int x, int y, int , void * ) |
||||
{ |
||||
// Action when left button is clicked
|
||||
if(event == EVENT_LBUTTONDOWN) |
||||
{ |
||||
if(!start_draw) |
||||
{ |
||||
roi_x0 = x; |
||||
roi_y0 = y; |
||||
start_draw = true; |
||||
} else { |
||||
roi_x1 = x; |
||||
roi_y1 = y; |
||||
start_draw = false; |
||||
} |
||||
} |
||||
// Action when mouse is moving
|
||||
if((event == EVENT_MOUSEMOVE) && start_draw) |
||||
{ |
||||
// Redraw bounding box for annotation
|
||||
Mat current_view; |
||||
image.copyTo(current_view); |
||||
rectangle(current_view, Point(roi_x0,roi_y0), Point(x,y), Scalar(0,0,255)); |
||||
imshow(window_name, current_view); |
||||
} |
||||
} |
||||
|
||||
// FUNCTION : snippet to convert an integer value to a string using a clean function
|
||||
// instead of creating a stringstream each time inside the main code
|
||||
string int2string(int num) |
||||
{ |
||||
stringstream temp_stream; |
||||
temp_stream << num; |
||||
return temp_stream.str(); |
||||
} |
||||
|
||||
// FUNCTION : given an image containing positive object instances, add all the object
|
||||
// annotations to a known stringstream
|
||||
void get_annotations(Mat input_image, stringstream* output_stream) |
||||
{ |
||||
// Make it possible to exit the annotation
|
||||
bool stop = false; |
||||
|
||||
// Reset the num_of_rec element at each iteration
|
||||
// Make sure the global image is set to the current image
|
||||
num_of_rec = 0; |
||||
image = input_image; |
||||
|
||||
// Init window interface and couple mouse actions
|
||||
namedWindow(window_name, WINDOW_AUTOSIZE); |
||||
setMouseCallback(window_name, on_mouse); |
||||
|
||||
imshow(window_name, image); |
||||
stringstream temp_stream; |
||||
int key_pressed = 0; |
||||
|
||||
do |
||||
{ |
||||
// Keys for processing
|
||||
// You need to select one for confirming a selection and one to continue to the next image
|
||||
// Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
|
||||
// c = 99 add rectangle to current image
|
||||
// n = 110 save added rectangles and show next image
|
||||
// <ESC> = 27 exit program
|
||||
key_pressed = 0xFF & waitKey(0); |
||||
switch( key_pressed ) |
||||
{ |
||||
case 27: |
||||
destroyWindow(window_name); |
||||
stop = true; |
||||
case 99: |
||||
// Add a rectangle to the list
|
||||
num_of_rec++; |
||||
// Draw initiated from top left corner
|
||||
if(roi_x0<roi_x1 && roi_y0<roi_y1) |
||||
{ |
||||
temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y0) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y1-roi_y0); |
||||
} |
||||
// Draw initiated from bottom right corner
|
||||
if(roi_x0>roi_x1 && roi_y0>roi_y1) |
||||
{ |
||||
temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y1) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y0-roi_y1); |
||||
} |
||||
// Draw initiated from top right corner
|
||||
if(roi_x0>roi_x1 && roi_y0<roi_y1) |
||||
{ |
||||
temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y0) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y1-roi_y0); |
||||
} |
||||
// Draw initiated from bottom left corner
|
||||
if(roi_x0<roi_x1 && roi_y0>roi_y1) |
||||
{ |
||||
temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y1) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y0-roi_y1); |
||||
} |
||||
|
||||
rectangle(input_image, Point(roi_x0,roi_y0), Point(roi_x1,roi_y1), Scalar(0,255,0), 1); |
||||
|
||||
break; |
||||
} |
||||
|
||||
// Check if escape has been pressed
|
||||
if(stop) |
||||
{ |
||||
break; |
||||
} |
||||
} |
||||
// Continue as long as the next image key has not been pressed
|
||||
while(key_pressed != 110); |
||||
|
||||
// If there are annotations AND the next image key is pressed
|
||||
// Write the image annotations to the file
|
||||
if(num_of_rec>0 && key_pressed==110) |
||||
{ |
||||
*output_stream << " " << num_of_rec << temp_stream.str() << endl; |
||||
} |
||||
|
||||
// Close down the window
|
||||
destroyWindow(window_name); |
||||
} |
||||
|
||||
int main( int argc, const char** argv ) |
||||
{ |
||||
// Read in the input arguments
|
||||
string image_folder; |
||||
string annotations; |
||||
for(int i = 1; i < argc; ++i ) |
||||
{ |
||||
if( !strcmp( argv[i], "-images" ) ) |
||||
{ |
||||
image_folder = argv[++i]; |
||||
} |
||||
else if( !strcmp( argv[i], "-annotations" ) ) |
||||
{ |
||||
annotations = argv[++i]; |
||||
} |
||||
} |
||||
|
||||
// Create the outputfilestream
|
||||
ofstream output(annotations.c_str()); |
||||
|
||||
// Return the image filenames inside the image folder
|
||||
vector<String> filenames; |
||||
String folder(image_folder); |
||||
glob(folder, filenames); |
||||
|
||||
// Loop through each image stored in the images folder
|
||||
// Create and temporarily store the annotations
|
||||
// At the end write everything to the annotations file
|
||||
for (size_t i = 0; i < filenames.size(); i++){ |
||||
// Read in an image
|
||||
Mat current_image = imread(filenames[i]); |
||||
|
||||
// Perform annotations & generate corresponding output
|
||||
stringstream output_stream; |
||||
get_annotations(current_image, &output_stream); |
||||
|
||||
// Store the annotations, write to the output file
|
||||
if (output_stream.str() != ""){ |
||||
output << filenames[i] << output_stream.str(); |
||||
} |
||||
} |
||||
|
||||
return 0; |
||||
} |
After Width: | Height: | Size: 6.5 KiB |
After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 767 B |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.4 KiB |
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 3.9 KiB |
After Width: | Height: | Size: 5.9 KiB |
After Width: | Height: | Size: 4.1 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 2.3 KiB |
@ -0,0 +1,86 @@ |
||||
Extract horizontal and vertical lines by using morphological operations {#tutorial_moprh_lines_detection} |
||||
============= |
||||
|
||||
Goal |
||||
---- |
||||
|
||||
In this tutorial you will learn how to: |
||||
|
||||
- Apply two very common morphology operators (i.e. Dilation and Erosion), with the creation of custom kernels, in order to extract straight lines on the horizontal and vertical axes. For this purpose, you will use the following OpenCV functions: |
||||
- @ref cv::erode |
||||
- @ref cv::dilate |
||||
- @ref cv::getStructuringElement |
||||
|
||||
in an example where your goal will be to extract the music notes from a music sheet. |
||||
|
||||
Theory |
||||
------ |
||||
|
||||
### Morphology Operations |
||||
Morphology is a set of image processing operations that process images based on predefined *structuring elements* known also as kernels. The value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors. By choosing the size and shape of the kernel, you can construct a morphological operation that is sensitive to specific shapes regarding the input image. |
||||
|
||||
Two of the most basic morphological operations are dilation and erosion. Dilation adds pixels to the boundaries of the object in an image, while erosion does exactly the opposite. The amount of pixels added or removed, respectively depends on the size and shape of the structuring element used to process the image. In general the rules followed from these two operations have as follows: |
||||
|
||||
- __Dilation__: The value of the output pixel is the <b><em>maximum</em></b> value of all the pixels that fall within the structuring element's size and shape. For example in a binary image, if any of the pixels of the input image falling within the range of the kernel is set to the value 1, the corresponding pixel of the output image will be set to 1 as well. The latter applies to any type of image (e.g. grayscale, rgb, etc). |
||||
|
||||
![Dilation on a Binary Image](images/morph21.gif) |
||||
|
||||
![Dilation on a Grayscale Image](images/morph6.gif) |
||||
|
||||
- __Erosion__: The vise versa applies for the erosion operation. The value of the output pixel is the <b><em>minimum</em></b> value of all the pixels that fall within the structuring element's size and shape. Look the at the example figures below: |
||||
|
||||
![Erosion on a Binary Image](images/morph211.png) |
||||
|
||||
![Erosion on a Grayscale Image](images/morph61.png) |
||||
|
||||
### Structuring Elements |
||||
|
||||
As it can be seen above and in general in any morphological operation the structuring element used to probe the input image, is the most important part. |
||||
|
||||
A structuring element is a matrix consisting of only 0's and 1's that can have any arbitrary shape and size. Typically are much smaller than the image being processed, while the pixels with values of 1 define the neighborhood. The center pixel of the structuring element, called the origin, identifies the pixel of interest -- the pixel being processed. |
||||
|
||||
For example, the following illustrates a diamond-shaped structuring element of 7x7 size. |
||||
|
||||
![A Diamond-Shaped Structuring Element and its Origin](images/morph12.gif) |
||||
|
||||
A structuring element can have many common shapes, such as lines, diamonds, disks, periodic lines, and circles and sizes. You typically choose a structuring element the same size and shape as the objects you want to process/extract in the input image. For example, to find lines in an image, create a linear structuring element as you will see later. |
||||
|
||||
Code |
||||
---- |
||||
|
||||
This tutorial code's is shown lines below. You can also download it from [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp). |
||||
@includelineno samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp |
||||
|
||||
Explanation / Result |
||||
-------------------- |
||||
|
||||
-# Load the source image and check if it is loaded without any problem, then show it: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp load_image |
||||
![](images/src.png) |
||||
|
||||
-# Then transform image to grayscale if it not already: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp gray |
||||
![](images/gray.png) |
||||
|
||||
-# Afterwards transform grayscale image to binary. Notice the ~ symbol which indicates that we use the inverse (i.e. bitwise_not) version of it: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp bin |
||||
![](images/binary.png) |
||||
|
||||
-# Now we are ready to apply morphological operations in order to extract the horizontal and vertical lines and as a consequence to separate the the music notes from the music sheet, but first let's initialize the output images that we will use for that reason: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp init |
||||
|
||||
-# As we specified in the theory in order to extract the object that we desire, we need to create the corresponding structure element. Since here we want to extract the horizontal lines, a corresponding structure element for that purpose will have the following shape: |
||||
![](images/linear_horiz.png) |
||||
and in the source code this is represented by the following code snippet: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp horiz |
||||
![](images/horiz.png) |
||||
|
||||
-# The same applies for the vertical lines, with the corresponding structure element: |
||||
![](images/linear_vert.png) |
||||
and again this is represented as follows: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp vert |
||||
![](images/vert.png) |
||||
|
||||
-# As you can see we are almost there. However, at that point you will notice that the edges of the notes are a bit rough. For that reason we need to refine the edges in order to obtain a smoother result: |
||||
@snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp smooth |
||||
![](images/smooth.png) |
After Width: | Height: | Size: 100 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 14 KiB |
@ -0,0 +1,158 @@ |
||||
Using OpenCV with biicode dependency manager {#tutorial_biicode} |
||||
============================================ |
||||
|
||||
Goals |
||||
----- |
||||
In this tutorial you will learn how to: |
||||
|
||||
* Get started with OpenCV using biicode. |
||||
* Develop your own application in OpenCV with biicode. |
||||
* Switching between OpenCV versions. |
||||
|
||||
What is biicode? |
||||
---------------- |
||||
|
||||
![](images/biicode.png) |
||||
[biicode](http://opencv.org/biicode.html) resolves and keeps track of dependencies and version compatibilities in C/C++ projects. |
||||
Using biicode *hooks feature*, **getting started with OpenCV in C++ and C** is pretty straight-forward. **Just write an include to OpenCV headers** and biicode will retrieve and install OpenCV in your computer and configure your project. |
||||
|
||||
Prerequisites |
||||
------------- |
||||
|
||||
* biicode. Here is a [link to install it at any OS](http://www.biicode.com/downloads). |
||||
* Windows users: Any Visual Studio version (Visual Studio 12 preferred). |
||||
|
||||
Explanation |
||||
----------- |
||||
|
||||
### Example: Detect faces in images using the Objdetect module from OpenCV |
||||
|
||||
Once biicode is installed, execute in your terminal/console: |
||||
|
||||
@code{.bash} |
||||
$ bii init mycvproject |
||||
$ cd mycvproject |
||||
$ bii open diego/opencvex |
||||
@endcode |
||||
|
||||
Windows users also execute: |
||||
|
||||
@code{.bash} |
||||
$ bii cpp:configure -G "Visual Studio 12" |
||||
@endcode |
||||
|
||||
Now execute ``bii cpp:build`` to build the project. **Note** that this can take a while, until it downloads and builds OpenCV. However, this is downloaded just once in your machine in your "user/.biicode" folder. If the OpenCV installation process fails, you might simply go there, delete OpenCV files inside "user/.biicode" and repeat. |
||||
|
||||
@code{.bash} |
||||
$ bii cpp:build |
||||
@endcode |
||||
|
||||
Find your binaries in the bin folder: |
||||
|
||||
@code{.bash} |
||||
$ cd bin |
||||
$ ./diego_opencvex_main |
||||
@endcode |
||||
|
||||
![](images/biiapp.png) |
||||
|
||||
@code{.bash} |
||||
$ ./diego_opencvex_mainfaces |
||||
@endcode |
||||
|
||||
![](images/bii_lena.png) |
||||
|
||||
###Developing your own application |
||||
|
||||
**biicode works with include headers in your source-code files**, it reads them and retrieves all the dependencies in its database. So it is as simple as typing: |
||||
|
||||
@code{.cpp} |
||||
#include "diego/opencv/opencv/cv.h" |
||||
@endcode |
||||
|
||||
in the headers of your ``.cpp`` file. |
||||
|
||||
To start a new project using OpenCV, execute: |
||||
|
||||
@code{.bash} |
||||
$ bii init mycvproject |
||||
$ cd mycvproject |
||||
@endcode |
||||
|
||||
The next line just creates a *myuser/myblock* folder inside "blocks" with a simple "Hello World" *main.cpp* into it. You can also do it manually: |
||||
|
||||
@code{.bash} |
||||
$ bii new myuser/myblock --hello=cpp |
||||
@endcode |
||||
|
||||
Now replace your *main.cpp* contents inside *blocks/myuser/myblock* with **your app code**. |
||||
Put the includes as: |
||||
|
||||
@code{.cpp} |
||||
#include "diego/opencv/opencv/cv.h |
||||
@endcode |
||||
|
||||
If you type: |
||||
|
||||
@code{.bash} |
||||
$ bii deps |
||||
@endcode |
||||
|
||||
You will check that ``opencv/cv.h`` is an "unresolved" dependency. You can find it with: |
||||
|
||||
@code{.bash} |
||||
$ bii find |
||||
@endcode |
||||
|
||||
Now, you can just `bii cpp:configure` and `bii cpp:build` your project as described above. |
||||
|
||||
**To use regular include directives**, configure them in your **biicode.conf** file. Let your includes be: |
||||
|
||||
@code{.cpp} |
||||
#include "opencv/cv.h" |
||||
@endcode |
||||
|
||||
And write in your **biicode.conf**: |
||||
|
||||
@code{.cpp} |
||||
[includes] |
||||
opencv/cv.h: diego/opencv |
||||
[requirements] |
||||
diego/opencv: 0 |
||||
@endcode |
||||
|
||||
###Switching OpenCV versions |
||||
|
||||
If you want to try or develop your application against **OpenCV 2.4.10** and also against **3.0-beta**, change it in your **biicode.conf** file, simply alternating track in your `[requirements]`: |
||||
|
||||
@code{.cpp} |
||||
[requirements] |
||||
diego/opencv: 0 |
||||
@endcode |
||||
|
||||
replace with: |
||||
|
||||
@code{.cpp} |
||||
[requirements] |
||||
diego/opencv(beta): 0 |
||||
@endcode |
||||
|
||||
**Note** that the first time you switch to 3.0-beta, it will also take a while to download and build the 3.0-beta release. From that point you can change back and forth between versions, just modifying your *biicode.conf requirements*. |
||||
|
||||
Find the hooks and examples: |
||||
* [OpenCV 2.4.10](http://www.biicode.com/diego/opencv) |
||||
* [OpenCV 3.0 beta](http://www.biicode.com/diego/diego/opencv/beta) |
||||
* [objdetect module from OpenCV](@ref tutorial_table_of_content_objdetect) |
||||
|
||||
This is just an example of how can it be done with biicode python hooks. Probably now that CMake files reuse is possible with biicode, it could be better to implement it with CMake, in order to get more control over the build of OpenCV. |
||||
|
||||
Results and conclusion |
||||
---------------------- |
||||
|
||||
Installing OpenCV with biicode is straight forward for any OS. |
||||
|
||||
Run any example like you just did with *objdetect module* from OpenCV, or develop your own application. It only needs a *biicode.conf* file to get OpenCV library working in your computer. |
||||
|
||||
Switching between OpenCV versions is available too and effortless. |
||||
|
||||
For any doubts or further information regarding biicode, suit yourselves at [Stackoverflow](http://stackoverflow.com/questions/tagged/biicode?sort=newest), biicode’s [forum](http://forum.biicode.com/) or [ask biicode](http://web.biicode.com/contact-us/), we will be glad to help you. |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 203 KiB |
After Width: | Height: | Size: 8.2 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 32 KiB |
@ -0,0 +1,133 @@ |
||||
Introduction to Principal Component Analysis (PCA) {#tutorial_introduction_to_pca} |
||||
======================================= |
||||
|
||||
Goal |
||||
---- |
||||
|
||||
In this tutorial you will learn how to: |
||||
|
||||
- Use the OpenCV class @ref cv::PCA to calculate the orientation of an object. |
||||
|
||||
What is PCA? |
||||
-------------- |
||||
|
||||
Principal Component Analysis (PCA) is a statistical procedure that extracts the most important features of a dataset. |
||||
|
||||
![](images/pca_line.png) |
||||
|
||||
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. |
||||
|
||||
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. |
||||
|
||||
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. |
||||
|
||||
![](images/pca_eigen.png) |
||||
|
||||
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. |
||||
|
||||
How are the eigenvectors and eigenvalues computed? |
||||
-------------------------------------------------- |
||||
|
||||
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__: |
||||
|
||||
\f[ \mathbf{Y} = \mathbb{K} \mathbb{L} \mathbb{T} \{\mathbf{X}\} \f] |
||||
|
||||
__Organize the data set__ |
||||
|
||||
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. |
||||
|
||||
- Write \f$ x_1...x_n \f$ as row vectors, each of which has _p_ columns. |
||||
- Place the row vectors into a single matrix __X__ of dimensions \f$ n\times p \f$. |
||||
|
||||
__Calculate the empirical mean__ |
||||
|
||||
- Find the empirical mean along each dimension \f$ j = 1, ..., p \f$. |
||||
|
||||
- Place the calculated mean values into an empirical mean vector __u__ of dimensions \f$ p\times 1 \f$. |
||||
|
||||
\f[ \mathbf{u[j]} = \frac{1}{n}\sum_{i=1}^{n}\mathbf{X[i,j]} \f] |
||||
|
||||
__Calculate the deviations from the mean__ |
||||
|
||||
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: |
||||
|
||||
- Subtract the empirical mean vector __u__ from each row of the data matrix __X__. |
||||
|
||||
- Store mean-subtracted data in the \f$ n\times p \f$ matrix __B__. |
||||
|
||||
\f[ \mathbf{B} = \mathbf{X} - \mathbf{h}\mathbf{u^{T}} \f] |
||||
|
||||
where __h__ is an \f$ n\times 1 \f$ column vector of all 1s: |
||||
|
||||
\f[ h[i] = 1, i = 1, ..., n \f] |
||||
|
||||
__Find the covariance matrix__ |
||||
|
||||
- Find the \f$ p\times p \f$ empirical covariance matrix __C__ from the outer product of matrix __B__ with itself: |
||||
|
||||
\f[ \mathbf{C} = \frac{1}{n-1} \mathbf{B^{*}} \cdot \mathbf{B} \f] |
||||
|
||||
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. |
||||
|
||||
__Find the eigenvectors and eigenvalues of the covariance matrix__ |
||||
|
||||
- Compute the matrix __V__ of eigenvectors which diagonalizes the covariance matrix __C__: |
||||
|
||||
\f[ \mathbf{V^{-1}} \mathbf{C} \mathbf{V} = \mathbf{D} \f] |
||||
|
||||
where __D__ is the diagonal matrix of eigenvalues of __C__. |
||||
|
||||
- Matrix __D__ will take the form of an \f$ p \times p \f$ diagonal matrix: |
||||
|
||||
\f[ D[k,l] = \left\{\begin{matrix} \lambda_k, k = l \\ 0, k \neq l \end{matrix}\right. \f] |
||||
|
||||
here, \f$ \lambda_j \f$ is the _j_-th eigenvalue of the covariance matrix __C__ |
||||
|
||||
- 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__. |
||||
- The eigenvalues and eigenvectors are ordered and paired. The _j_ th eigenvalue corresponds to the _j_ th eigenvector. |
||||
|
||||
@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. |
||||
|
||||
Source Code |
||||
----------- |
||||
|
||||
This tutorial code's is shown lines below. You can also download it from |
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp). |
||||
@includelineno cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp |
||||
|
||||
@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) |
||||
|
||||
Explanation |
||||
----------- |
||||
|
||||
-# __Read image and convert it to binary__ |
||||
|
||||
Here we apply the necessary pre-processing procedures in order to be able to detect the objects of interest. |
||||
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pre-process |
||||
|
||||
-# __Extract objects of interest__ |
||||
|
||||
Then find and filter contours by size and obtain the orientation of the remaining ones. |
||||
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp contours |
||||
|
||||
-# __Extract orientation__ |
||||
|
||||
Orientation is extracted by the call of getOrientation() function, which performs all the PCA procedure. |
||||
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pca |
||||
|
||||
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. |
||||
|
||||
-# __Visualize result__ |
||||
|
||||
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. |
||||
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization |
||||
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization1 |
||||
|
||||
Results |
||||
------- |
||||
|
||||
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. |
||||
|
||||
![](images/pca_test1.jpg) |
||||
|
||||
![](images/output.png) |
@ -1,2 +1,2 @@ |
||||
set(the_description "Camera Calibration and 3D Reconstruction") |
||||
ocv_define_module(calib3d opencv_imgproc opencv_features2d) |
||||
ocv_define_module(calib3d opencv_imgproc opencv_features2d WRAP java python) |
||||
|