From f25603112a5d6d6384f07504c98ca1388bebec23 Mon Sep 17 00:00:00 2001 From: Bernat Gabor Date: Mon, 25 Jul 2011 07:15:52 +0000 Subject: [PATCH] Added Fernandos first tutorial regarding SVMs. Extended the global links (conf.py). Corrected the author names. Minor modification to the dft tutorial. --- doc/conf.py | 2 + .../discrete_fourier_transform.rst | 23 +++ doc/tutorials/definitions/tocDefinitions.rst | 4 +- .../images/optimal-hyperplane.png | Bin 0 -> 7998 bytes .../ml/introduction_to_svm/images/result.png | Bin 0 -> 1886 bytes .../images/separating-lines.png | Bin 0 -> 7731 bytes .../introduction_to_svm.rst | 188 ++++++++++++++++++ .../images/introduction_to_svm.png | Bin 0 -> 1402 bytes .../table_of_content_ml.rst | 26 ++- .../discrete_fourier_transform.cpp | 3 +- .../introduction_to_svm.cpp | 68 +++++++ 11 files changed, 309 insertions(+), 5 deletions(-) create mode 100644 doc/tutorials/ml/introduction_to_svm/images/optimal-hyperplane.png create mode 100644 doc/tutorials/ml/introduction_to_svm/images/result.png create mode 100644 doc/tutorials/ml/introduction_to_svm/images/separating-lines.png create mode 100644 doc/tutorials/ml/introduction_to_svm/introduction_to_svm.rst create mode 100644 doc/tutorials/ml/table_of_content_ml/images/introduction_to_svm.png create mode 100644 samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp diff --git a/doc/conf.py b/doc/conf.py index e8c045ffb..88a5c2644 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -348,10 +348,12 @@ extlinks = {'cvt_color': ('http://opencv.willowgarage.com/documentation/cpp/imgp 'contour_area' : ('http://opencv.willowgarage.com/documentation/cpp/imgproc_structural_analysis_and_shape_descriptors.html#cv-contourarea%s', None), 'arc_length' : ('http://opencv.willowgarage.com/documentation/cpp/imgproc_structural_analysis_and_shape_descriptors.html#cv-arclength%s', None), 'basicstructures' : ('http://opencv.itseez.com/modules/core/doc/basic_structures.html#%s', None), + 'oldbasicstructures' : ('http://opencv.itseez.com/modules/core/doc/old_basic_structures.html#%s', None), 'readwriteimagevideo' : ('http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html#%s', None), 'operationsonarrays' : ('http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html#%s', None), '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), 'point_polygon_test' : ('http://opencv.willowgarage.com/documentation/cpp/imgproc_structural_analysis_and_shape_descriptors.html#cv-pointpolygontest%s', None) } diff --git a/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.rst b/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.rst index bf98b5b5d..354c07d35 100644 --- a/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.rst +++ b/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.rst @@ -13,3 +13,26 @@ We'll seek answers for the following questions: + What is a Fourier transform and why use it? + How to do it in OpenCV? + Usage of functions such as: :imgprocfilter:`copyMakeBorder() `, :operationsonarrays:`merge() `, :operationsonarrays:`dft() `, :operationsonarrays:`getOptimalDFTSize() `, :operationsonarrays:`log() ` and :operationsonarrays:`normalize() ` . + +Source code +=========== + +Here's a sample usage of :operationsonarrays:`dft() ` : + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 1-3, 5, 19-20, 23-78 + +Explanation +=========== + +The Fourier Transform will decompose an image into its sinus and cosines components. In other words, it will transform an image from its spatial domain to its frequency domain. The idea is that any function may be approximated exactly with the sum of infinite sinus and cosines functions. The Fourier Transform is a way how to do this. Mathematically a two dimensional images Fourier transform is: + +.. math:: + + F(k,l) = \displaystyle\sum\limits_{i=0}^{N-1}\sum\limits_{j=0}^{N-1} f(i,j)e^{-i2\pi(\frac{ki}{N}+\frac{lj}{N})} + + e^{ix} = \cos{x} + i\sin {x} + diff --git a/doc/tutorials/definitions/tocDefinitions.rst b/doc/tutorials/definitions/tocDefinitions.rst index 7f7dca7aa..2793de8d6 100644 --- a/doc/tutorials/definitions/tocDefinitions.rst +++ b/doc/tutorials/definitions/tocDefinitions.rst @@ -1,7 +1,7 @@ .. |Author_AnaH| unicode:: Ana U+0020 Huam U+00E1 n .. |Author_BernatG| unicode:: Bern U+00E1 t U+0020 G U+00E1 bor .. |Author_AndreyK| unicode:: Andrey U+0020 Kamaev -.. |Author_VictorE| unicode:: Victor Eruhimov - +.. |Author_VictorE| unicode:: Victor U+0020 Eruhimov +.. |Author_FernandoI| unicode:: Fernando U+0020 Iglesias U+0020 Garc U+00ED a diff --git a/doc/tutorials/ml/introduction_to_svm/images/optimal-hyperplane.png b/doc/tutorials/ml/introduction_to_svm/images/optimal-hyperplane.png new file mode 100644 index 0000000000000000000000000000000000000000..d4522f047986799c7d327ce744f9931cda5e719b GIT binary patch literal 7998 zcmV-EAHm>>P)+AnpTmLv`6sV@DrWhvFh?k?IqdPnQbaZqS6cpRr+c{|t%!$Y+Zvwo&XY< zu8BQt8f%SG+MT6ukXo)FJ@xheEnxv%TwJLPEKYhR*_EU9<>){^{4{0}<>}+LGEM5m z&EDkN*x%H2bpMH@bh5*w%gg`l?dzfd8yr*srMZ+eWeerCzIHhyx45^((Y&mz|6^nS zR((3S#;whAgh6f_W{O5dawBPsO^>L2oU)5+uzC6 zz5@*a`1$v~OJX}}6!hcf_4W1i>Fa)ldW@5Yf{J@#Y*|He9lOk=p0ARw96CO3 zBE?*BZjer;!HG_IE0L;vS${dt*vXUt0Pxk@uLUPPJv}BQDVzWe9vUI%=IA#yIkvR8 z`t$UDe1NN~t42de-{{#xL;pZ+71oQInZ1ovd^FeK(t)0A9ls@E00001bW%=J06^y0 zW&i*H0b)x>L|1(Z*&hG^010qNS#tmYE+YT{E+YYWr9XB6000?uMObuGZ)S9NVRB^v zXKrt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I4;?<7(5IqLroQ@;J2I{Td=PHrv|{Y04o{o?n_90&Mm;0 zJ#G^%F~N(CcG2L`=^1ErtDp}%k)c>+z=8{jhEV{b?gWVg69~b%4X^U_4S(-li>1J3 zm&Hc9+@}GCL;&llqvZ+f%a?D?Tm-ziZ}^#lfqG76;dh5S8Y;kg*Gj?q(D2rqGh}${ z)_V)*`cM^qZ@%)4ho;IB*5$waT!#FpuYj=NBjHvGcX<48II^KEVf|I+8}D4cr+~2F z1L0#7W%6kYr>{7y52pY6Hhkgo^M!;3H_XzkO*KD+f^U|8&w8P-J&RV3{uogfPOm^% z5MDv(TqVL9Iz&?+YF7|Z#snxD*4gdfq&Y+7t{^*qTy7-|!{*sAO*O3zsgsy1O=pQ0 z!jDiin`(v?2n!B;VGI^cDsMKd!m7d|HwJbeufigC6jyIiHmnb3!+L(@hT#<2KB#0^ z!}okQLmuC{g0SH9eUtYq7uMsKvvbS)`kq{in`iaEq+GhUa&5MBuv{%~X;U;@`AEV!%iha<;00OF485f*&C@M=pi zFaQ{YWnpOn6IY(t|4aJu?}fdj-30(p>&0bYsr*2x^3|2NXjHWfEGTOAS|y79&)C2D zswgx51H*nFmNix4UTcc&^cd_(1nK(#U-FyMG*zeQ6~u!R0^(KOQnkic5q zT2Zi$ZlK%6@PTN|bpOzi^FcL!lIP_2b2HY#NxW1fdBvi07*qoM6N<$f|!PA A-~a#s literal 0 HcmV?d00001 diff --git a/doc/tutorials/ml/introduction_to_svm/images/result.png b/doc/tutorials/ml/introduction_to_svm/images/result.png new file mode 100644 index 0000000000000000000000000000000000000000..5f477a4f699af3776b36055f86229c96295c44e3 GIT binary patch literal 1886 zcmcgsSx{3~7(NjqNFYcANmal}2SH`g(ukCylujfVY_J0HLc|6X>5#BV7K9KWA|XLw zY$4EU-H=+dU{H~PS|NdgS}M#~(y~(rl}!*#f`f$Hn=tLzK6Iu&^vpf~{r>Iymxm+X zPbJ&gY_I_U*zFA?9RL93WxCcDh=p1sAOm0l_V0_@1H&*1g|gCFu}WEhR|EhomBPS& z7IgapQWMxbTvJmc5U^NTETeJRyT^NeUKbr59U2Pv^$n6KWRGngB?Tp+q0L=gJ&ukr zRY|4O&q{cb%F5Hg;dxgVoR}z;NLYk^0(J&lRFqs&QWYHB(%A|7`E}uN)2BG6$cfCk`spDyK#JNkcdgaudJ7@lEfaoMu*p_f6khVZ6ce zNjZSItiRPl`ZslLW<=%V4Vw*7fRD_AHz57D0`$^33wy+gVjrMJf{&j5cFd$Cgp)~j zhFkH-e!!aau$W8$eBL!(R90;;vR;eHy`+$+tp4YHQ?zvlH+96hKVdd>5qHPLmO zdR>FEwquoR@#nQ)kH!ozt{Ku{=lwU9#;T4je|3iVTp)1X!JJbVQ+JgF>ER?RH-4cZ zYMvaoby;jo+!iGF(Jn=$9h1koD0wXrhhNXc5bMu3E?#(eyKcQUI}+PFylx6(pQf0l zD+yZ4vnOor(#z?zQ!Tp8txytmnbTruOPb=ziis^j-z3Ila-Y(OGs?!ZoyiUTBmQ;K z4pS4GH{Q;8_3Pu24}S419)WOu0%K3&SO>ELGU$@*`ctFpqTuRBReYBCi=pViqo1yIX_i)}7asIpUK)La8 z)Larx9DXRs*g4QUd3Q8i`KnK5xDe>nOF_v67g5ZEpRJu>RNiVQsU|a-DlSiapHW%g z&Q?)+DAw{P<8UrA}qzwKGI9*G-c^N0{zn z;tYoHiDKuhl2qS8Pmn?Srqzn_=5}^{2b7?JGL#kd8sFk8^_LfD{gB#yOPSN_yo5>U zlKYl&rx=1Ff4!m=7vK954tAh1#H!&7N+RSwLRYN{FLde=Iza?QYo8E7dNt-UO0LMk zn4(t2n*FaHur#k#Q?j6G*&QRmDJTOLv`mS dL%nFPv!A%q>vyKzb>8%X?A=2p)qTtm{sF}u%G>|| literal 0 HcmV?d00001 diff --git a/doc/tutorials/ml/introduction_to_svm/images/separating-lines.png b/doc/tutorials/ml/introduction_to_svm/images/separating-lines.png new file mode 100644 index 0000000000000000000000000000000000000000..93ae457c0c9ab2c012c2189d1d962b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=a27gb*3lo-&R_%#^|yn$NBDj=zKu z7uL^i<=Rg~DdB3>XgQC?T|$TpOFC-~Rp}jX3855N;7X`U?;%&dM}@^TW;D%|5b?}H zC{Finc$8Br36`7Zgsiwo2qnQnO%L%r8Ha@oCBb5>O(#Y|C<<0H zvB3AV@Dlw1g;9VMU0hSIjH=jtu3&g74}5 zp4eFk1;QHj!M6FWA%2^oKv=uQQt&`f7cz^>@VWQipqlnKl26BZ_0c8DS& zlnDzmnr2D}O_UPih1FZN%7z76h4u7)5!BnrN22*x0rw%u&UG-bw6cYji!BKK7B3N< zg!u4I1bMJN0<5b@&XyNsYX@$-N9eb_qw#Kt;!5kb<{-%0E&Q-9Od>M2ula<3&v%I* zM1R4_dOlb{d)?lovem0UAJz9wQl5pZy#eKg1yI#c6lk~ZTAH-30jlv@<6Z<=eNUvj zp=y465JB$K0u>gNUA=D&go0O1K)CY(HB^f-{!n4@Pv~oNwgPZGXKkYjE3zF&kj4u+ z$Z>TT!q|1lB=x^KXTjb7D8-(0RfPp-TM^`b^==3~4w2P<9jd**QH8~CTj(SM?ONI= zkb6{EyjGus-0%W|tO09wtwq+Uuy{r3B(fT%M&erSJzH*8Ve#9V)vKvsuyJzr7V7UR tECJwDSSl=uV5xUQcr8D9vkEI(`+pIb@795QG_e2x002ovPDHLkV1m*$#Wer` literal 0 HcmV?d00001 diff --git a/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.rst b/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.rst new file mode 100644 index 000000000..c2ef546f0 --- /dev/null +++ b/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.rst @@ -0,0 +1,188 @@ +.. _introductiontosvms: + +Introduction to Support Vector Machines +*************************************** + +Goal +==== + +In this tutorial you will learn how to: + +.. container:: enumeratevisibleitemswithsquare + + + Use the OpenCV functions :svms:`CvSVM::train ` to build a classifier based on SVMs and :svms:`CvSVM::predict ` to test its performance. + +What is a SVM? +============== + +A Support Vector Machine (SVM) is a discriminative classifier formally defined by a separating hyperplane. In other words, given labeled training data (*supervised learning*), the algorithm outputs an optimal hyperplane which categorizes new examples. + +In which sense is the hyperplane obtained optimal? Let's consider the following +simple problem: + + For a linearly separable set of 2D-points which belong to one of two classes, find a separating straight line. + +.. image:: images/separating-lines.png + :alt: A seperation example + :align: center + +.. note:: In this example we deal with lines and points in the Cartesian plane instead of hyperplanes and vectors in a high dimensional space. This is a simplification of the problem.It is important to understand that this is done only because our intuition is better built from examples that are easy to imagine. However, the same concepts apply to tasks where the examples to classify lie in a space whose dimension is higher than two. + +In the above picture you can see that there exists multiple lines that offer a solution to the problem. Is any of them better than the others? We can intuitively define a criterion to estimate the worth of the lines: + + A line is bad if it passes too close to the points because it will be noise sensitive and it will not generalize correctly. Therefore, our goal should be to find the line passing as far as possible from all points. + +Then, the operation of the SVM algorithm is based on finding the hyperplane that gives the largest minimum distance to the training examples. Twice, this distance receives the important name of **margin** within SVM's theory. Therefore, the optimal separating hyperplane *maximizes* the margin of the training data. + +.. image:: images/optimal-hyperplane.png + :alt: The Optimal hyperplane + :align: center + +How is the optimal hyperplane computed? +======================================= + +Let's introduce the notation used to define formally a hyperplane: + +.. math:: + f(x) = \beta_{0} + \beta^{T} x, + +where :math:`\beta` is known as the *weight vector* and :math:`\beta_{0}` as the *bias*. + +.. seealso:: A more in depth description of this and hyperplanes you can find in the section 4.5 (*Seperating Hyperplanes*) of the book: *Elements of Statistical Learning* by T. Hastie, R. Tibshirani and J. H. Friedman. + +The optimal hyperplane can be represented in an infinite number of different ways by scaling of :math:`\beta` and :math:`\beta_{0}`. As a matter of convention, among all the possible representations of the hyperplane, the one chosen is + +.. math:: + |\beta_{0} + \beta^{T} x| = 1 + +where :math:`x` symbolizes the training examples closest to the hyperplane. In general, the training examples that are closest to the hyperplane are called **support vectors**. This representation is known as the **canonical hyperplane**. + +Now, we use the result of geometry that gives the distance between a point :math:`x` and a hyperplane :math:`(\beta, \beta_{0})`: + +.. math:: + \mathrm{distance} = \frac{|\beta_{0} + \beta^{T} x|}{||\beta||}. + +In particular, for the canonical hyperplane, the numerator is equal to one and the distance to the support vectors is + +.. math:: + \mathrm{distance}_{\text{ support vectors}} = \frac{|\beta_{0} + \beta^{T} x|}{||\beta||} = \frac{1}{||\beta||}. + +Recall that the margin introduced in the previous section, here denoted as :math:`M`, is twice the distance to the closest examples: + +.. math:: + M = \frac{2}{||\beta||} + +Finally, the problem of maximizing :math:`M` is equivalent to the problem of minimizing a function :math:`L(\beta)` subject to some constraints. The constraints model the requirement for the hyperplane to classify correctly all the training examples :math:`x_{i}`. Formally, + +.. math:: + \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, + +where :math:`y_{i}` represents each of the labels of the training examples. + +This is a problem of Lagrangian optimization that can be solved using Lagrange multipliers to obtain the weight vector :math:`\beta` and the bias :math:`\beta_{0}` of the optimal hyperplane. + +Source Code +=========== + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp + :language: cpp + :linenos: + :tab-width: 4 + +Explanation +=========== + +1. **Set up the training data** + + The training data of this exercise is formed by a set of labeled 2D-points that belong to one of two different classes; one of the classes consists of one point and the other of three points. + + .. code-block:: cpp + + float labels[4] = {1.0, -1.0, -1.0, -1.0}; + float trainingData[4][2] = {{501, 10}, {255, 10}, {501, 255}, {10, 501}}; + + The function :svms:`CvSVM::train ` that will be used afterwards requires the training data to be stored as :basicstructures:`Mat ` objects of floats. Therefore, we create these objects from the arrays defined above: + + .. code-block:: cpp + + Mat trainingDataMat(3, 2, CV_32FC1, trainingData); + Mat labelsMat (3, 1, CV_32FC1, labels); + +2. **Set up SVM's parameters** + + In this tutorial we have introduced the theory of SVMs in the most simple case, when the training examples are spread into two classes that are linearly separable. However, SVMs can be used in a wide variety of problems (e.g. problems with non-linearly separable data, a SVM using a kernel function to raise the dimensionality of the examples, etc). As a consequence of this, we have to define some parameters before training the SVM. These parameters are stored in an object of the class :svms:`CvSVMParams ` . + + .. code-block:: cpp + + CvSVMParams params; + params.svm_type = CvSVM::C_SVC; + params.kernel_type = CvSVM::LINEAR; + params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); + + * *Type of SVM*. We choose here the type **CvSVM::C_SVC** that can be used for n-class classification (n :math:`\geq` 2). This parameter is defined in the attribute *CvSVMParams.svm_type*. + + .. note:: The important feature of the type of SVM **CvSVM::C_SVC** deals with imperfect separation of classes (i.e. when the training data is non-linearly separable). This feature is not important here since the data is linearly separable and we chose this SVM type only for being the most commonly used. + + * *Type of SVM kernel*. We have not talked about kernel functions since they are not interesting for the training data we are dealing with. Nevertheless, let's explain briefly now the main idea behind a kernel function. It is a mapping done to the training data to improve its resemblance to a linearly separable set of data. This mapping consists of increasing the dimensionality of the data and is done efficiently using a kernel function. We choose here the type **CvSVM::LINEAR** which means that no mapping is done. This parameter is defined in the attribute *CvSVMParams.kernel_type*. + + * *Termination criteria of the algorithm*. The SVM training procedure is implemented solving a constrained quadratic optimization problem in an **iterative** fashion. Here we specify a maximum number of iterations and a tolerance error so we allow the algorithm to finish in less number of steps even if the optimal hyperplane has not been computed yet. This parameter is defined in a structure :oldbasicstructures:`cvTermCriteria `. + +3. **Train the SVM** + + We call the method `CvSVM::train `_ to build the SVM model. + + .. code-block:: cpp + + CvSVM SVM; + SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params); + +4. **Regions classified by the SVM** + + The method :svms:`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 green if it is the class with label 1 and in blue if it is the class with label -1. + + .. code-block:: cpp + + Vec3b green(0,255,0), blue (255,0,0); + + for (int i = 0; i < image.rows; ++i) + for (int j = 0; j < image.cols; ++j) + { + Mat sampleMat = (Mat_(1,2) << i,j); + float response = SVM.predict(sampleMat); + + if (response == 1) + image.at(j, i) = green; + else + if (response == -1) + image.at(j, i) = blue; + } + +5. **Support vectors** + + We use here a couple of methods to obtain information about the support vectors. The method :svms:`CvSVM::get_support_vector_count ` outputs the total number of support vectors used in the problem and with the method :svms:`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. + + .. code-block:: cpp + + int c = SVM.get_support_vector_count(); + + for (int i = 0; i < c; ++i) + { + const float* v = SVM.get_support_vector(i); // get and then highlight with grayscale + circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType); + } + +Results +======= + +.. container:: enumeratevisibleitemswithsquare + + * The code opens an image and shows the training examples of both classes. The points of one class are represented with white circles and black ones are used for the other class. + + * 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 optimal separating hyperplane. + + * Finally the support vectors are shown using gray rings around the training examples. + +.. image:: images/result.png + :alt: The seperated planes + :align: center + diff --git a/doc/tutorials/ml/table_of_content_ml/images/introduction_to_svm.png b/doc/tutorials/ml/table_of_content_ml/images/introduction_to_svm.png new file mode 100644 index 0000000000000000000000000000000000000000..f2d63751fc07ea78272082cee0444c36ad1ada92 GIT binary patch literal 1402 zcmd5)doWyA9R6Kb>-dwVWzNB-?!J#&8FIp2AF=R4>8 z@(+gjvo>381^`xIfZz~nGwERB2)EpoPXXX?FhuA_Q4|2GdVp0hScMu9%}6F1z$vs? zXKg)}o?elZBxAAW0n@?G$MC=fmJH2anu*Fqv3aqMj~tVetMvNC`FRze--2PQK+He~ zA69T|6%+IbTnHBiFyeCKA|r22P5nGDaEHt70fQB&jD=+!bg)OYC&V*^YsD3ni5QF* zJYGASEyu7GKvGrFm=1GSx%>6_u}Bt%3{=HHwHR*3fGi5CqoGO+S_TxP0=|M_dZDl` zC8ff~X3WE*U9Ild=@v6GzIAfy8yvhdH}_B?k#V`*e15a3sm91i@9eCfn9wyg>TsO$ z_t#O>s#>jQFen_?3j}&L8@E6A94t9(;3X>8 z>Ay&jE?^4cGdi>v;4&JgGk~H9a*7l}4-gp<%gXF_ib}LVxj|^r0y=2@HvkNM0YVUS zDvCmQc@qLua^iBT6jqXBdY5%Zkq3_ejsKrhl3OfwGGh-5(d=l(0pk#~}eTW!K1ESLaICgYk znKB>ryaq;rf&;?LN6*Gb!+KpTjHZ@Bej`Eq-q}`#L8b5RvkUirTJbTuUB8PNR=8`t zYwmWfFo@Ho_e{?&stFjzs)Kgim&(3TDP!b>`_;u$+mpVpX4{jjE1~L=1m^M7%K9%{ z+HTd~t8(KiDr@UHzALHj&Mf|ue27Vk7*Cap{SKk!HwvzVhZr!9TDs;Pds-AxE^%u~ zdzt^%rNT6mFN1+gn(;o~JNVUflxXx?uPoZq(Zt+NQyBD|UkTiXPK75iy%^#>bgh@7 zntT^sOY6H`TRJD|xy@c;$(9_lP4RX@v8RJPvDG+un{CZ6BK67BttNwIh84#LlDGY) z$?CP8xG1w>ejw3E9ZWcTseWxj^f6BB&1PJjH1n&16JO@p?6dEDlEl0;AvL~u=~eAX z!#Yy&!@2&3DIc$VH{RW&ZMh_8$!T9+v8l{HKUh4{uDZ^8Ly5DkvqSRIGt{) zQH(d)f5y_he?qx^w&{6bv&xI@hW}22W|Zk2zk4C;{IoBTkZ1lO4Gm1*A8kDa1zlEJ z<5$^5a<^sXyC`^ F`~^xF&;0-Z literal 0 HcmV?d00001 diff --git a/doc/tutorials/ml/table_of_content_ml/table_of_content_ml.rst b/doc/tutorials/ml/table_of_content_ml/table_of_content_ml.rst index 1f7f13861..b2b581afa 100644 --- a/doc/tutorials/ml/table_of_content_ml/table_of_content_ml.rst +++ b/doc/tutorials/ml/table_of_content_ml/table_of_content_ml.rst @@ -5,8 +5,32 @@ Use the powerfull machine learning classes for statistical classification, regression and clustering of data. -.. include:: ../../definitions/noContent.rst +.. include:: ../../definitions/tocDefinitions.rst + ++ + .. tabularcolumns:: m{100pt} m{300pt} + .. cssclass:: toctableopencv + + ============ ============================================== + |IntroSVM| **Title:** :ref:`introductiontosvms` + + *Compatibility:* > OpenCV 2.0 + + *Author:* |Author_FernandoI| + + Learn what a Suport Vector Machine is. + + ============ ============================================== + + .. |IntroSVM| image:: images/introduction_to_svm.png + :height: 90pt + :width: 90pt .. raw:: latex \pagebreak + +.. toctree:: + :hidden: + + ../introduction_to_svm/introduction_to_svm diff --git a/samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp b/samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp index 6f05d0780..43bdd5c15 100644 --- a/samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp +++ b/samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp @@ -75,5 +75,4 @@ int main(int argc, char ** argv) waitKey(); return 0; -} - +} \ No newline at end of file diff --git a/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp b/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp new file mode 100644 index 000000000..b01059dd8 --- /dev/null +++ b/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace cv; + +int main() +{ + // Data for visual representation + int width = 512, height = 512; + Mat image = Mat::zeros(height, width, CV_8UC3); + + // Set up training data + float labels[4] = {1.0, -1.0, -1.0, -1.0}; + Mat labelsMat(3, 1, CV_32FC1, labels); + + float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} }; + Mat trainingDataMat(3, 2, CV_32FC1, trainingData); + + // Set up SVM's parameters + CvSVMParams params; + params.svm_type = CvSVM::C_SVC; + params.kernel_type = CvSVM::LINEAR; + params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); + + // Train the SVM + CvSVM SVM; + SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params); + + Vec3b green(0,255,0), blue (255,0,0); + // Show the decision regions given by the SVM + for (int i = 0; i < image.rows; ++i) + for (int j = 0; j < image.cols; ++j) + { + Mat sampleMat = (Mat_(1,2) << i,j); + float response = SVM.predict(sampleMat); + + if (response == 1) + image.at(j, i) = green; + else if (response == -1) + image.at(j, i) = blue; + } + + // Show the training data + int thickness = -1; + int lineType = 8; + circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType); + circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType); + circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType); + circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType); + + // Show support vectors + thickness = 2; + lineType = 8; + int c = SVM.get_support_vector_count(); + + for (int i = 0; i < c; ++i) + { + const float* v = SVM.get_support_vector(i); + circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType); + } + + imwrite("result.png", image); // save the image + + imshow("SVM Simple Example", image); // show it to the user + waitKey(0); + +}