From: mdim Date: Thu, 3 Dec 2009 15:13:11 +0000 (+0000) Subject: added PCA test X-Git-Url: http://rtime.felk.cvut.cz/gitweb/opencv.git/commitdiff_plain/f15872cf946e11c36e987ebca78180570ca08811 added PCA test git-svn-id: https://code.ros.org/svn/opencv/trunk@2354 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08 --- diff --git a/opencv/tests/cxcore/src/apca.cpp b/opencv/tests/cxcore/src/apca.cpp new file mode 100644 index 00000000..0282d1bd --- /dev/null +++ b/opencv/tests/cxcore/src/apca.cpp @@ -0,0 +1,101 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// Intel License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "cxcoretest.h" + +using namespace cv; + + +class CV_PCATest : public CvTest +{ +public: + CV_PCATest() : CvTest( "pca", "PCA funcs" ) {} +protected: + void run( int); +}; + +void CV_PCATest::run( int ) +{ + int code = CvTS::OK, err; + int maxComponents = 1; + Mat points( 1000, 3, CV_32FC1); + randn( points, Scalar::all(0.0), Scalar::all(1.0) ); + float mp[] = { 3.0f, 3.0f, 3.0f }, cp[] = { 0.5f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.3f }; + Mat mean( 1, 3, CV_32FC1, mp ), + cov( 3, 3, CV_32FC1, cp ); + for( int i = 0; i < points.rows; i++ ) + { + Mat r(1, points.cols, CV_32FC1, points.ptr(i)); + r = r * cov + mean; + } + + PCA pca( points, Mat(), CV_PCA_DATA_AS_ROW, maxComponents ); + + // check project + Mat prjPoints = pca.project( points ); + err = 0; + for( int i = 0; i < prjPoints.rows; i++ ) + { + float val = prjPoints.at(i,0); + if( val > 3.0f || val < -3.0f ) + err++; + } + if( (float)err > prjPoints.rows*0.01f ) + { + ts->printf( CvTS::LOG, "bad accuracy of project()" ); + code = CvTS::FAIL_BAD_ACCURACY; + } + + // check backProject + Mat points1 = pca.backProject( prjPoints ); + err = 0; + for( int i = 0; i < points.rows; i++ ) { if( fabs(points1.at(i,0) - mean.at(0,0)) > 0.05 || fabs(points1.at(i,1) - points.at(i,1)) > 0.02 || fabs(points1.at(i,2) - mean.at(0,2)) > 0.05 ) err++; } if( (float)err > prjPoints.rows*0.04f ) + { + ts->printf( CvTS::LOG, "bad accuracy of backProject()" ); + code = CvTS::FAIL_BAD_ACCURACY; + } + + ts->set_failed_test_info( code ); +} + +CV_PCATest pca_test; \ No newline at end of file diff --git a/opencv/tests/ml/src/aemknearestkmeans.cpp b/opencv/tests/ml/src/aemknearestkmeans.cpp index 1a3fee3d..3b90e5ea 100644 --- a/opencv/tests/ml/src/aemknearestkmeans.cpp +++ b/opencv/tests/ml/src/aemknearestkmeans.cpp @@ -46,16 +46,16 @@ using namespace cv; void defaultDistribs( vector& means, vector& covs ) { - float mp0[] = {0.0f, 0.0f}, ep0[] = {0.67f, 0.0f, 0.0f, 0.67f}; - float mp1[] = {5.0f, 0.0f}, ep1[] = {1.0f, 0.0f, 0.0f, 1.0f}; - float mp2[] = {1.0f, 5.0f}, ep2[] = {1.0f, 0.0f, 0.0f, 1.0f}; - Mat m0( 1, 2, CV_32FC1, mp0 ), e0( 2, 2, CV_32FC1, ep0 ); - Mat m1( 1, 2, CV_32FC1, mp1 ), e1( 2, 2, CV_32FC1, ep1 ); - Mat m2( 1, 2, CV_32FC1, mp2 ), e2( 2, 2, CV_32FC1, ep2 ); + float mp0[] = {0.0f, 0.0f}, cp0[] = {0.67f, 0.0f, 0.0f, 0.67f}; + float mp1[] = {5.0f, 0.0f}, cp1[] = {1.0f, 0.0f, 0.0f, 1.0f}; + float mp2[] = {1.0f, 5.0f}, cp2[] = {1.0f, 0.0f, 0.0f, 1.0f}; + Mat m0( 1, 2, CV_32FC1, mp0 ), c0( 2, 2, CV_32FC1, cp0 ); + Mat m1( 1, 2, CV_32FC1, mp1 ), c1( 2, 2, CV_32FC1, cp1 ); + Mat m2( 1, 2, CV_32FC1, mp2 ), c2( 2, 2, CV_32FC1, cp2 ); means.resize(3), covs.resize(3); - m0.copyTo(means[0]), e0.copyTo(covs[0]); - m1.copyTo(means[1]), e1.copyTo(covs[1]); - m2.copyTo(means[2]), e2.copyTo(covs[2]); + m0.copyTo(means[0]), c0.copyTo(covs[0]); + m1.copyTo(means[1]), c1.copyTo(covs[1]); + m2.copyTo(means[2]), c2.copyTo(covs[2]); } // generate points sets by normal distributions