int main (int argc, char* argv[]){ ClassificationDataset dataset; dataset.setName("PN"); dataset.addClass("positive"); dataset.addClass("negative"); RNG random; for(int i=0;i<100;i++){ Mat tempMat(1,1,CV_64FC1,1.0); random.next(); vector<realv> meanValue = vector<realv>(0.0,1); vector<realv> stdValue = vector<realv>(1.0,1); random.fill(tempMat,RNG::NORMAL,meanValue,stdValue); // random.fill(tempMat,RNG::NORMAL,0.0,1.0); FeatureVector tempVec(tempMat); if(tempVec[0]>0){ dataset.addSequence(tempVec, "positive"); } else{ dataset.addSequence(tempVec, "negative"); } } // cout << dataset; cout << dataset.getMean(); cout << dataset.getStandardDeviation(); dataset.save("../xml/pn.xml"); return EXIT_SUCCESS; }
void spm_bp::RandomAssignRepresentativePixel(const vector<vector<int> >& spPixelsList, int numOfLabels, vector<int>& rePixel) { rePixel.resize(numOfLabels); RNG rng; int iy; for (iy = 0; iy < numOfLabels; ++iy) { rePixel[iy] = spPixelsList[iy][rng.next() % spPixelsList[iy].size()]; } }
void CV_KMeansTest::run( int /*start_from*/ ) { const int iters = 100; int sizesArr[] = { 5000, 7000, 8000 }; int pointsCount = sizesArr[0]+ sizesArr[1] + sizesArr[2]; Mat data( pointsCount, 2, CV_32FC1 ), labels; vector<int> sizes( sizesArr, sizesArr + sizeof(sizesArr) / sizeof(sizesArr[0]) ); vector<Mat> means, covs; defaultDistribs( means, covs ); generateData( data, labels, sizes, means, covs, CV_32SC1 ); int code = cvtest::TS::OK; Mat bestLabels; // 1. flag==KMEANS_PP_CENTERS kmeans( data, 3, bestLabels, TermCriteria( TermCriteria::COUNT, iters, 0.0), 0, KMEANS_PP_CENTERS, noArray() ); if( calcErr( bestLabels, labels, sizes, false ) > 0.01f ) { ts->printf( cvtest::TS::LOG, "bad accuracy if flag==KMEANS_PP_CENTERS" ); code = cvtest::TS::FAIL_BAD_ACCURACY; } // 2. flag==KMEANS_RANDOM_CENTERS kmeans( data, 3, bestLabels, TermCriteria( TermCriteria::COUNT, iters, 0.0), 0, KMEANS_RANDOM_CENTERS, noArray() ); if( calcErr( bestLabels, labels, sizes, false ) > 0.01f ) { ts->printf( cvtest::TS::LOG, "bad accuracy if flag==KMEANS_PP_CENTERS" ); code = cvtest::TS::FAIL_BAD_ACCURACY; } // 3. flag==KMEANS_USE_INITIAL_LABELS labels.copyTo( bestLabels ); RNG rng; for( int i = 0; i < 0.5f * pointsCount; i++ ) bestLabels.at<int>( rng.next() % pointsCount, 0 ) = rng.next() % 3; kmeans( data, 3, bestLabels, TermCriteria( TermCriteria::COUNT, iters, 0.0), 0, KMEANS_USE_INITIAL_LABELS, noArray() ); if( calcErr( bestLabels, labels, sizes, false ) > 0.01f ) { ts->printf( cvtest::TS::LOG, "bad accuracy if flag==KMEANS_PP_CENTERS" ); code = cvtest::TS::FAIL_BAD_ACCURACY; } ts->set_failed_test_info( code ); }
//================================== // Update CodeTree //================================== void SpineDecoder::expand_wavefront(vector<int> sym_spine){ /* 变量的定义和初始化是按照变量的用途分类的 */ int edge_metric = 0; int distance = 0; RNG rng; SymbolMap mapper; uint8_t node_symbol = 0; vector<CodeTreeNode> wavefront = this->wavefront; vector<CodeTreeNode> new_wavefront; /* 数组 wavefront 中的每一个元素对应 spine_t 的一个节点 */ vector<CodeTreeNode>::iterator iwave = wavefront.begin(); /* 创建树的过程是一层一层的创建,使用 vector 添加或删除节点时,实现起来比较方便 */ while(iwave != wavefront.end()){ for(int edge=0; edge!=(1<<(this->k)); ++edge){//edge 也就是 mt,这个循环用来遍历可能出现的 k-bit m. CodeTreeNode new_node; /* 1. update spine_value */ new_node.spine_value = hash_func(iwave->spine_value, edge); rng = RNG(new_node.spine_value); for(int i=0; i!=this->L; ++i){ node_symbol = mapper.map_func(rng.next()); distance = sym_spine[i] - node_symbol; edge_metric = distance * distance; }//end for /* 2.update path_metric */ new_node.path_metric = iwave->path_metric + edge_metric; /* 3.update path */ new_node.path = iwave->path; new_node.path.push_back(edge); /* Finaly, update wavefront */ new_wavefront.push_back(new_node); }//end for ++iwave; }//end while this->wavefront = new_wavefront; if(!wavefront.empty()) wavefront.clear(); if(!new_wavefront.empty()) new_wavefront.clear(); }
int NearestNeighborTest::checkFind( const Mat& data ) { int code = CvTS::OK; int pointsCount = 1000; float noise = 0.2f; RNG rng; Mat points( pointsCount, dims, CV_32FC1 ); Mat results( pointsCount, K, CV_32SC1 ); std::vector<int> fmap( pointsCount ); for( int pi = 0; pi < pointsCount; pi++ ) { int fi = rng.next() % featuresCount; fmap[pi] = fi; for( int d = 0; d < dims; d++ ) points.at<float>(pi, d) = data.at<float>(fi, d) + rng.uniform(0.0f, 1.0f) * noise; } code = findNeighbors( points, results ); if( code == CvTS::OK ) { int correctMatches = 0; for( int pi = 0; pi < pointsCount; pi++ ) { if( fmap[pi] == results.at<int>(pi, 0) ) correctMatches++; } double correctPerc = correctMatches / (double)pointsCount; if (correctPerc < .75) { ts->printf( CvTS::LOG, "correct_perc = %d\n", correctPerc ); code = CvTS::FAIL_BAD_ACCURACY; } } return code; }