]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/tests/cxcore/src/apca.cpp
added PCA test
[opencv.git] / opencv / tests / cxcore / src / apca.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #include "cxcoretest.h"\r
43 \r
44 using namespace cv;\r
45 \r
46 \r
47 class CV_PCATest : public CvTest\r
48 {\r
49 public:\r
50     CV_PCATest() : CvTest( "pca", "PCA funcs" ) {}\r
51 protected:\r
52     void run( int);\r
53 };\r
54 \r
55 void CV_PCATest::run( int )\r
56 {\r
57     int code = CvTS::OK, err;\r
58     int maxComponents = 1;\r
59     Mat points( 1000, 3, CV_32FC1);\r
60     randn( points, Scalar::all(0.0), Scalar::all(1.0) );\r
61     float mp[] = { 3.0f, 3.0f, 3.0f }, cp[] = { 0.5f, 0.0f, 0.0f,\r
62                                                 0.0f, 1.0f, 0.0f,\r
63                                                 0.0f, 0.0f, 0.3f };\r
64     Mat mean( 1, 3, CV_32FC1, mp ),\r
65         cov( 3, 3, CV_32FC1, cp );\r
66     for( int i = 0; i < points.rows; i++ )\r
67     {\r
68         Mat r(1, points.cols, CV_32FC1, points.ptr<float>(i));\r
69         r =  r * cov + mean; \r
70     }\r
71 \r
72     PCA pca( points, Mat(), CV_PCA_DATA_AS_ROW, maxComponents );\r
73 \r
74     // check project\r
75     Mat prjPoints = pca.project( points );\r
76     err = 0;\r
77     for( int i = 0; i < prjPoints.rows; i++ )\r
78     {\r
79         float val = prjPoints.at<float>(i,0);\r
80         if( val > 3.0f || val < -3.0f )\r
81             err++;\r
82     }\r
83     if( (float)err > prjPoints.rows*0.01f )\r
84     {
85         ts->printf( CvTS::LOG, "bad accuracy of project()" );
86         code = CvTS::FAIL_BAD_ACCURACY;
87     }
88
89     // check backProject
90     Mat points1 = pca.backProject( prjPoints );
91     err = 0;
92     for( int i = 0; i < points.rows; i++ )\r    {\r        if( fabs(points1.at<float>(i,0) - mean.at<float>(0,0)) > 0.05 ||\r            fabs(points1.at<float>(i,1) - points.at<float>(i,1)) > 0.02 ||\r            fabs(points1.at<float>(i,2) - mean.at<float>(0,2)) > 0.05 )\r            err++;\r    }\r    if( (float)err > prjPoints.rows*0.04f )\r
93     {
94         ts->printf( CvTS::LOG, "bad accuracy of backProject()" );
95         code = CvTS::FAIL_BAD_ACCURACY;
96     }
97
98     ts->set_failed_test_info( code );\r
99 }\r
100 \r
101 CV_PCATest pca_test;