/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10//11// Copyright (C) 2000, Intel Corporation, all rights reserved.12// Third party copyrights are property of their respective owners.13//14// Redistribution and use in source and binary forms, with or without modification,15// are permitted provided that the following conditions are met:16//17// * Redistribution's of source code must retain the above copyright notice,18// this list of conditions and the following disclaimer.19//20// * Redistribution's in binary form must reproduce the above copyright notice,21// this list of conditions and the following disclaimer in the documentation22// and/or other materials provided with the distribution.23//24// * The name of Intel Corporation may not be used to endorse or promote products25// derived from this software without specific prior written permission.26//27// This software is provided by the copyright holders and contributors "as is" and28// any express or implied warranties, including, but not limited to, the implied29// warranties of merchantability and fitness for a particular purpose are disclaimed.30// In no event shall the Intel Corporation or contributors be liable for any direct,31// indirect, incidental, special, exemplary, or consequential damages32// (including, but not limited to, procurement of substitute goods or services;33// loss of use, data, or profits; or business interruption) however caused34// and on any theory of liability, whether in contract, strict liability,35// or tort (including negligence or otherwise) arising in any way out of36// the use of this software, even if advised of the possibility of such damage.37//38//M*/3940#include "precomp.hpp"4142namespace cv { namespace ml {4344struct PairDI45{46double d;47int i;48};4950struct CmpPairDI51{52bool operator ()(const PairDI& e1, const PairDI& e2) const53{54return (e1.d < e2.d) || (e1.d == e2.d && e1.i < e2.i);55}56};5758void createConcentricSpheresTestSet( int num_samples, int num_features, int num_classes,59OutputArray _samples, OutputArray _responses)60{61if( num_samples < 1 )62CV_Error( CV_StsBadArg, "num_samples parameter must be positive" );6364if( num_features < 1 )65CV_Error( CV_StsBadArg, "num_features parameter must be positive" );6667if( num_classes < 1 )68CV_Error( CV_StsBadArg, "num_classes parameter must be positive" );6970int i, cur_class;7172_samples.create( num_samples, num_features, CV_32F );73_responses.create( 1, num_samples, CV_32S );7475Mat responses = _responses.getMat();7677Mat mean = Mat::zeros(1, num_features, CV_32F);78Mat cov = Mat::eye(num_features, num_features, CV_32F);7980// fill the feature values matrix with random numbers drawn from standard normal distribution81randMVNormal( mean, cov, num_samples, _samples );82Mat samples = _samples.getMat();8384// calculate distances from the origin to the samples and put them85// into the sequence along with indices86std::vector<PairDI> dis(samples.rows);8788for( i = 0; i < samples.rows; i++ )89{90PairDI& elem = dis[i];91elem.i = i;92elem.d = norm(samples.row(i), NORM_L2);93}9495std::sort(dis.begin(), dis.end(), CmpPairDI());9697// assign class labels98num_classes = std::min( num_samples, num_classes );99for( i = 0, cur_class = 0; i < num_samples; ++cur_class )100{101int last_idx = num_samples * (cur_class + 1) / num_classes - 1;102double max_dst = dis[last_idx].d;103max_dst = std::max( max_dst, dis[i].d );104105for( ; i < num_samples && dis[i].d <= max_dst; ++i )106responses.at<int>(dis[i].i) = cur_class;107}108}109110}}111112/* End of file. */113114115