Added to the tutorials the "Support Vector Machines for Non-Linearly Separable Data...
authorBernat Gabor <no@email>
Sun, 14 Aug 2011 19:58:20 +0000 (19:58 +0000)
committerBernat Gabor <no@email>
Sun, 14 Aug 2011 19:58:20 +0000 (19:58 +0000)
doc/conf.py
doc/tutorials/gpu/gpu-basics-similarity/gpu-basics-similarity.rst
doc/tutorials/ml/non_linear_svms/images/result.png [new file with mode: 0644]
doc/tutorials/ml/non_linear_svms/images/sample-errors-dist.png [new file with mode: 0644]
doc/tutorials/ml/non_linear_svms/non_linear_svms.rst [new file with mode: 0644]
doc/tutorials/ml/table_of_content_ml/images/non_linear_svms.png [new file with mode: 0644]
doc/tutorials/ml/table_of_content_ml/table_of_content_ml.rst
samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp
samples/cpp/tutorial_code/ml/non_linear_svms/non_linear_svms.cpp [new file with mode: 0644]

index 29408be..e7eac42 100644 (file)
@@ -355,6 +355,7 @@ extlinks = {'cvt_color': ('http://opencv.willowgarage.com/documentation/cpp/imgp
             'utilitysystemfunctions':('http://opencv.itseez.com/modules/core/doc/utility_and_system_functions_and_macros.html#%s', None),
             'imgprocfilter':('http://opencv.itseez.com/modules/imgproc/doc/filtering.html#%s', None),
             'svms':('http://opencv.itseez.com/modules/ml/doc/support_vector_machines.html#%s', None),
+                       'drawingfunc':('http://opencv.itseez.com/modules/core/doc/drawing_functions.html#%s', None),
             'xmlymlpers':('http://opencv.itseez.com/modules/core/doc/xml_yaml_persistence.html#%s', None),
                        'huivideo' : ('http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html#%s', None),
                        'gpuinit' : ('http://opencv.itseez.com/modules/gpu/doc/initalization_and_information.html#%s', None),
index d81b9fa..04ce2b2 100644 (file)
@@ -1 +1 @@
-.. _gpuBasicsSimilarity:\r\rSimilarity check (PNSR and SSIM) on the GPU\r*******************************************\r\rGoal\r====\r\rIn the :ref:`videoInputPSNRMSSIM` tutorial I already presented the PSNR and SSIM methods for checking the similarity between the two images. And as you could see there performing these takes quite some time, especially in the case of the SSIM. However, if the performance numbers of an OpenCV implementation for the CPU do not satisfy you and you happen to have an NVidia CUDA GPU device in your system all is not lost. You may try to port or write your algorithm for the video card. \r\rThis tutorial will give a good grasp on how to approach coding by using the GPU module of OpenCV. As a prerequisite you should already know how to handle the core, highgui and imgproc modules. So, our goals are: \r\r.. container:: enumeratevisibleitemswithsquare\r\r   + What's different compared to the CPU?\r   + Create the GPU code for the PSNR and SSIM \r   + Optimize the code for maximal performance\r\rThe source code\r===============\r\rYou may also find the source code and these video file in the :file:`samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity` folder of the OpenCV source library or :download:`download it from here <../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp>`. The full source code is quite long (due to the controlling of the application via the command line arguments and performance measurement). Therefore, to avoid cluttering up these sections with those you'll find here only the functions itself. \r\rThe PSNR returns a float number, that if the two inputs are similar between 30 and 50 (higher is better). \r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 165-210, 17-23, 211-233\r\rThe SSIM returns the MSSIM of the images. This is too a float number between zero and one (higher is better), however we have one for each channel. Therefore, we return a *Scalar* OpenCV data structure:\r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 235-283, 25-42, 284-\r\rHow to do it? - The GPU\r=======================\r\rNow as you can see we have three types of functions for each operation. One for the CPU and two for the GPU. The reason I made two for the GPU is too illustrate that often simple porting your CPU to GPU will actually make it slower. If you want some performance gain you will need to remember a few rules, whose I'm going to detail later on.\r\rThe development of the GPU module was made so that it resembles as much as possible its CPU counterpart. This is to make porting easy. The first thing you need to do before writing any code is to link the GPU module to your project, and include the header file for the module. All the functions and data structures of the GPU are in a *gpu* sub namespace of the *cv* namespace. You may add this to the default one via the *use namespace* keyword, or mark it everywhere explicitly via the cv:: to avoid confusion. I'll do the later. \r\r.. code-block:: cpp\r\r   #include <opencv2/gpu/gpu.hpp>        // GPU structures and methods\r\rGPU stands for **g**\ raphics **p**\ rocessing **u**\ nit. It was originally build to render graphical scenes. These scenes somehow build on a lot of data. Nevertheless, these aren't all dependent one from another in a sequential way and as it is possible a parallel processing of them. Due to this a GPU will contain multiple smaller processing units. These aren't the state of the art processors and on a one on one test with a CPU it will fall behind. However, its strength lies in its numbers. In the last years there has been an increasing trend to harvest these massive parallel powers of the GPU in non-graphical scene rendering too. This gave birth to the general-purpose computation on graphics processing units (GPGPU). \r\rThe GPU has its own memory. When you read data from the hard drive with OpenCV into a *Mat* object that takes place in your systems memory. The CPU works somehow directly on this (via its cache), however the GPU cannot. He has too transferred the information he will use for calculations from the system memory to its own. This is done via an upload process and takes time. In the end the result will have to be downloaded back to your system memory for your CPU to see it and use it. Porting small functions to GPU is not recommended as the upload/download time will be larger than the amount you gain by a parallel execution. \r\r*Mat* objects are stored **only** in the system memory (or the CPU cache). For getting an OpenCV matrix to the GPU you'll need to use its GPU counterpart :gpudatastructure:`GpuMat <gpu-gpumat>`. It works similar to the *Mat* with a 2D only limitation and no reference returning for its functions (cannot mix GPU references with CPU ones). To upload a *Mat* object to the *GPU* you need to call the *upload* function after creating an instance of the class. To download you may use simple assignment to a *Mat* object or use the *download* function.\r\r.. code-block:: cpp \r\r   Mat I1;         // Main memory item - read image into with imread for example\r   gpu::GpuMat gI; // GPU matrix - for now empty\r   gI1.upload(I1); // Upload a data from the system memory to the GPU memory\r\r   I1 = gI1;       // Download, gI1.download(I1) will work too\r\rOnce you have your data up in the GPU memory you may call GPU enabled functions of OpenCV. Most of the functions keep the same name just as on the CPU, with the difference that they only accept *GpuMat* inputs. A full list of these you will find in the documentation: `online here <http://opencv.itseez.com/modules/gpu/doc/gpu.html>`_ or the OpenCV reference manual that comes with the source code.\r\rAnother thing to keep in mind is that not for all channel numbers you can make efficient algorithms on the GPU. Generally, I found that the input images for the GPU images need to be either one or four channel ones and one of the char or float type for the item sizes. No double support on the GPU, sorry. Passing other types of objects for some functions will result in an exception thrown, and an error message on the error output. The documentation details in most of the places the types accepted for the inputs. If you have three channel images as an input you can do two things: either adds a new channel (and use char elements) or split up the image and call the function for each image. The first one isn't really recommended as you waste memory. \r\rFor some functions, where the position of the elements (neighbor items) doesn't matter quick solution is to just reshape it into a single channel image. This is the case for the PSNR implementation where for the *absdiff* method the value of the neighbors is not important. However, for the *GaussianBlur* this isn't an option and such need to use the split method for the SSIM. With this knowledge you can already make a GPU viable code (like mine GPU one) and run it. You'll be surprised to see that it might turn out slower than your CPU implementation. \r\rOptimization\r============\r\rThe reason for this is that you're throwing out on the window the price for memory allocation and data transfer. And on the GPU this is damn high. Another possibility for optimization is to introduce asynchronous OpenCV GPU calls too with the help of the :gpudatastructure:`gpu::Stream <gpu-stream>`. \r\r1. Memory allocation on the GPU is considerable. Therefore, if it’s possible allocate new memory as few times as possible. If you create a function what you intend to call multiple times it is a good idea to allocate any local parameters for the function only once, during the first call. To do this you create a data structure containing all the local variables you will use. For instance in case of the PSNR these are: \r\r   .. code-block:: cpp \r\r      struct BufferPSNR                                     // Optimized GPU versions\r        {   // Data allocations are very expensive on GPU. Use a buffer to solve: allocate once reuse later.\r        gpu::GpuMat gI1, gI2, gs, t1,t2;\r\r        gpu::GpuMat buf;\r      };\r\r   Then create an instance of this in the main program: \r\r   .. code-block:: cpp\r\r      BufferPSNR bufferPSNR;\r\r   And finally pass this to the function each time you call it: \r\r   .. code-block:: cpp\r\r      double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)\r\r   Now you access these local parameters as: *b.gI1*, *b.buf* and so on. The GpuMat will only reallocate itself on a new call if the new matrix size is different from the previous one. \r\r#. Avoid unnecessary function data transfers. Any small data transfer will be significant one once you go to the GPU. Therefore, if possible make all calculations in-place (in other words do not create new memory objects - for reasons explained at the previous point). For example, although expressing arithmetical operations may be easier to express in one line formulas, it will be slower. In case of the SSIM at one point I need to calculate:\r\r   .. code-block:: cpp \r\r      b.t1 = 2 * b.mu1_mu2 + C1;  \r\r   Although the upper call will succeed observe that there is a hidden data transfer present. Before it makes the addition it needs to store somewhere the multiplication. Therefore, it will create a local matrix in the background, add to that the *C1* value and finally assign that to *t1*. To avoid this we use the gpu functions, instead of the arithmetic operators: \r\r   .. code-block:: cpp\r\r      gpu::multiply(b.mu1_mu2, 2, b.t1); //b.t1 = 2 * b.mu1_mu2 + C1; \r      gpu::add(b.t1, C1, b.t1);\r\r#. Use asynchronous calls (the :gpudatastructure:`gpu::Stream <gpu-stream>`). By default whenever you call a gpu function it will wait for the call to finish and return with the result afterwards. However, it is possible to make asynchronous calls, meaning it will call for the operation execution, make the costly data allocations for the algorithm and return back right away. Now you can call another function if you wish to do so. For the MSSIM this is a small optimization point. In our default implementation we split up the image into channels and call then for each channel the gpu functions. A small degree of parallelization is possible with the stream. By using a stream we can make the data allocation, upload operations while the GPU is already executing a given method. For example we need to upload two images. We queue these one after another and call already the function that processes it. The functions will wait for the upload to finish, however while that happens makes the output buffer allocations for the function to be executed next. \r\r   .. code-block:: cpp \r\r      gpu::Stream stream;\r\r      stream.enqueueConvert(b.gI1, b.t1, CV_32F);    // Upload\r\r      gpu::split(b.t1, b.vI1, stream);              // Methods (pass the stream as final parameter).\r      gpu::multiply(b.vI1[i], b.vI1[i], b.I1_2, stream);        // I1^2\r\rResult and conclusion\r=====================\r\rOn an Intel P8700 laptop CPU paired with a low end NVidia GT220M here are the performance numbers: \r\r.. code-block:: cpp\r\r   Time of PSNR CPU (averaged for 10 runs): 41.4122 milliseconds. With result of: 19.2506\r   Time of PSNR GPU (averaged for 10 runs): 158.977 milliseconds. With result of: 19.2506\r   Initial call GPU optimized:              31.3418 milliseconds. With result of: 19.2506\r   Time of PSNR GPU OPTIMIZED ( / 10 runs): 24.8171 milliseconds. With result of: 19.2506\r\r   Time of MSSIM CPU (averaged for 10 runs): 484.343 milliseconds. With result of B0.890964 G0.903845 R0.936934\r   Time of MSSIM GPU (averaged for 10 runs): 745.105 milliseconds. With result of B0.89922 G0.909051 R0.968223\r   Time of MSSIM GPU Initial Call            357.746 milliseconds. With result of B0.890964 G0.903845 R0.936934\r   Time of MSSIM GPU OPTIMIZED ( / 10 runs): 203.091 milliseconds. With result of B0.890964 G0.903845 R0.936934\r\rIn both cases we managed a performance increase of almost 100% compared to the CPU implementation. It may be just the improvement needed for your application to work. You may observe a runtime instance of this on the `YouTube here <https://www.youtube.com/watch?v=3_ESXmFlnvY>`_. \r\r.. raw:: html\r\r  <div align="center">\r  <iframe title="Similarity check (PNSR and SSIM) on the GPU" width="560" height="349" src="http://www.youtube.com/embed/3_ESXmFlnvY?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>\r  </div>\r
\ No newline at end of file
+.. _gpuBasicsSimilarity:\r\rSimilarity check (PNSR and SSIM) on the GPU\r*******************************************\r\rGoal\r====\r\rIn the :ref:`videoInputPSNRMSSIM` tutorial I already presented the PSNR and SSIM methods for checking the similarity between the two images. And as you could see there performing these takes quite some time, especially in the case of the SSIM. However, if the performance numbers of an OpenCV implementation for the CPU do not satisfy you and you happen to have an NVidia CUDA GPU device in your system all is not lost. You may try to port or write your algorithm for the video card. \r\rThis tutorial will give a good grasp on how to approach coding by using the GPU module of OpenCV. As a prerequisite you should already know how to handle the core, highgui and imgproc modules. So, our goals are: \r\r.. container:: enumeratevisibleitemswithsquare\r\r   + What's different compared to the CPU?\r   + Create the GPU code for the PSNR and SSIM \r   + Optimize the code for maximal performance\r\rThe source code\r===============\r\rYou may also find the source code and these video file in the :file:`samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity` folder of the OpenCV source library or :download:`download it from here <../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp>`. The full source code is quite long (due to the controlling of the application via the command line arguments and performance measurement). Therefore, to avoid cluttering up these sections with those you'll find here only the functions itself. \r\rThe PSNR returns a float number, that if the two inputs are similar between 30 and 50 (higher is better). \r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 165-210, 18-23, 210-235\r\rThe SSIM returns the MSSIM of the images. This is too a float number between zero and one (higher is better), however we have one for each channel. Therefore, we return a *Scalar* OpenCV data structure:\r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 235-355, 26-42, 357-\r\rHow to do it? - The GPU\r=======================\r\rNow as you can see we have three types of functions for each operation. One for the CPU and two for the GPU. The reason I made two for the GPU is too illustrate that often simple porting your CPU to GPU will actually make it slower. If you want some performance gain you will need to remember a few rules, whose I'm going to detail later on.\r\rThe development of the GPU module was made so that it resembles as much as possible its CPU counterpart. This is to make porting easy. The first thing you need to do before writing any code is to link the GPU module to your project, and include the header file for the module. All the functions and data structures of the GPU are in a *gpu* sub namespace of the *cv* namespace. You may add this to the default one via the *use namespace* keyword, or mark it everywhere explicitly via the cv:: to avoid confusion. I'll do the later. \r\r.. code-block:: cpp\r\r   #include <opencv2/gpu/gpu.hpp>        // GPU structures and methods\r\rGPU stands for **g**\ raphics **p**\ rocessing **u**\ nit. It was originally build to render graphical scenes. These scenes somehow build on a lot of data. Nevertheless, these aren't all dependent one from another in a sequential way and as it is possible a parallel processing of them. Due to this a GPU will contain multiple smaller processing units. These aren't the state of the art processors and on a one on one test with a CPU it will fall behind. However, its strength lies in its numbers. In the last years there has been an increasing trend to harvest these massive parallel powers of the GPU in non-graphical scene rendering too. This gave birth to the general-purpose computation on graphics processing units (GPGPU). \r\rThe GPU has its own memory. When you read data from the hard drive with OpenCV into a *Mat* object that takes place in your systems memory. The CPU works somehow directly on this (via its cache), however the GPU cannot. He has too transferred the information he will use for calculations from the system memory to its own. This is done via an upload process and takes time. In the end the result will have to be downloaded back to your system memory for your CPU to see it and use it. Porting small functions to GPU is not recommended as the upload/download time will be larger than the amount you gain by a parallel execution. \r\r*Mat* objects are stored **only** in the system memory (or the CPU cache). For getting an OpenCV matrix to the GPU you'll need to use its GPU counterpart :gpudatastructure:`GpuMat <gpu-gpumat>`. It works similar to the *Mat* with a 2D only limitation and no reference returning for its functions (cannot mix GPU references with CPU ones). To upload a *Mat* object to the *GPU* you need to call the *upload* function after creating an instance of the class. To download you may use simple assignment to a *Mat* object or use the *download* function.\r\r.. code-block:: cpp \r\r   Mat I1;         // Main memory item - read image into with imread for example\r   gpu::GpuMat gI; // GPU matrix - for now empty\r   gI1.upload(I1); // Upload a data from the system memory to the GPU memory\r\r   I1 = gI1;       // Download, gI1.download(I1) will work too\r\rOnce you have your data up in the GPU memory you may call GPU enabled functions of OpenCV. Most of the functions keep the same name just as on the CPU, with the difference that they only accept *GpuMat* inputs. A full list of these you will find in the documentation: `online here <http://opencv.itseez.com/modules/gpu/doc/gpu.html>`_ or the OpenCV reference manual that comes with the source code.\r\rAnother thing to keep in mind is that not for all channel numbers you can make efficient algorithms on the GPU. Generally, I found that the input images for the GPU images need to be either one or four channel ones and one of the char or float type for the item sizes. No double support on the GPU, sorry. Passing other types of objects for some functions will result in an exception thrown, and an error message on the error output. The documentation details in most of the places the types accepted for the inputs. If you have three channel images as an input you can do two things: either adds a new channel (and use char elements) or split up the image and call the function for each image. The first one isn't really recommended as you waste memory. \r\rFor some functions, where the position of the elements (neighbor items) doesn't matter quick solution is to just reshape it into a single channel image. This is the case for the PSNR implementation where for the *absdiff* method the value of the neighbors is not important. However, for the *GaussianBlur* this isn't an option and such need to use the split method for the SSIM. With this knowledge you can already make a GPU viable code (like mine GPU one) and run it. You'll be surprised to see that it might turn out slower than your CPU implementation. \r\rOptimization\r============\r\rThe reason for this is that you're throwing out on the window the price for memory allocation and data transfer. And on the GPU this is damn high. Another possibility for optimization is to introduce asynchronous OpenCV GPU calls too with the help of the :gpudatastructure:`gpu::Stream <gpu-stream>`. \r\r1. Memory allocation on the GPU is considerable. Therefore, if it’s possible allocate new memory as few times as possible. If you create a function what you intend to call multiple times it is a good idea to allocate any local parameters for the function only once, during the first call. To do this you create a data structure containing all the local variables you will use. For instance in case of the PSNR these are: \r\r   .. code-block:: cpp \r\r      struct BufferPSNR                                     // Optimized GPU versions\r        {   // Data allocations are very expensive on GPU. Use a buffer to solve: allocate once reuse later.\r        gpu::GpuMat gI1, gI2, gs, t1,t2;\r\r        gpu::GpuMat buf;\r      };\r\r   Then create an instance of this in the main program: \r\r   .. code-block:: cpp\r\r      BufferPSNR bufferPSNR;\r\r   And finally pass this to the function each time you call it: \r\r   .. code-block:: cpp\r\r      double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)\r\r   Now you access these local parameters as: *b.gI1*, *b.buf* and so on. The GpuMat will only reallocate itself on a new call if the new matrix size is different from the previous one. \r\r#. Avoid unnecessary function data transfers. Any small data transfer will be significant one once you go to the GPU. Therefore, if possible make all calculations in-place (in other words do not create new memory objects - for reasons explained at the previous point). For example, although expressing arithmetical operations may be easier to express in one line formulas, it will be slower. In case of the SSIM at one point I need to calculate:\r\r   .. code-block:: cpp \r\r      b.t1 = 2 * b.mu1_mu2 + C1;  \r\r   Although the upper call will succeed observe that there is a hidden data transfer present. Before it makes the addition it needs to store somewhere the multiplication. Therefore, it will create a local matrix in the background, add to that the *C1* value and finally assign that to *t1*. To avoid this we use the gpu functions, instead of the arithmetic operators: \r\r   .. code-block:: cpp\r\r      gpu::multiply(b.mu1_mu2, 2, b.t1); //b.t1 = 2 * b.mu1_mu2 + C1; \r      gpu::add(b.t1, C1, b.t1);\r\r#. Use asynchronous calls (the :gpudatastructure:`gpu::Stream <gpu-stream>`). By default whenever you call a gpu function it will wait for the call to finish and return with the result afterwards. However, it is possible to make asynchronous calls, meaning it will call for the operation execution, make the costly data allocations for the algorithm and return back right away. Now you can call another function if you wish to do so. For the MSSIM this is a small optimization point. In our default implementation we split up the image into channels and call then for each channel the gpu functions. A small degree of parallelization is possible with the stream. By using a stream we can make the data allocation, upload operations while the GPU is already executing a given method. For example we need to upload two images. We queue these one after another and call already the function that processes it. The functions will wait for the upload to finish, however while that happens makes the output buffer allocations for the function to be executed next. \r\r   .. code-block:: cpp \r\r      gpu::Stream stream;\r\r      stream.enqueueConvert(b.gI1, b.t1, CV_32F);    // Upload\r\r      gpu::split(b.t1, b.vI1, stream);              // Methods (pass the stream as final parameter).\r      gpu::multiply(b.vI1[i], b.vI1[i], b.I1_2, stream);        // I1^2\r\rResult and conclusion\r=====================\r\rOn an Intel P8700 laptop CPU paired with a low end NVidia GT220M here are the performance numbers: \r\r.. code-block:: cpp\r\r   Time of PSNR CPU (averaged for 10 runs): 41.4122 milliseconds. With result of: 19.2506\r   Time of PSNR GPU (averaged for 10 runs): 158.977 milliseconds. With result of: 19.2506\r   Initial call GPU optimized:              31.3418 milliseconds. With result of: 19.2506\r   Time of PSNR GPU OPTIMIZED ( / 10 runs): 24.8171 milliseconds. With result of: 19.2506\r\r   Time of MSSIM CPU (averaged for 10 runs): 484.343 milliseconds. With result of B0.890964 G0.903845 R0.936934\r   Time of MSSIM GPU (averaged for 10 runs): 745.105 milliseconds. With result of B0.89922 G0.909051 R0.968223\r   Time of MSSIM GPU Initial Call            357.746 milliseconds. With result of B0.890964 G0.903845 R0.936934\r   Time of MSSIM GPU OPTIMIZED ( / 10 runs): 203.091 milliseconds. With result of B0.890964 G0.903845 R0.936934\r\rIn both cases we managed a performance increase of almost 100% compared to the CPU implementation. It may be just the improvement needed for your application to work. You may observe a runtime instance of this on the `YouTube here <https://www.youtube.com/watch?v=3_ESXmFlnvY>`_. \r\r.. raw:: html\r\r  <div align="center">\r  <iframe title="Similarity check (PNSR and SSIM) on the GPU" width="560" height="349" src="http://www.youtube.com/embed/3_ESXmFlnvY?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>\r  </div>\r
\ No newline at end of file
diff --git a/doc/tutorials/ml/non_linear_svms/images/result.png b/doc/tutorials/ml/non_linear_svms/images/result.png
new file mode 100644 (file)
index 0000000..bfecae9
Binary files /dev/null and b/doc/tutorials/ml/non_linear_svms/images/result.png differ
diff --git a/doc/tutorials/ml/non_linear_svms/images/sample-errors-dist.png b/doc/tutorials/ml/non_linear_svms/images/sample-errors-dist.png
new file mode 100644 (file)
index 0000000..1379a56
Binary files /dev/null and b/doc/tutorials/ml/non_linear_svms/images/sample-errors-dist.png differ
diff --git a/doc/tutorials/ml/non_linear_svms/non_linear_svms.rst b/doc/tutorials/ml/non_linear_svms/non_linear_svms.rst
new file mode 100644 (file)
index 0000000..8fbcc56
--- /dev/null
@@ -0,0 +1 @@
+.. _nonLinearSvmS:\r \rSupport Vector Machines for Non-Linearly Separable Data\r*******************************************************\r\rGoal\r====\r\rIn this tutorial you will learn how to:\r\r.. container:: enumeratevisibleitemswithsquare\r\r  + Define the optimization problem for SVMs when it is not possible to separate linearly the training data.\r\r  + How to configure the parameters in :svms:`CvSVMParams <cvsvmparams>` to adapt your SVM for this class of problems.\r\rMotivation\r==========\r\rWhy is it interesting to extend the SVM optimation problem in order to handle non-linearly separable training data? Most of the applications in which SVMs are used in computer vision require a more powerful tool than a simple linear classifier. This stems from the fact that in these tasks **the training data can be rarely separated using an hyperplane**.\r\rConsider one of these tasks, for example, face detection. The training data in this case is composed by a set of images that are faces and another set of images that are non-faces (*every other thing in the world except from faces*). This training data is too complex so as to find a representation of each sample (*feature vector*) that could make the whole set of faces linearly separable from the whole set of non-faces.\r\rExtension of the Optimization Problem\r=====================================\r\rRemember that using SVMs we obtain a separating hyperplane. Therefore, since the training data is now non-linearly separable, we must admit that the hyperplane found will misclassify some of the samples. This *misclassification* is a new variable in the optimization that must be taken into account. The new model has to include both the old requirement of finding the hyperplane that gives the biggest margin and the new one of generalizing the training data correctly by not allowing too many classification errors.  \r\rWe start here from the formulation of the optimization problem of finding the hyperplane which maximizes the **margin** (this is explained in the :ref:`previous tutorial <introductiontosvms>`):\r\r.. math::\r  \min_{\beta, \beta_{0}} L(\beta) = \frac{1}{2}||\beta||^{2} \text{ subject to } y_{i}(\beta^{T} x_{i} + \beta_{0}) \geq 1 \text{ } \forall i\r\rThere are multiple ways in which this model can be modified so it takes into account the misclassification errors. For example, one could think of minimizing the same quantity plus a constant times the number of misclassification errors in the training data, i.e.:\r\r.. math::\r  \min ||\beta||^{2} + C \text{(\# misclassication errors)}\r\rHowever, this one is not a very good solution since, among some other reasons, we do not distinguish between samples that are misclassified with a small distance to their appropriate decision region or samples that are not. Therefore, a better solution will take into account the *distance of the misclassified samples to their correct decision regions*, i.e.:\r\r.. math::\r  \min ||\beta||^{2} + C \text{(distance of misclassified samples to their correct regions)}\r\rFor each sample of the training data a new parameter :math:`\xi_{i}` is defined. Each one of these parameters contains the distance from its corresponding training sample to their correct decision region. The following picture shows non-linearly separable training data from two classes, a separating hyperplane and the distances to their correct regions of the samples that are misclassified.\r\r.. image:: images/sample-errors-dist.png\r   :alt: Samples misclassified and their distances to their correct regions\r   :align: center \r\r.. note:: Only the distances of the samples that are misclassified are shown in the picture. The distances of the rest of the samples are zero since they lay already in their correct decision region.\r\rThe red and blue lines that appear on the picture are the margins to each one of the decision regions. It is very **important** to realize that each of the :math:`\xi_{i}` goes from a misclassified training sample to the margin of its appropriate region.\r\rFinally, the new formulation for the optimization problem is:\r\r.. math::\r  \min_{\beta, \beta_{0}} L(\beta) = ||\beta||^{2} + C \sum_{i} {\xi_{i}} \text{ subject to } y_{i}(\beta^{T} x_{i} + \beta_{0}) \geq 1 - \xi_{i} \text{ and } \xi_{i} \geq 0 \text{ } \forall i \r\rHow should the parameter C be chosen? It is obvious that the answer to this question depends on how the training data is distributed. Although there is no general answer, it is useful to take into account these rules:\r\r.. container:: enumeratevisibleitemswithsquare\r\r   * Large values of C give solutions with *less misclassification errors* but a *smaller margin*. Consider that in this case it is expensive to make misclassification errors. Since the aim of the optimization is to minimize the argument, few misclassifications errors are allowed.\r\r   * Small values of C give solutions with *bigger margin* and *more classification errors*. In this case the minimization does not consider that much the term of the sum so it focuses more on finding a hyperplane with big margin.\r\rSource Code\r===========\r\rYou may also find the source code and these video file in the :file:`samples/cpp/tutorial_code/gpu/non_linear_svms/non_linear_svms` folder of the OpenCV source library or :download:`download it from here <../../../../samples/cpp/tutorial_code/ml/non_linear_svms/non_linear_svms.cpp>`.\r\r.. literalinclude:: ../../../../samples/cpp/tutorial_code/ml/non_linear_svms/non_linear_svms.cpp\r   :language: cpp\r   :linenos:\r   :tab-width: 4\r   :lines: 1-11, 22-23, 26-\r\rExplanation\r===========\r\r1. **Set up the training data**\r\r  The training data of this exercise is formed by a set of labeled 2D-points that belong to one of two different classes. To make the exercise more appealing, the training data is generated randomly using a uniform probability density functions (PDFs).\r\r  We have divided the generation of the training data into two main parts.\r\r  In the first part we generate data for both classes that is linearly separable.\r\r  .. code-block:: cpp\r\r     // Generate random points for the class 1\r     Mat trainClass = trainData.rowRange(0, nLinearSamples);\r     // The x coordinate of the points is in [0, 0.4)\r     Mat c = trainClass.colRange(0, 1);\r     rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * WIDTH));\r     // The y coordinate of the points is in [0, 1)\r     c = trainClass.colRange(1,2);\r     rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));\r\r     // Generate random points for the class 2\r     trainClass = trainData.rowRange(2*NTRAINING_SAMPLES-nLinearSamples, 2*NTRAINING_SAMPLES);\r     // The x coordinate of the points is in [0.6, 1]\r     c = trainClass.colRange(0 , 1); \r     rng.fill(c, RNG::UNIFORM, Scalar(0.6*WIDTH), Scalar(WIDTH));\r     // The y coordinate of the points is in [0, 1)\r     c = trainClass.colRange(1,2);\r     rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));\r\r  In the second part we create data for both classes that is non-linearly separable, data that overlaps.\r\r  .. code-block:: cpp\r\r     // Generate random points for the classes 1 and 2\r     trainClass = trainData.rowRange(  nLinearSamples, 2*NTRAINING_SAMPLES-nLinearSamples);\r     // The x coordinate of the points is in [0.4, 0.6)\r     c = trainClass.colRange(0,1);\r     rng.fill(c, RNG::UNIFORM, Scalar(0.4*WIDTH), Scalar(0.6*WIDTH)); \r     // The y coordinate of the points is in [0, 1)\r     c = trainClass.colRange(1,2);\r     rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT)); \r\r2. **Set up SVM's parameters**\r\r  .. seealso::\r\r      In the previous tutorial :ref:`introductiontosvms` there is an explanation of the atributes of the class :svms:`CvSVMParams <cvsvmparams>` that we configure here before training the SVM.\r\r  .. code-block:: cpp\r\r     CvSVMParams params;\r     params.svm_type    = SVM::C_SVC;\r     params.C              = 0.1;\r     params.kernel_type = SVM::LINEAR;\r     params.term_crit   = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);\r\r  There are just two differences between the configuration we do here and the one that was done in the :ref:`previous tutorial <introductiontosvms>` that we use as reference.\r\r  * *CvSVM::C_SVC*. We chose here a small value of this parameter in order not to punish too much the misclassification errors in the optimization. The idea of doing this stems from the will of obtaining a solution close to the one intuitively expected. However, we recommend to get a better insight of the problem by making adjustments to this parameter.\r\r      .. note:: Here there are just very few points in the overlapping region between classes, giving a smaller value to **FRAC_LINEAR_SEP** the density of points can be incremented and the impact of the parameter **CvSVM::C_SVC** explored deeply.\r\r  * *Termination Criteria of the algorithm*. The maximum number of iterations has to be increased considerably in order to solve correctly a problem with non-linearly separable training data. In particular, we have increased in five orders of magnitude this value.   \r\r3. **Train the SVM**\r\r  We call the method :svms:`CvSVM::train <cvsvm-train>` to build the SVM model. Watch out that the training process may take a quite long time. Have patiance when your run the program.\r\r  .. code-block:: cpp\r\r     CvSVM svm;\r     svm.train(trainData, labels, Mat(), Mat(), params);\r\r4. **Show the Decision Regions**\r\r  The method :svms:`CvSVM::predict <cvsvm-predict>` is used to classify an input sample using a trained SVM. In this example we have used this method in order to color the space depending on the prediction done by the SVM. In other words, an image is traversed interpreting its pixels as points of the Cartesian plane. Each of the points is colored depending on the class predicted by the SVM; in dark green if it is the class with label 1 and in dark blue if it is the class with label 2.\r\r  .. code-block:: cpp\r\r     Vec3b green(0,100,0), blue (100,0,0);\r     for (int i = 0; i < I.rows; ++i)\r          for (int j = 0; j < I.cols; ++j)\r          {\r               Mat sampleMat = (Mat_<float>(1,2) << i, j);\r               float response = svm.predict(sampleMat);\r\r               if      (response == 1)    I.at<Vec3b>(j, i)  = green;\r               else if (response == 2)    I.at<Vec3b>(j, i)  = blue;\r          }\r\r5. **Show the training data**\r\r  The method :drawingFunc:`circle <circle>` is used to show the samples that compose the training data. The samples of the class labeled with 1 are shown in light green and in light blue the samples of the class labeled with 2.\r\r  .. code-block:: cpp\r\r     int thick = -1;\r     int lineType = 8;\r     float px, py;\r     // Class 1\r     for (int i = 0; i < NTRAINING_SAMPLES; ++i)\r     {\r          px = trainData.at<float>(i,0);\r          py = trainData.at<float>(i,1);\r          circle(I, Point( (int) px,  (int) py ), 3, Scalar(0, 255, 0), thick, lineType);\r     }\r     // Class 2\r     for (int i = NTRAINING_SAMPLES; i <2*NTRAINING_SAMPLES; ++i)\r     {\r          px = trainData.at<float>(i,0);\r          py = trainData.at<float>(i,1);\r          circle(I, Point( (int) px, (int) py ), 3, Scalar(255, 0, 0), thick, lineType);\r     }\r\r6. **Support vectors**\r\r  We use here a couple of methods to obtain information about the support vectors. The method :svms:`CvSVM::get_support_vector_count <cvsvm-get-support-vector>` outputs the total number of support vectors used in the problem and with the method :svms:`CvSVM::get_support_vector <cvsvm-get-support-vector>` we obtain each of the support vectors using an index. We have used this methods here to find the training examples that are support vectors and highlight them.\r\r  .. code-block:: cpp\r\r     thick = 2;\r     lineType  = 8;\r     int x     = svm.get_support_vector_count();\r\r     for (int i = 0; i < x; ++i)\r     {\r          const float* v = svm.get_support_vector(i);\r          circle(     I,  Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thick, lineType);\r     }\r\rResults\r=======\r\r.. container:: enumeratevisibleitemswithsquare\r\r   * The code opens an image and shows the training examples of both classes. The points of one class are represented with light green and light blue ones are used for the other class.\r\r   * The SVM is trained and used to classify all the pixels of the image. This results in a division of the image in a blue region and a green region. The boundary between both regions is the separating hyperplane. Since the training data is non-linearly separable, it can be seen that some of the examples of both classes are misclassified; some green points lay on the blue region and some blue points lay on the green one.\r\r   * Finally the support vectors are shown using gray rings around the training examples.\r\r.. image:: images/result.png\r  :alt: Training data and decision regions given by the SVM\r  :width: 300pt\r  :align: center \r\rYou may observe a runtime instance of this on the `YouTube here <https://www.youtube.com/watch?v=vFv2yPcSo-Q>`_. \r\r.. raw:: html\r\r  <div align="center">\r  <iframe title="Support Vector Machines for Non-Linearly Separable Data" width="560" height="349" src="http://www.youtube.com/embed/vFv2yPcSo-Q?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>\r  </div>
\ No newline at end of file
diff --git a/doc/tutorials/ml/table_of_content_ml/images/non_linear_svms.png b/doc/tutorials/ml/table_of_content_ml/images/non_linear_svms.png
new file mode 100644 (file)
index 0000000..bd185d4
Binary files /dev/null and b/doc/tutorials/ml/table_of_content_ml/images/non_linear_svms.png differ
index b2b581a..34bd446 100644 (file)
@@ -26,6 +26,25 @@ Use the powerfull machine learning classes for statistical classification, regre
      :height: 90pt\r
      :width:  90pt\r
 \r
++ \r
+  .. tabularcolumns:: m{100pt} m{300pt}\r
+  .. cssclass:: toctableopencv\r
+\r
+  ============ ==============================================\r
+  |NonLinSVM|  **Title:** :ref:`nonLinearSvmS`\r
+\r
+               *Compatibility:* > OpenCV 2.0\r
+\r
+               *Author:* |Author_FernandoI|\r
+\r
+               Here you will learn how to define the optimization problem for SVMs when it is not possible to separate linearly the training data.\r
+\r
+  ============ ==============================================\r
+\r
+  .. |NonLinSVM| image:: images/non_linear_svms.png\r
+     :height: 90pt\r
+     :width:  90pt\r
+\r
 .. raw:: latex\r
 \r
    \pagebreak\r
@@ -34,3 +53,4 @@ Use the powerfull machine learning classes for statistical classification, regre
    :hidden:\r
    \r
    ../introduction_to_svm/introduction_to_svm\r
+   ../non_linear_svms/non_linear_svms\r
index b064f73..42663e0 100644 (file)
@@ -56,7 +56,7 @@ void help()
 \r
 int main(int argc, char *argv[])\r
 {\r
-       help(); \r
+    help(); \r
     Mat I1 = imread(argv[1]);           // Read the two images\r
     Mat I2 = imread(argv[2]);\r
 \r
@@ -68,7 +68,7 @@ int main(int argc, char *argv[])
 \r
     BufferPSNR bufferPSNR;\r
     BufferMSSIM bufferMSSIM;\r
-    \r
+\r
     int TIMES; \r
     stringstream sstr(argv[3]); \r
     sstr >> TIMES;\r
@@ -103,7 +103,7 @@ int main(int argc, char *argv[])
     result = getPSNR_GPU_optimized(I1, I2, bufferPSNR);\r
     time = 1000*((double)getTickCount() - time)/getTickFrequency();\r
     cout << "Initial call GPU optimized:              " << time  <<" milliseconds."\r
-         << " With result of: " << result << endl;\r
+        << " With result of: " << result << endl;\r
 \r
     time = (double)getTickCount();    \r
     for (int i = 0; i < TIMES; ++i)\r
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
     time /= TIMES;\r
 \r
     cout << "Time of PSNR GPU OPTIMIZED ( / " << TIMES << " runs): " << time \r
-         << " milliseconds." << " With result of: " <<  result << endl << endl; \r
+        << " milliseconds." << " With result of: " <<  result << endl << endl; \r
 \r
 \r
     //------------------------------- SSIM CPU -----------------------------------------------------\r
@@ -183,50 +183,52 @@ double getPSNR(const Mat& I1, const Mat& I2)
     }\r
 }\r
 \r
-double getPSNR_GPU(const Mat& I1, const Mat& I2)\r
-{\r
-    gpu::GpuMat gI1, gI2, gs, t1,t2; \r
 \r
-    gI1.upload(I1);\r
-    gI2.upload(I2);\r
 \r
-    gI1.convertTo(t1, CV_32F);\r
-    gI2.convertTo(t2, CV_32F);\r
+double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)\r
+{    \r
+    b.gI1.upload(I1);\r
+    b.gI2.upload(I2);\r
 \r
-    gpu::absdiff(t1.reshape(1), t2.reshape(1), gs); \r
-    gpu::multiply(gs, gs, gs);\r
-    \r
-    Scalar s = gpu::sum(gs);\r
-    double sse = s.val[0] + s.val[1] + s.val[2];\r
+    b.gI1.convertTo(b.t1, CV_32F);\r
+    b.gI2.convertTo(b.t2, CV_32F);\r
+\r
+    gpu::absdiff(b.t1.reshape(1), b.t2.reshape(1), b.gs);\r
+    gpu::multiply(b.gs, b.gs, b.gs);\r
+\r
+    double sse = gpu::sum(b.gs, b.buf)[0];\r
 \r
     if( sse <= 1e-10) // for small values return zero\r
         return 0;\r
     else\r
     {\r
-        double  mse =sse /(double)(gI1.channels() * I1.total());\r
+        double mse = sse /(double)(I1.channels() * I1.total());\r
         double psnr = 10.0*log10((255*255)/mse);\r
         return psnr;\r
     }\r
 }\r
 \r
-double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)\r
-{    \r
-    b.gI1.upload(I1);\r
-    b.gI2.upload(I2);\r
+double getPSNR_GPU(const Mat& I1, const Mat& I2)\r
+{\r
+    gpu::GpuMat gI1, gI2, gs, t1,t2; \r
 \r
-    b.gI1.convertTo(b.t1, CV_32F);\r
-    b.gI2.convertTo(b.t2, CV_32F);\r
+    gI1.upload(I1);\r
+    gI2.upload(I2);\r
 \r
-    gpu::absdiff(b.t1.reshape(1), b.t2.reshape(1), b.gs);\r
-    gpu::multiply(b.gs, b.gs, b.gs);\r
+    gI1.convertTo(t1, CV_32F);\r
+    gI2.convertTo(t2, CV_32F);\r
 \r
-    double sse = gpu::sum(b.gs, b.buf)[0];\r
+    gpu::absdiff(t1.reshape(1), t2.reshape(1), gs); \r
+    gpu::multiply(gs, gs, gs);\r
+\r
+    Scalar s = gpu::sum(gs);\r
+    double sse = s.val[0] + s.val[1] + s.val[2];\r
 \r
     if( sse <= 1e-10) // for small values return zero\r
         return 0;\r
     else\r
     {\r
-        double mse = sse /(double)(I1.channels() * I1.total());\r
+        double  mse =sse /(double)(gI1.channels() * I1.total());\r
         double psnr = 10.0*log10((255*255)/mse);\r
         return psnr;\r
     }\r
@@ -235,6 +237,7 @@ double getPSNR_GPU_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b)
 Scalar getMSSIM( const Mat& i1, const Mat& i2)\r
 { \r
     const double C1 = 6.5025, C2 = 58.5225;\r
+    /***************************** INITS **********************************/\r
     int d     = CV_32F;\r
 \r
     Mat I1, I2; \r
@@ -244,8 +247,10 @@ Scalar getMSSIM( const Mat& i1, const Mat& i2)
     Mat I2_2   = I2.mul(I2);        // I2^2\r
     Mat I1_2   = I1.mul(I1);        // I1^2\r
     Mat I1_I2  = I1.mul(I2);        // I1 * I2\r
-       \r
-    Mat mu1, mu2;  \r
+\r
+    /*************************** END INITS **********************************/\r
+\r
+    Mat mu1, mu2;   // PRELIMINARY COMPUTING\r
     GaussianBlur(I1, mu1, Size(11, 11), 1.5);\r
     GaussianBlur(I2, mu2, Size(11, 11), 1.5);\r
 \r
@@ -282,10 +287,79 @@ Scalar getMSSIM( const Mat& i1, const Mat& i2)
     return mssim; \r
 }\r
 \r
+Scalar getMSSIM_GPU( const Mat& i1, const Mat& i2)\r
+{ \r
+    const float C1 = 6.5025f, C2 = 58.5225f;\r
+    /***************************** INITS **********************************/\r
+    gpu::GpuMat gI1, gI2, gs1, t1,t2; \r
+\r
+    gI1.upload(i1);\r
+    gI2.upload(i2);\r
+\r
+    gI1.convertTo(t1, CV_MAKE_TYPE(CV_32F, gI1.channels()));\r
+    gI2.convertTo(t2, CV_MAKE_TYPE(CV_32F, gI2.channels()));\r
+\r
+    vector<gpu::GpuMat> vI1, vI2; \r
+    gpu::split(t1, vI1);\r
+    gpu::split(t2, vI2);\r
+    Scalar mssim;\r
+\r
+    for( int i = 0; i < gI1.channels(); ++i )\r
+    {\r
+        gpu::GpuMat I2_2, I1_2, I1_I2; \r
+\r
+        gpu::multiply(vI2[i], vI2[i], I2_2);        // I2^2\r
+        gpu::multiply(vI1[i], vI1[i], I1_2);        // I1^2\r
+        gpu::multiply(vI1[i], vI2[i], I1_I2);       // I1 * I2\r
+\r
+        /*************************** END INITS **********************************/\r
+        gpu::GpuMat mu1, mu2;   // PRELIMINARY COMPUTING\r
+        gpu::GaussianBlur(vI1[i], mu1, Size(11, 11), 1.5);\r
+        gpu::GaussianBlur(vI2[i], mu2, Size(11, 11), 1.5);\r
+\r
+        gpu::GpuMat mu1_2, mu2_2, mu1_mu2; \r
+        gpu::multiply(mu1, mu1, mu1_2);   \r
+        gpu::multiply(mu2, mu2, mu2_2);   \r
+        gpu::multiply(mu1, mu2, mu1_mu2);   \r
+\r
+        gpu::GpuMat sigma1_2, sigma2_2, sigma12; \r
+\r
+        gpu::GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);\r
+        sigma1_2 -= mu1_2;\r
+\r
+        gpu::GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);\r
+        sigma2_2 -= mu2_2;\r
+\r
+        gpu::GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);\r
+        sigma12 -= mu1_mu2;\r
+\r
+        ///////////////////////////////// FORMULA ////////////////////////////////\r
+        gpu::GpuMat t1, t2, t3; \r
+\r
+        t1 = 2 * mu1_mu2 + C1; \r
+        t2 = 2 * sigma12 + C2; \r
+        gpu::multiply(t1, t2, t3);     // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))\r
+\r
+        t1 = mu1_2 + mu2_2 + C1; \r
+        t2 = sigma1_2 + sigma2_2 + C2;     \r
+        gpu::multiply(t1, t2, t1);     // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))\r
+\r
+        gpu::GpuMat ssim_map;\r
+        gpu::divide(t3, t1, ssim_map);      // ssim_map =  t3./t1;\r
+\r
+        Scalar s = gpu::sum(ssim_map);    \r
+        mssim.val[i] = s.val[0] / (ssim_map.rows * ssim_map.cols);\r
+\r
+    }\r
+    return mssim; \r
+}\r
+\r
 Scalar getMSSIM_GPU_optimized( const Mat& i1, const Mat& i2, BufferMSSIM& b)\r
 { \r
     int cn = i1.channels();\r
+\r
     const float C1 = 6.5025f, C2 = 58.5225f;\r
+    /***************************** INITS **********************************/\r
 \r
     b.gI1.upload(i1);\r
     b.gI2.upload(i2);\r
diff --git a/samples/cpp/tutorial_code/ml/non_linear_svms/non_linear_svms.cpp b/samples/cpp/tutorial_code/ml/non_linear_svms/non_linear_svms.cpp
new file mode 100644 (file)
index 0000000..44320fa
--- /dev/null
@@ -0,0 +1,130 @@
+#include <iostream>
+#include <opencv2/core/core.hpp>
+#include <opencv2/highgui/highgui.hpp>
+#include <opencv2/ml/ml.hpp>
+
+#define        NTRAINING_SAMPLES       100                     // Number of training samples per class
+#define FRAC_LINEAR_SEP                0.9f        // Fraction of samples which compose the linear separable part
+
+using namespace cv;
+using namespace std;
+
+void help()
+{
+    cout<< "\n--------------------------------------------------------------------------" << endl
+        << "This program shows Support Vector Machines for Non-Linearly Separable Data. " << endl
+        << "Usage:"                                                               << endl
+        << "./non_linear_svms" << endl
+        << "--------------------------------------------------------------------------"   << endl
+        << endl;
+}
+
+int main()
+{
+    help();
+
+    // Data for visual representation
+    const int WIDTH = 512, HEIGHT = 512;
+    Mat I = Mat::zeros(HEIGHT, WIDTH, CV_8UC3);
+
+    //--------------------- 1. Set up training data randomly ---------------------------------------
+    Mat trainData(2*NTRAINING_SAMPLES, 2, CV_32FC1);
+    Mat labels   (2*NTRAINING_SAMPLES, 1, CV_32FC1);
+    
+    RNG rng(100); // Random value generation class
+
+    // Set up the linearly separable part of the training data
+    int nLinearSamples = (int) (FRAC_LINEAR_SEP * NTRAINING_SAMPLES);
+
+    // Generate random points for the class 1
+    Mat trainClass = trainData.rowRange(0, nLinearSamples);
+    // The x coordinate of the points is in [0, 0.4)
+    Mat c = trainClass.colRange(0, 1);
+    rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * WIDTH));
+    // The y coordinate of the points is in [0, 1)
+    c = trainClass.colRange(1,2);
+    rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
+
+    // Generate random points for the class 2
+    trainClass = trainData.rowRange(2*NTRAINING_SAMPLES-nLinearSamples, 2*NTRAINING_SAMPLES);
+    // The x coordinate of the points is in [0.6, 1]
+    c = trainClass.colRange(0 , 1); 
+    rng.fill(c, RNG::UNIFORM, Scalar(0.6*WIDTH), Scalar(WIDTH));
+    // The y coordinate of the points is in [0, 1)
+    c = trainClass.colRange(1,2);
+    rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
+
+    //------------------ Set up the non-linearly separable part of the training data ---------------
+
+    // Generate random points for the classes 1 and 2
+    trainClass = trainData.rowRange(  nLinearSamples, 2*NTRAINING_SAMPLES-nLinearSamples);
+    // The x coordinate of the points is in [0.4, 0.6)
+    c = trainClass.colRange(0,1);
+    rng.fill(c, RNG::UNIFORM, Scalar(0.4*WIDTH), Scalar(0.6*WIDTH)); 
+    // The y coordinate of the points is in [0, 1)
+    c = trainClass.colRange(1,2);
+    rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
+    
+    //------------------------- Set up the labels for the classes ---------------------------------
+    labels.rowRange(                0,   NTRAINING_SAMPLES).setTo(1);  // Class 1
+    labels.rowRange(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES).setTo(2);  // Class 2
+
+    //------------------------ 2. Set up the support vector machines parameters --------------------
+    CvSVMParams params;
+    params.svm_type    = SVM::C_SVC;
+    params.C              = 0.1;
+    params.kernel_type = SVM::LINEAR;
+    params.term_crit   = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);
+
+    //------------------------ 3. Train the svm ----------------------------------------------------
+    cout << "Starting training process" << endl;
+    CvSVM svm;
+    svm.train(trainData, labels, Mat(), Mat(), params);
+    cout << "Finished training process" << endl;
+    
+    //------------------------ 4. Show the decision regions ----------------------------------------
+       Vec3b green(0,100,0), blue (100,0,0);
+    for (int i = 0; i < I.rows; ++i)
+        for (int j = 0; j < I.cols; ++j)
+        {
+            Mat sampleMat = (Mat_<float>(1,2) << i, j);
+            float response = svm.predict(sampleMat);
+
+            if      (response == 1)    I.at<Vec3b>(j, i)  = green;
+            else if (response == 2)    I.at<Vec3b>(j, i)  = blue;
+        }
+
+    //----------------------- 5. Show the training data --------------------------------------------
+    int thick = -1;
+    int lineType = 8;
+    float px, py;
+    // Class 1
+    for (int i = 0; i < NTRAINING_SAMPLES; ++i)
+    {
+        px = trainData.at<float>(i,0);
+        py = trainData.at<float>(i,1);
+        circle(I, Point( (int) px,  (int) py ), 3, Scalar(0, 255, 0), thick, lineType);
+    }
+    // Class 2
+    for (int i = NTRAINING_SAMPLES; i <2*NTRAINING_SAMPLES; ++i)
+    {
+        px = trainData.at<float>(i,0);
+        py = trainData.at<float>(i,1);
+        circle(I, Point( (int) px, (int) py ), 3, Scalar(255, 0, 0), thick, lineType);
+    }
+
+    //------------------------- 6. Show support vectors --------------------------------------------
+    thick = 2;
+    lineType  = 8;
+    int x     = svm.get_support_vector_count();
+
+    for (int i = 0; i < x; ++i)
+    {
+        const float* v = svm.get_support_vector(i);
+        circle(        I,  Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thick, lineType);
+    }
+
+    imwrite("result.png", I);                     // save the Image
+    imshow("SVM for Non-Linear Training Data", I); // show it to the user
+    waitKey(0);
+}
\ No newline at end of file