1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\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
10 // Intel License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
\r
14 // Third party copyrights are property of their respective owners.
\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
19 // * Redistribution's of source code must retain the above copyright notice,
\r
20 // this list of conditions and the following disclaimer.
\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
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
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
42 #include "cxcoretest.h"
\r
47 class CV_PCATest : public CvTest
\r
50 CV_PCATest() : CvTest( "pca", "PCA funcs" ) {}
\r
55 void CV_PCATest::run( int )
\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
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
68 Mat r(1, points.cols, CV_32FC1, points.ptr<float>(i));
\r
69 r = r * cov + mean;
\r
72 PCA pca( points, Mat(), CV_PCA_DATA_AS_ROW, maxComponents );
\r
75 Mat prjPoints = pca.project( points );
\r
77 for( int i = 0; i < prjPoints.rows; i++ )
\r
79 float val = prjPoints.at<float>(i,0);
\r
80 if( val > 3.0f || val < -3.0f )
\r
83 if( (float)err > prjPoints.rows*0.01f )
\r
85 ts->printf( CvTS::LOG, "bad accuracy of project()" );
86 code = CvTS::FAIL_BAD_ACCURACY;
90 Mat points1 = pca.backProject( prjPoints );
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
94 ts->printf( CvTS::LOG, "bad accuracy of backProject()" );
95 code = CvTS::FAIL_BAD_ACCURACY;
98 ts->set_failed_test_info( code );
\r