qa_randomtree.cpp
00001 00002 /*************************************************************************** 00003 * qa_randomtree.h - QA for random tree 00004 * 00005 * Generated: Mon Apr 21 11:49:34 2008 00006 * Copyright 2008 Vaishak Belle 00007 * 00008 *****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 /// @cond QA 00025 #include <string> 00026 #include <opencv/cv.h> 00027 #include <opencv/highgui.h> 00028 #include <fvutils/recognition/forest/forest.h> 00029 #include <fvutils/recognition/forest/forest_aux.h> 00030 #include <fvutils/recognition/forest/forest_param_default.h> 00031 00032 //the main functions 00033 00034 00035 00036 00037 00038 // class MergeWindowClass : public UserDef::WindowClass { 00039 00040 // private: 00041 // bool considered ; 00042 00043 // public: 00044 // void setAsConsidered() { considered = true; } 00045 00046 // bool isConsidered() const { return considered; } 00047 00048 // MergeWindowClass( int h, int w, int H, int W, double p ) : WindowClass( h, w, H, W, p), 00049 // considered(false) { } 00050 // }; 00051 00052 00053 // typedef struct ObjectPos 00054 // { 00055 // float x; 00056 // float y; 00057 // float width; 00058 // int found; /* for reference */ 00059 // int neighbors; 00060 // } ObjectPos; 00061 00062 00063 00064 //int Forest::Tree::TreeLevel = 0; 00065 00066 // using Forest::Tree; 00067 // using Forest::Test; 00068 // using Forest::propagateTree; 00069 00070 00071 // /* Unit Testing module */ 00072 00073 00074 // /* _working_ */ 00075 // void _tTest(UserDef::ConfigClass &config) { 00076 00077 // using Forest::Test; 00078 00079 // Test *sampleTest; 00080 // sampleTest = new Test( 8, 9, config ); 00081 00082 // sampleTest->runTestsOnIntegralImages( config.integralImages, 00083 // 0, 00084 // config ); 00085 00086 00087 // delete sampleTest; 00088 00089 // } 00090 00091 00092 /* _working_ */ 00093 // void _tTree( UserDef::ConfigClass &config ) { 00094 00095 // using Forest::Tree; 00096 // int globalNodeIndex = 0; 00097 00098 // Tree* aTree = new Tree( config, 00099 // globalNodeIndex, 00100 // 1 ); 00101 00102 00103 // delete aTree; 00104 00105 00106 // } 00107 00108 00109 // /* _not_checked_ */ 00110 // void dealloc_mem( Forest::Tree* root ) { 00111 00112 00113 // ERR_MODULE("dealloc_mem"); 00114 // exit(1); 00115 00116 // /* 00117 // if( root == 0 ) 00118 // return ; 00119 00120 // Test* test = root->bestTest; 00121 00122 // if( test == 0 ) 00123 // return; 00124 00125 // for( int i = 0 ; i < NCLASSES ; i++ ) 00126 // test->leftBranch[i].iiVector.clear(); 00127 00128 // for( int i = 0 ; i < NCLASSES ; i++ ) 00129 // test->rightBranch[i].iiVector.clear(); 00130 00131 // Tree* leftTree = root->leftTree; 00132 // Tree* rightTree = root->rightTree; 00133 00134 // if( leftTree != 0 ) 00135 // dealloc_mem( leftTree ); 00136 00137 // if( rightTree != 0 ) 00138 // dealloc_mem( rightTree ); 00139 // */ 00140 00141 // } 00142 00143 00144 // /* _working_ */ 00145 // int writeToFile(std::ostream& out, Forest::Test& test){ 00146 // out<<test.theta1<<endl; 00147 // out<<test.theta2<<endl; 00148 // out<<test.xdash<<endl; 00149 // out<<test.ydash<<endl; 00150 // out<<test.xMain<<endl; 00151 // out<<test.yMain<<endl; 00152 // out<<test.haarFeature<<endl; 00153 // return 0; 00154 // } 00155 00156 // /* _working_ */ 00157 // int readFromFile(std::ifstream& in, Forest::Test& test){ 00158 // in>>test.theta1; 00159 // in>>test.theta2; 00160 // in>>test.xdash; 00161 // in>>test.ydash; 00162 // in>>test.xMain; 00163 // in>>test.yMain; 00164 // in>>test.haarFeature; 00165 // return 0; 00166 // } 00167 00168 00169 00170 00171 /***************************************** 00172 00173 writeToFile 00174 00175 ****************************************/ 00176 00177 // int writeToFile( std::ostream& out, std::ostream& testOut, Forest::Tree& tree ){ 00178 00179 // /* The write to file is as per the following simple rules: 00180 // * 1. If it is a null node, i write -1 00181 // * 2. If the node is a leaf node, then I write to the traintree.dat file, -2 times nodeIndex - 00182 // * this is because when I read back index, if they happen to below 0 && !=-1, I can distinguish leaf nodes 00183 // * 3. If the node is not a null node and not a leaf node, i write down the nodeIndex as is. 00184 // */ 00185 00186 // if(tree.bestTest == 0){ 00187 // // If this is indeed a leaf node, we need theIntegralImages[0,1].iiVector.size()'s. So, what we do is write -2*nodeIndex to the file. While reading, if it encounters <0 && !=-1, then we ask it to read the sizes instead of the normal test instance. See the corresponding readFromFile for more implementation details. 00188 00189 // out<<-2*tree.nodeIndex_<<endl; // this is a leaf node - no best test 00190 // cout<<tree.theIntegralImages[0].iiVector.size()<<" "<<tree.theIntegralImages[1].iiVector.size()<<" "; 00191 00192 // out<<tree.theIntegralImages[0].iiVector.size()<<endl; 00193 // out<<tree.theIntegralImages[1].iiVector.size()<<endl; 00194 // } 00195 00196 // else { 00197 00198 // out<<tree.nodeIndex_<<endl; 00199 // writeToFile(testOut, *(tree.bestTest)); 00200 // } 00201 00202 // if(tree.leftTree!=0) 00203 // writeToFile(out, testOut, *(tree.leftTree)); 00204 00205 // else 00206 // out<<-1<<endl; 00207 00208 // if(tree.rightTree!=0) 00209 // writeToFile(out, testOut, *(tree.rightTree)); 00210 00211 // else 00212 // out<<-1<<endl; 00213 // } 00214 00215 00216 00217 /***************************************** 00218 00219 readFromFile 00220 00221 ****************************************/ 00222 00223 // int readFromFile(std::ifstream& in, std::ifstream& testIn, Forest::Tree** tree){ 00224 00225 // int nodeIndex; 00226 00227 // // Read node index 00228 // in>>nodeIndex; 00229 00230 // // Check if it supposed to be a null node (not a leaf node but a null node) 00231 // if(nodeIndex == -1) 00232 // *tree = 0; 00233 00234 // if(nodeIndex<0 && nodeIndex!=-1){ 00235 00236 // nodeIndex /=-2; 00237 00238 // UserDef::VectorOfIntegralImages* integralImages = new UserDef::VectorOfIntegralImages[2]; 00239 00240 // integralImages[0].iiVector.clear(); 00241 // integralImages[1].iiVector.clear(); 00242 00243 // if(*tree==0) { 00244 00245 // *tree = new Forest::Tree(integralImages,0,0,nodeIndex,0); 00246 // // (*tree)->leftTree = (*tree)->rightTree = 0; 00247 // // (*tree)->bestTest = 0; 00248 // } 00249 00250 // else { 00251 00252 // //(*tree)->theIntegralImages = new UserDef::VectorOfIntegralImages[NCLASSES]; 00253 00254 // for(int i=0;i<NCLASSES;i++) 00255 // (*tree)->theIntegralImages[i].iiVector.assign( 00256 // integralImages[i].iiVector.begin(), 00257 // integralImages[i].iiVector.end()); 00258 // } 00259 00260 // (*tree)->nodeIndex_ = nodeIndex; 00261 00262 // int size1=0, size2=0; 00263 00264 // in>>size1; 00265 // in>>size2; 00266 00267 // (*tree)->theIntegralImages[0].iiVector.resize(size1,0); 00268 // (*tree)->theIntegralImages[1].iiVector.resize(size2,0); 00269 00270 // readFromFile(in, testIn, &((*tree)->leftTree)); // this will most definitely be a nullnode 00271 // readFromFile(in, testIn, &((*tree)->rightTree));// So Will this 00272 // } 00273 00274 // else{ 00275 00276 // if(*tree == 0){ 00277 00278 // // UserDef::VectorOfIntegralImages* integralImages = new UserDef::VectorOfIntegralImages[2]; 00279 // // integralImages[0].iiVector.clear(); 00280 // // integralImages[1].iiVector.clear(); 00281 00282 // *tree = new Forest::Tree(0,0,0,nodeIndex,0); 00283 00284 // // (*tree)->leftTree = (*tree)->rightTree = 0; 00285 // // (*tree)->theIntegralImages = 0; 00286 // // (*tree)->bestTest = 0; 00287 // } 00288 00289 // else 00290 // (*tree)->nodeIndex_ = nodeIndex; 00291 00292 // // Alloc mem for test 00293 // (*tree)->bestTest = new Forest::Test(); 00294 00295 // //read test instance 00296 // readFromFile(testIn,*((*tree)->bestTest)); 00297 00298 // //Repeat procedure for left and right tree 00299 // readFromFile(in, testIn, &((*tree)->leftTree)); 00300 // readFromFile(in, testIn, &((*tree)->rightTree)); 00301 00302 // } 00303 // } 00304 00305 00306 00307 00308 /***************************************** 00309 00310 initImages 00311 00312 ****************************************/ 00313 00314 // void initImages( UserDef::ConfigClass& config, char* trainImages, bool trainLocGiven ){ 00315 00316 00317 00318 // string* s = new string( trainImages ); 00319 00320 // if( trainLocGiven) 00321 // config.setTrainImages( *s ); 00322 00323 // else 00324 // config.setTrainImages(); 00325 00326 // IplImage* tempImage = 0; 00327 // DIR* dir = 0; 00328 00329 // // Holds file name for reading images. 00330 // char itoaString[20]; 00331 00332 // int i = 0, j = 0; 00333 00334 // // Set to false if loaded file is not an image. 00335 // bool flagNotImage = false; 00336 00337 // struct dirent *ent = 0; 00338 // string mainDirPath = config.getTrainImages(); 00339 // string fileName , dirPath; 00340 00341 00342 // /** 00343 // * The object images will be populated with images from each 00344 // * directory specified 00345 // * for a class across all classes. ie. 00346 // * images[6] is the vector of IplImages 00347 // * specified for the class 6. 00348 // */ 00349 // for(i = 0;i<NCLASSES;i++){ 00350 // j = 0; 00351 00352 00353 // sprintf(itoaString,"%d",i); 00354 // dirPath = mainDirPath + itoaString; 00355 00356 // if((dir = opendir(dirPath.c_str())) == NULL){ 00357 00358 // perror("Directory does not exist"); 00359 // exit(0); 00360 // } 00361 00362 // bool setBorders = false; 00363 00364 // while((ent = readdir(dir))!=NULL){ 00365 00366 // fileName = dirPath + "/" + ent->d_name; 00367 00368 // tempImage = cvLoadImage(fileName.c_str()); 00369 00370 // if(!tempImage){ 00371 00372 // flagNotImage = true; 00373 // } 00374 00375 // if( !flagNotImage ) { 00376 00377 // j++; 00378 00379 // // config.images[i].push_back(tempImage); 00380 00381 // config.imageInfoInstance.setHeight( tempImage->height ); 00382 // config.imageInfoInstance.setWidth( tempImage->width ); 00383 00384 // int *tempII = new int[ ( tempImage->height ) * ( tempImage->width ) ]; 00385 // UserDef::calculateIntegralImage(tempImage, tempII); 00386 // config.integralImages[i].iiVector.push_back( tempII ); 00387 00388 00389 // if( !setBorders ) { 00390 // config.integralImages[i].setHeight( tempImage->height ); 00391 // config.integralImages[i].setWidth( tempImage->width ); 00392 // setBorders = true; 00393 // } 00394 00395 // //#OK# cout<<j<<" "<<fileName.c_str()<<endl; 00396 00397 // cvReleaseImage( &tempImage ); 00398 // } // if( !flag.. 00399 00400 // flagNotImage = false; 00401 // } // while(.. 00402 // } // for( i.. 00403 00404 // //Write to file 00405 // config.setTrainIntegralImagesLoc(TRAINDT); 00406 00407 // // Replicate the integral Images 00408 // // UserDef::VectorOfIntegralImages vectorOfIntegralImages[ NCLASSES ]; 00409 // // for( int i = 0 ; i < NCLASSES ; i++ ) 00410 // // for( int j = 0; j < config.integralImages[i].size() ; j++ ) 00411 // // vectorOfIntegralImages[i].iiVector.push_back( &( config.integralImages[i].iiVector.at(j) ) ); 00412 00413 // Auxillary::writeToFile(config.integralImages , 00414 // config.getTrainIntegralImagesLoc(), 00415 // config.imageInfoInstance.getHeight(), 00416 // config.imageInfoInstance.getWidth()); 00417 00418 // dir = 0; 00419 // ent = 0; 00420 00421 00422 // } 00423 00424 00425 /***************************************** 00426 00427 initTestImages 00428 00429 ****************************************/ 00430 // void initTestImages(UserDef::ConfigClass &config){ 00431 00432 // config.setTestImages(); 00433 00434 // IplImage* tempImage = 0; 00435 // DIR* dir = 0; 00436 // char itoaString[20]; 00437 00438 // int i =0, j=0; 00439 00440 // bool flagNotImage = false; 00441 00442 // struct dirent *ent = 0; 00443 00444 // string mainDirPath = config.getTestImages(); 00445 00446 // string fileName, dirPath; 00447 // int *tempII = 0; 00448 00449 // for(i = 0;i<NCLASSES;i++){ 00450 00451 // j = 0; 00452 // sprintf(itoaString,"%d",i); 00453 // dirPath = mainDirPath + itoaString; 00454 00455 // if((dir = opendir(dirPath.c_str())) == NULL){ 00456 00457 // perror("Directory does not exist"); 00458 // exit(0); 00459 // } 00460 00461 00462 // while((ent = readdir(dir))!=NULL){ 00463 // fileName = dirPath + "/" + ent->d_name; 00464 00465 // tempImage = cvLoadImage(fileName.c_str()); 00466 00467 // if(!tempImage){ 00468 00469 // flagNotImage = true; 00470 // } 00471 00472 // if(!flagNotImage){ 00473 00474 // j++; 00475 // // config.testImages[i].push_back(tempImage); 00476 // int height = tempImage->height; 00477 // int width = tempImage->width; 00478 00479 // tempII = new int[height*width]; 00480 // calculateIntegralImage(tempImage,tempII); 00481 00482 // config.testIntegralImages[i].iiVector.push_back( tempII ); 00483 00484 // cvReleaseImage( &tempImage ); 00485 // } 00486 00487 // flagNotImage = false; 00488 // } 00489 // } 00490 00491 // // Write down contents in a file 00492 // config.setTestIntegralImagesLoc(TESTDT); 00493 00494 // // UserDef::VectorOfIntegralImages vectorOfIntegralImages[ NCLASSES ]; 00495 00496 // // for( int i = 0 ; i < NCLASSES ; i++ ) 00497 // // for( int j = 0 ; j < config.testIntegralImages[i].size() ; j++ ) 00498 // // vectorOfIntegralImages[i].iiVector.push_back( &( config.testIntegralImages[i].iiVector.at(j) ) ); 00499 00500 // Auxillary::writeToFile( config.testIntegralImages , 00501 // config.getTestIntegralImageDataLoc(), 00502 // config.imageInfoInstance.getHeight(), 00503 // config.imageInfoInstance.getWidth()); 00504 00505 // tempImage = 0; 00506 // dir = 0; 00507 // ent = 0; 00508 // tempII = 0; 00509 00510 // } 00511 00512 00513 00514 /***************************************** 00515 00516 00517 runclassification 00518 00519 00520 ****************************************/ 00521 // void runClassification(Forest::Tree* tree, UserDef::ConfigClass& config){ 00522 00523 // int correctClassified = 0; 00524 // int totalNumberOfImages = 0; 00525 // int classifyResult = -1; 00526 00527 // for(int classIndex = 0;classIndex<NCLASSES; classIndex++) 00528 00529 // for(int imageIndex = 0;imageIndex<config.testIntegralImages[classIndex].iiVector.size();imageIndex++){ 00530 00531 // totalNumberOfImages++; 00532 // double faceProbabilityResult = -1.0; 00533 00534 // classifyResult = tree->getBinOfClassification(tree, 00535 // (config.testIntegralImages[classIndex]. 00536 // iiVector.at(imageIndex)), 00537 // config, faceProbabilityResult); 00538 00539 // if(classifyResult == classIndex) 00540 // correctClassified++; 00541 // } 00542 00543 // cout<<"Correctly classified are"<<correctClassified<<" out of "<<totalNumberOfImages; 00544 // cout<<"The error rate is "<<(1.0 - (double)correctClassified/(totalNumberOfImages*1.0))<<endl; 00545 00546 // } 00547 00548 00549 00550 // // Common module for handling filterNoise1234. 00551 // void filterNoise( vector< UserDef::WindowClass* > &forestWindows, UserDef::ConfigClass &config ) { 00552 00553 00554 // using UserDef::filterNoise1; 00555 // using UserDef::filterNoise2; 00556 // using UserDef::filterNoise3; 00557 // using UserDef::filterNoise4; 00558 // using UserDef::filterNoise5; 00559 00560 // //filterNoise3( forestWindows, config, 1.0 ); 00561 // filterNoise3( forestWindows, config, 1.0 ); 00562 // filterNoise3( forestWindows, config, 1.0 ); 00563 // // filterNoise5( forestWindows, config, 1.0, 1 ); 00564 // // filterNoise5( forestWindows, config, 1.0 ); 00565 // // filterNoise5( forestWindows, config, 1.0 ); 00566 // // filterNoise5( forestWindows, config, 1.0 ); 00567 00568 00569 // } 00570 00571 00572 // void final(UserDef::ConfigClass& config){ 00573 00574 // // IplImage* tempImage = 0; 00575 // for(int i=0;i<config.getNclasses();i++){ 00576 00577 // // while(config.images[i].size()!=0){./Ra 00578 00579 // // tempImage = config.images[i].back(); 00580 // // cvReleaseImage(&tempImage); 00581 // // config.images[i].pop_back(); 00582 // // } 00583 00584 // // for( int j = 0 ; j < config.integralImages[i].size() ; j++ ) 00585 // // if( config.integralImages[i].iiVector.at(j) != 0 ) 00586 // // delete[] config.integralImages[i].iiVector.at( j ); 00587 00588 00589 // config.integralImages[i].iiVector.clear(); 00590 // } 00591 00592 // // delete[] config.integralImages; 00593 00594 // for(int i=0;i<config.getNclasses();i++){ 00595 00596 // // while(config.testImages[i].size()!=0){ 00597 // // tempImage = config.testImages[i].back(); 00598 // // cvReleaseImage(&tempImage); 00599 // // config.testImages[i].pop_back(); 00600 // // } 00601 00602 // // for( int j = 0 ; j < config.testIntegralImages[i].size() ; j++ ) 00603 // // if( config.testIntegralImages[i].iiVector.at(j) != 0 ) 00604 // // delete[] config.testIntegralImages[i].iiVector.at(j); 00605 00606 // config.testIntegralImages[i].iiVector.clear(); 00607 00608 // } 00609 00610 00611 // UserDef::WindowClass* wc = 0; 00612 // for( unsigned int i = 0 ; i < config.forestWindows.size() ; i++ ) { 00613 // wc = config.forestWindows.at( i ) ; 00614 // delete wc; 00615 // } 00616 00617 // config.forestWindows.clear(); 00618 00619 // for( unsigned int i = 0 ; i < config.windows.size() ; i++ ) { 00620 // wc = config.windows.at( i ) ; 00621 // delete wc; 00622 // } 00623 00624 // config.windows.clear(); 00625 00626 00627 // // delete[] config.testIntegralImages; 00628 00629 // // tempImage = 0; 00630 // } 00631 00632 00633 00634 // /* _working_ ? */ 00635 // /* heuristic to combine votes for sliding window with scaled tests */ 00636 // void combineVotes_2( UserDef::ConfigClass &config, int range) { // this is for vote==2 00637 00638 00639 // /* have to finish up: 00640 // * 1. fix up "range" variable to be rightly scaled 00641 // * so the modified fucntion is 00642 // * com* ( config, range, scaleFactor ) 00643 // * 2. correct vote==2 proc 00644 // */ 00645 00646 // ERR_MODULE( "combinevotes_2" ); 00647 // exit( 1 ); 00648 00649 00650 // // if(config.forestWindows.size()==0) // If the size is 0, put everything in 00651 00652 // // for(int i=0;i<config.windows.size();i++) 00653 00654 // // config.forestWindows.push_back( new UserDef::WindowClass( 00655 // // config.windows.at(i)->getY(), 00656 // // config.windows.at(i)->getX(), 00657 // // config.windows.at(i)->getHeight(), 00658 // // config.windows.at(i)->getWidth(), 00659 // // config.windows.at(i)-> 00660 // // getFaceProbabilityResult() ) ); 00661 00662 // // else { 00663 00664 // // // In case the size is not 0, then we run through the set of detections we have in forestWindows. 00665 // // // If we find something in the neighborhood, then we choose the best Window between 00666 // // // config.windows' one and config.forestWindows' one. 00667 // // // If however, for an entry of config.windows, not a single forestWindows is member, then put 00668 // // // the config.windows' entry to forestWindows 00669 00670 // // using Auxillary::inRange; 00671 00672 // // using UserDef::WindowClass; 00673 00674 00675 00676 // // vector< MergeWindowClass* > original; 00677 // // vector< WindowClass* > temp; 00678 00679 // // for( int i = 0; i < config.forestWindows.size(); i++ ) 00680 00681 // // original.push_back( new MergeWindowClass( config.forestWindows.at(i)->getY(), 00682 // // config.forestWindows.at(i)->getX(), 00683 // // config.forestWindows.at(i)->getHeight(), 00684 // // config.forestWindows.at(i)->getWidth(), 00685 // // config.forestWindows.at(i)-> 00686 // // getFaceProbabilityResult() ) ); 00687 00688 00689 00690 // // for(int i=0;i<config.windows.size();i++) { 00691 00692 // // bool thereIsNeighbor = false; 00693 00694 // // for(int j=0;j<original.size();j++) { 00695 00696 // // if( 00697 // // inRange( config.windows.at(i)->getX(), 00698 // // original.at(j)->getX(), 00699 // // range ) && 00700 // // inRange( config.windows.at(i)->getY(), 00701 // // original.at(j)->getY(), 00702 // // range ) ) { 00703 00704 // // thereIsNeighbor = true; // Now, we choose the best neighbor 00705 00706 // // if( config.windows.at(i)->getFaceProbabilityResult() > 00707 // // original.at(j)->getFaceProbabilityResult() ) { 00708 00709 // // original.at( j )->setAsConsidered(); // Mark the forestWindows entry! 00710 00711 // // temp.push_back( new UserDef::WindowClass( 00712 // // config.windows.at(i)->getY(), 00713 // // config.windows.at(i)->getX(), 00714 // // config.windows.at(i)->getHeight() > 00715 // // original.at(j)->getHeight() ? 00716 // // config.windows.at(i)->getHeight() : 00717 // // original.at(j)->getHeight(), 00718 // // config.windows.at(i)->getWidth() > 00719 // // original.at(j)->getWidth() ? 00720 // // config.windows.at(i)->getWidth() : 00721 // // original.at(j)->getWidth(), 00722 // // config.windows.at(i)->getFaceProbabilityResult() 00723 // // ) ); 00724 00725 // // } 00726 00727 // // else ; // The reference entry stays 00728 00729 // // } // if( inRange.. 00730 // // } // Checked against all forestWindows 00731 00732 00733 // // if( !thereIsNeighbor ) 00734 00735 // // temp.push_back( new UserDef::WindowClass( 00736 // // config.windows.at(i)->getY(), 00737 // // config.windows.at(i)->getX(), 00738 // // config.windows.at(i)->getHeight(), 00739 // // config.windows.at(i)->getWidth(), 00740 // // config.windows.at(i)-> 00741 // // getFaceProbabilityResult() ) ); 00742 00743 00744 // // thereIsNeighbor = false; // Reset 00745 00746 // // } 00747 00748 // // // Set up forestWindows 00749 // // config.forestWindows.clear(); 00750 00751 // // // Copy temp's contents (the good config.windows members) 00752 // // for( int i = 0; i < temp.size(); i++ ) 00753 00754 // // config.forestWindows.push_back( new UserDef::WindowClass( temp.at(i)->getY(), 00755 // // temp.at(i)->getX(), 00756 // // temp.at(i)->getHeight(), 00757 // // temp.at(i)->getWidth(), 00758 // // temp.at(i)->getFaceProbabilityResult() ) ); 00759 00760 // // // Copy the original forestwindows members (those that have not been ousted) 00761 // // for( int i = 0; i < original.size(); i++ ) 00762 00763 // // if( !original.at(i)->isConsidered() ) 00764 00765 // // config.forestWindows.push_back( new UserDef::WindowClass( original.at(i)->getY(), 00766 // // original.at(i)->getX(), 00767 // // original.at(i)->getHeight(), 00768 // // original.at(i)->getWidth(), 00769 // // original.at(i)->getFaceProbabilityResult() 00770 // // ) ); 00771 00772 // // } 00773 00774 // } 00775 00776 00777 /* 00778 * Dummy_combineVotes is a non-heuristic alternative to combineVotes. While combine votes 00779 * checks against past detections to put in only those that are not already there in the 00780 * neighborhood of anything before, dummy_combineVotes blindly inserts every detection 00781 * from every config.windows set 00782 */ 00783 00784 // /* _working_ */ 00785 // void dummy_combineVotes(UserDef::ConfigClass &config, int range, double scaleFactor) { 00786 00787 // scaleFactor = 1.0F/scaleFactor; 00788 00789 00790 // for(unsigned int i = 0 ; i < config.windows.size() ; i++ ) 00791 // config.forestWindows.push_back( new UserDef::WindowClass( (int)( scaleFactor * 00792 // (double)(config.windows.at(i)->getY()) ), 00793 // (int)( scaleFactor * 00794 // (double)(config.windows.at(i)->getX()) ), 00795 // (int)( scaleFactor * 00796 // (double)(config.imageInfoInstance.getHeight()) ), 00797 // (int)( scaleFactor * 00798 // (double)(config.imageInfoInstance.getWidth()) ), 00799 // config.windows.at(i)-> 00800 // getFaceProbabilityResult() ) ); 00801 00802 00803 // } 00804 00805 00806 // /***************************************** 00807 00808 // combineVotes - the old scheme 00809 00810 // ****************************************/ 00811 00812 // // void combineVotes(UserDef::ConfigClass &config , int range, double scaleFactor) { 00813 00814 // // // Since I multiplied all the terms by scaleFactor, when I insert it back , the terms are to be divided. 00815 // // scaleFactor = 1.0F/scaleFactor; 00816 00817 00818 // // if(config.forestWindows.size()==0) // If the size is 0, put everything in 00819 00820 // // for(int i=0;i<config.windows.size();i++) 00821 00822 // // config.forestWindows.push_back( new UserDef::WindowClass( 00823 // // (int)(scaleFactor * 00824 // // config.windows.at(i)->getY()), 00825 // // (int)(scaleFactor * 00826 // // config.windows.at(i)->getX()), 00827 // // (int)(scaleFactor * 00828 // // config.imageInfoInstance.getHeight()), 00829 // // (int)(scaleFactor * 00830 // // config.imageInfoInstance.getWidth()), 00831 // // config.windows.at(i)-> 00832 // // getFaceProbabilityResult() ) ); 00833 00834 // // else { 00835 00836 // // // In case the size is not 0, then we run through the set of detections we have in forestWindows. 00837 // // // If we find something in the neighborhood, then we choose the best Window between 00838 // // // config.windows' one and config.forestWindows' one. 00839 // // // If however, for an entry of config.windows, not a single forestWindows is member, then put 00840 // // // the config.windows' entry to forestWindows 00841 00842 // // using Auxillary::inRange; 00843 00844 // // using UserDef::WindowClass; 00845 00846 // // vector< MergeWindowClass* > original; 00847 // // vector< WindowClass* > temp; 00848 00849 // // for( int i = 0; i < config.forestWindows.size(); i++ ) 00850 00851 // // original.push_back( new MergeWindowClass( config.forestWindows.at(i)->getY(), 00852 // // config.forestWindows.at(i)->getX(), 00853 // // config.forestWindows.at(i)->getHeight(), 00854 // // config.forestWindows.at(i)->getWidth(), 00855 // // config.forestWindows.at(i)-> 00856 // // getFaceProbabilityResult() ) ); 00857 00858 00859 00860 // // for(int i=0;i<config.windows.size();i++) { 00861 00862 // // bool thereIsNeighbor = false; 00863 00864 // // for(int j=0;j<original.size();j++) { 00865 00866 // // if( 00867 // // inRange( (int)(scaleFactor*config.windows.at(i)->getX()), 00868 // // original.at(j)->getX(), 00869 // // range ) && 00870 // // inRange( (int)(scaleFactor*config.windows.at(i)->getY()), 00871 // // original.at(j)->getY(), 00872 // // range ) ) { 00873 00874 // // thereIsNeighbor = true; // Now, we choose the best neighbor 00875 00876 // // if( config.windows.at(i)->getFaceProbabilityResult() > 00877 // // original.at(j)->getFaceProbabilityResult() ) { 00878 00879 // // original.at( j )->setAsConsidered(); // Mark the forestWindows entry! 00880 00881 // // temp.push_back( new UserDef::WindowClass( 00882 // // (int)(scaleFactor * 00883 // // config.windows.at(i)->getY()), 00884 // // (int)(scaleFactor * 00885 // // config.windows.at(i)->getX()), 00886 // // (int)(scaleFactor * 00887 // // config.imageInfoInstance. 00888 // // getHeight()), 00889 // // (int)(scaleFactor * 00890 // // config.imageInfoInstance. 00891 // // getWidth()), 00892 // // config.windows.at(i)-> 00893 // // getFaceProbabilityResult() ) ); 00894 00895 // // } 00896 00897 // // else ; // The reference entry stays 00898 00899 // // } // if( inRange.. 00900 // // } // Checked against all forestWindows 00901 00902 00903 // // if( !thereIsNeighbor ) 00904 // // temp.push_back( new UserDef::WindowClass( 00905 // // (int)(scaleFactor * 00906 // // config.windows.at(i)->getY()), 00907 // // (int)(scaleFactor * 00908 // // config.windows.at(i)->getX()), 00909 // // (int)(scaleFactor * 00910 // // config.imageInfoInstance. 00911 // // getHeight()), 00912 // // (int)(scaleFactor * 00913 // // config.imageInfoInstance. 00914 // // getWidth()), 00915 // // config.windows.at(i)-> 00916 // // getFaceProbabilityResult() ) ); 00917 00918 00919 // // thereIsNeighbor = false; // Reset 00920 00921 // // } 00922 00923 // // // Set up forestWindows 00924 // // config.forestWindows.clear(); 00925 00926 // // // Copy temp's contents (the good config.windows members) 00927 // // for( int i = 0; i < temp.size(); i++ ) 00928 00929 // // config.forestWindows.push_back( new UserDef::WindowClass( temp.at(i)->getY(), 00930 // // temp.at(i)->getX(), 00931 // // temp.at(i)->getHeight(), 00932 // // temp.at(i)->getWidth(), 00933 // // temp.at(i)->getFaceProbabilityResult() ) ); 00934 00935 // // // Copy the original forestwindows members (those that have not been ousted) 00936 // // for( int i = 0; i < original.size(); i++ ) 00937 00938 // // if( !original.at(i)->isConsidered() ) 00939 00940 // // config.forestWindows.push_back( new UserDef::WindowClass( original.at(i)->getY(), 00941 // // original.at(i)->getX(), 00942 // // original.at(i)->getHeight(), 00943 // // original.at(i)->getWidth(), 00944 // // original.at(i)->getFaceProbabilityResult() 00945 // // ) ); 00946 00947 // // } 00948 00949 00950 // // } 00951 00952 00953 00954 // /***************************************** 00955 00956 // comparePerformance 00957 00958 // ****************************************/ 00959 00960 // // void comparePerformance(UserDef::ConfigClass &config, ifstream &in, int range) { 00961 00962 // // vector<int> x; 00963 // // vector<int> y; 00964 00965 // // int tempx, tempy; 00966 // // using Auxillary::inRange; 00967 00968 // // int numberOfDetections = config.forestWindows.size(); 00969 // // int hits = 0; 00970 // // int missed = 0; 00971 // // int numberOfFaces = 0; 00972 00973 00974 // // if(in.is_open()) 00975 00976 // // while(!in.eof()) { 00977 00978 // // in>>tempx; 00979 // // in>>tempy; 00980 // // x.push_back(tempx); 00981 // // y.push_back(tempy); 00982 // // } 00983 00984 // // numberOfFaces = x.size(); 00985 00986 // // if(numberOfDetections == 0) { 00987 00988 // // cout<<"tp: "<<0<<"fp: "<<0<<"missed: "<<x.size()<<endl; return; 00989 // // } 00990 00991 // // for(int i=0;i<config.forestWindows.size();i++) 00992 00993 // // for(int j=0;j<numberOfFaces;j++) 00994 00995 // // if( inRange(config.forestWindows.at(i)->getX(), x.at(j), range) 00996 // // && 00997 // // inRange(config.forestWindows.at(i)->getY(), y.at(j), range) 00998 // // ) { 00999 01000 // // hits++; 01001 01002 // // if(j==0) { 01003 // // x.erase(x.begin()); y.erase(y.begin()); 01004 // // } 01005 01006 // // else { 01007 // // x.erase(x.begin()+j-1, x.begin()+j); y.erase(y.begin()+j-1, y.begin()+j); 01008 // // } 01009 01010 // // break; 01011 // // } 01012 01013 // // cout<<"tpr: "<<hits/numberOfFaces 01014 // // <<" fp: "<<numberOfDetections - numberOfFaces 01015 // // <<" missed: "<<numberOfFaces - hits<<endl; 01016 01017 // // } 01018 01019 01020 01021 // void getForestDetections( UserDef::ConfigClass& config, 01022 // int forestSize, 01023 // vector< Forest::Tree* > &forest, 01024 // int *category, 01025 // int indexOfFaceCategory, 01026 // int dt, 01027 // string nameLocOfImage, 01028 // bool saveDetected, 01029 // IplImage* imageToResize, 01030 // double* scales, 01031 // int number_of_scale_ranges, 01032 // Forest::Tree* aTree, 01033 // std::ofstream& detections_dat 01034 // ) { 01035 01036 01037 // using Forest::Tree; 01038 // using UserDef::WindowClass; 01039 01040 01041 // // By default the individual detections and the final resulting detection are all SAVED 01042 // bool saveIndividualTreeHits = true; 01043 // bool saveFinalForestHits = true; 01044 01045 // if( !saveDetected ) { 01046 // saveIndividualTreeHits = false; 01047 // saveFinalForestHits = false; 01048 // } 01049 01050 // // bool testImageSupplied = false; 01051 // // bool treeIsGrown = false; 01052 01053 // // Forest::Tree* aTree = 0; 01054 // int numberOfTreesGrown = 0 ; 01055 01056 01057 // while( numberOfTreesGrown < forestSize ) { 01058 01059 // // The handling of the performance of a forest will be handleded individually 01060 // // depenending on whether its (a) Classification (b) Sliding window detection 01061 01062 // using UserDef::WindowClass; 01063 // WindowClass* wc = 0 ; 01064 // for( unsigned int i = 0 ; i < config.forestWindows.size() ; i++ ) { 01065 // wc = config.forestWindows.at( i ); 01066 // delete wc; 01067 // } 01068 01069 // config.forestWindows.clear(); 01070 // numberOfTreesGrown++; 01071 01072 01073 // // if( aTree != 0 ) 01074 // // dealloc_mem( aTree ); 01075 01076 01077 01078 // // aTree = new Forest::Tree( config, 01079 // // globalNodeIndex, 01080 // // numberOfTreesGrown++ ); // 1. First grow the tree 01081 01082 01083 01084 // for( int nos_scales = 0 ; nos_scales < number_of_scale_ranges ; nos_scales++ ) { 01085 01086 // UserDef::WindowClass* wc = 0 ; 01087 // for( unsigned int i = 0 ; i < config.windows.size() ; i++ ) { 01088 // wc = config.windows.at( i ) ; 01089 // delete wc; 01090 // } 01091 01092 01093 // config.windows.clear(); 01094 01095 // // string nameLocOfImage; 01096 01097 // // if( testImageSupplied ) 01098 // // nameLocOfImage = testImagePath; 01099 01100 // // else 01101 // // nameLocOfImage = TEST_IMAGE_SLIDING_WINDOW; 01102 01103 // double scaleFactor = scales[ nos_scales ]; 01104 01105 // // IplImage* imageToResize = UserDef::getImageFromLocation(nameLocOfImage); 01106 01107 // config.setDetectionThreshold((int)( scaleFactor * dt ) ); 01108 01109 01110 // /* I am already doing subwindow scaling later on, why am I doing it again here? 01111 01112 // int sw = config.getSubwindowRange(); 01113 // sw = (int)( scaleFactor * sw ); 01114 01115 // if( sw > 4 ) 01116 // config.setSubwindowRange( sw ); 01117 01118 01119 // */ 01120 01121 // bool security_check = false; 01122 01123 // /* If size goes beyond 1400, noticed a problem 01124 // */ 01125 01126 // if( (int)(scaleFactor * imageToResize->height) < 1300 && 01127 // (int)(scaleFactor * imageToResize->width) < 1300 ) { 01128 01129 // if( security_check ) { 01130 01131 // if(config.getDetectionThreshold()==0 || config.getDetectionThreshold()<0) 01132 // config.setDetectionThreshold(1); 01133 01134 // if( 01135 // config.imageInfoInstance.getHeight() > 01136 // (int)( scaleFactor * config.testImageInfoInstance.getHeight() ) || 01137 // config.imageInfoInstance.getWidth() > 01138 // (int)( scaleFactor * config.testImageInfoInstance.getWidth() ) 01139 // ) 01140 // scaleFactor = 1.0; // Is scale too magnified? Then, do not resize test Image 01141 01142 // if( 01143 // (int)( scaleFactor * config.testImageInfoInstance.getHeight() ) == 0 01144 // || 01145 // (int)( scaleFactor * config.testImageInfoInstance.getWidth() ) == 0 01146 // ) 01147 // scaleFactor = 1.0; // Is scale too minute? Then, do not resize test image 01148 01149 01150 // } 01151 01152 // CvSize resizeImageSize = cvSize( (int)(scaleFactor*imageToResize->width), 01153 // (int) (scaleFactor*imageToResize->height) ); 01154 01155 // IplImage* slidingWindowTestIplImage = cvCreateImage( resizeImageSize, 01156 // imageToResize->depth, 01157 // imageToResize->nChannels ); 01158 01159 // cvResize( imageToResize, 01160 // slidingWindowTestIplImage, 01161 // CV_INTER_LINEAR ); 01162 01163 01164 // int* integralImageOfSlidingWindow = new int[( slidingWindowTestIplImage->height) * 01165 // (slidingWindowTestIplImage->width)]; 01166 01167 // using UserDef::calculateIntegralImage; 01168 // calculateIntegralImage(slidingWindowTestIplImage, 01169 // integralImageOfSlidingWindow); 01170 01171 // config.testImageInfoInstance.setHeight(slidingWindowTestIplImage->height); //set new height 01172 // config.testImageInfoInstance.setWidth(slidingWindowTestIplImage->width); //set new widht 01173 01174 // int status; 01175 01176 // status = aTree->slidingWindow( aTree, 01177 // integralImageOfSlidingWindow, 01178 // config, 01179 // category, 01180 // indexOfFaceCategory, 01181 // aTree->getForestIndex(), 01182 // scaleFactor 01183 // ); 01184 01185 01186 01187 // if( status == 0 ) { 01188 01189 // config.globalDetectionsFile<<"\n---------End of Tree: Before Merges------------\n"; 01190 // Auxillary::printConfigWindows( config, config.windows ); 01191 01192 01193 // config.globalDetectionsFile<<"\n---------End of Tree: After Merges------------\n"; 01194 01195 // mergeDetections( config.windows, config , scaleFactor ); 01196 01197 // Auxillary::printConfigWindows( config, config.windows ); 01198 01199 01200 // // if( forestSize > 1 ) { // integrate the results of each tree 01201 01202 // dummy_combineVotes(config, 01203 // NEIGHBOR_RANGE, 01204 // scaleFactor ); 01205 // // } 01206 01207 01208 // if( saveIndividualTreeHits ) { 01209 // char nameAdd[ 10 ] ; 01210 // sprintf( nameAdd , "%d#%d-" , numberOfTreesGrown , nos_scales ); // Unique name 01211 // UserDef::drawDetections( slidingWindowTestIplImage, 01212 // config.windows, 01213 // nameLocOfImage, 01214 // nameAdd, 01215 // saveDetected ); 01216 // } 01217 // } 01218 // cvReleaseImage( &slidingWindowTestIplImage ); 01219 // delete[] integralImageOfSlidingWindow; 01220 01221 // } 01222 01223 // } // for( nos_scales .. 01224 01225 // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-BEFORE TRIM--\n"; 01226 // Auxillary::printConfigWindows( config, config.forestWindows ); 01227 01228 01229 // using UserDef::filterNoise5; 01230 // filterNoise5( config.forestWindows, config, 1.0, 0 ); 01231 01232 01233 01234 01235 // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-AFTER-TRIM--\n"; 01236 // Auxillary::printConfigWindows(config, config.forestWindows); 01237 01238 01239 // if( saveFinalForestHits ) { 01240 01241 // CvSize resizeImageSize = cvSize( imageToResize->width, imageToResize->height ); 01242 01243 // IplImage* slidingWindowTestIplImage = cvCreateImage( resizeImageSize, 01244 // imageToResize->depth, 01245 // imageToResize->nChannels ); 01246 01247 // cvResize( imageToResize, 01248 // slidingWindowTestIplImage, 01249 // CV_INTER_LINEAR ); 01250 01251 01252 // char nameAdd[ 20 ]; 01253 // sprintf( nameAdd, "final_%d-", numberOfTreesGrown ); 01254 01255 // UserDef::drawDetections(slidingWindowTestIplImage, 01256 // config.forestWindows, 01257 // nameLocOfImage, 01258 // nameAdd, 01259 // saveDetected ); 01260 01261 01262 // UserDef::WindowClass* theWindow; 01263 // //Write detections down 01264 // char *buffer = new char[400]; 01265 // char *buffer_name = new char[PATH_MAX + 400]; 01266 // char* temp = new char[20]; 01267 // for( unsigned int i = 0; i<config.forestWindows.size(); i++ ) 01268 // { 01269 // theWindow = config.forestWindows.at(i); 01270 // sprintf( temp, " %d %d %d %d ", theWindow->getX(), theWindow->getY(), theWindow->getWidth(), theWindow->getHeight() ); 01271 01272 // if( i == 0 ) 01273 // strcpy( buffer, temp ); 01274 // else 01275 // strcat( buffer, temp ); 01276 // } 01277 01278 // cout << buffer; 01279 // strcpy(buffer_name, nameLocOfImage.c_str()); 01280 // strcat(buffer_name, buffer); 01281 01282 // detections_dat << buffer_name << endl; // Written 01283 01284 01285 // delete[] buffer; 01286 // delete[] buffer_name; 01287 01288 // cvReleaseImage(&slidingWindowTestIplImage); 01289 01290 // } 01291 // } // while 01292 01293 01294 01295 01296 01297 // // static double scales[] = {1.0, 0.25 , 0.5, 0.8 , 0.3, 0.6 , 0.2, 0.4 , 0.1}; 01298 01299 // // static bool forestGrown = false; 01300 01301 // // int globalNodeIndex = 0; 01302 01303 // // if( !forestGrown ) { 01304 01305 // // for( int i = 0; i < forestSize; i++ ) { 01306 01307 // // Tree* aTree = new Tree( config, 01308 // // globalNodeIndex, 01309 // // i ); 01310 01311 // // forest.push_back( aTree ); 01312 // // globalNodeIndex = 0; 01313 01314 // // } 01315 // // } 01316 01317 // // forestGrown = true; 01318 01319 01320 // // config.forestWindows.clear(); // A set of detections for each image should be collected 01321 01322 // // for( int f = 0; f < forest.size(); f++) { 01323 01324 // // double scaleFactor = scales[ f ]; 01325 // // Tree* aTree = forest.at( f ); 01326 01327 // // config.windows.clear(); 01328 01329 // // config.setDetectionThreshold((int)( scaleFactor * dt ) ); 01330 01331 // // if(config.getDetectionThreshold()==0 || config.getDetectionThreshold()<0) 01332 01333 // // config.setDetectionThreshold(1); 01334 01335 01336 // // CvSize resizeImageSize = cvSize( (int)(scaleFactor*imageToResize->width), 01337 // // (int) (scaleFactor*imageToResize->height) ); 01338 01339 // // IplImage* slidingWindowTestIplImage = cvCreateImage( resizeImageSize, 01340 // // imageToResize->depth, 01341 // // imageToResize->nChannels ); 01342 01343 // // cvResize( imageToResize, 01344 // // slidingWindowTestIplImage, 01345 // // CV_INTER_LINEAR ); 01346 01347 // // // cvReleaseImage( &imageToResize ); 01348 01349 // // int* integralImageOfSlidingWindow = new int[( slidingWindowTestIplImage->height) * 01350 // // (slidingWindowTestIplImage->width)]; 01351 01352 // // calculateIntegralImage(slidingWindowTestIplImage, 01353 // // integralImageOfSlidingWindow); 01354 01355 // // config.testImageInfoInstance.setHeight(slidingWindowTestIplImage->height); //set config details 01356 // // config.testImageInfoInstance.setWidth(slidingWindowTestIplImage->width); //set config details 01357 01358 // // aTree->slidingWindow( aTree, 01359 // // integralImageOfSlidingWindow, 01360 // // config, 01361 // // category, 01362 // // indexOfFaceCategory, 01363 // // aTree->getForestIndex() ); 01364 01365 01366 01367 // // Auxillary::printConfigWindows( config, config.windows ); 01368 // // config.globalDetectionsFile<<"\n---------End of Tree: Before Merges------------\n"; 01369 01370 // // mergeDetections( config.windows, config ); 01371 01372 // // Auxillary::printConfigWindows( config, config.windows ); 01373 // // config.globalDetectionsFile<<"\n---------End of Tree: After Merges------------\n"; 01374 01375 // // if( forest.size() > 1 ) { // integrate the results of each tree 01376 01377 01378 // // combineVotes(config, 01379 // // NEIGHBOR_RANGE, 01380 // // scaleFactor ); 01381 // // } 01382 01383 // // char nameAdd[10]; 01384 // // sprintf(nameAdd,"%d-", f); 01385 01386 // // if( saveIndividualTreeHits ) 01387 // // UserDef::drawDetections( slidingWindowTestIplImage, 01388 // // config.windows, 01389 // // nameLocOfImage, 01390 // // nameAdd, 01391 // // saveIndividualTreeHits ); 01392 01393 // // cvReleaseImage( &slidingWindowTestIplImage ); 01394 01395 // // } 01396 01397 // // if(forestSize>1){ 01398 01399 // // //Draw the final result of all detections 01400 01401 // // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-BEFORE TRIM--\n"; 01402 // // Auxillary::printConfigWindows( config, config.forestWindows ); 01403 01404 // // using UserDef::filterNoise2; 01405 // // filterNoise2( config, config.forestWindows); 01406 01407 // // IplImage* slidingWindowTestIplImage = UserDef::getImageFromLocation(testImagePath); 01408 // // char *nameAdd = "FINAL-"; 01409 01410 // // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-AFTER-TRIM--\n"; 01411 // // Auxillary::printConfigWindows(config, config.forestWindows); 01412 01413 // // if( saveFinalForestHits ) 01414 // // UserDef::drawDetections(slidingWindowTestIplImage, 01415 // // config.forestWindows, 01416 // // nameLocOfImage, 01417 // // nameAdd, 01418 // // saveFinalForestHits); 01419 01420 // // cvReleaseImage(&slidingWindowTestIplImage); 01421 // // } 01422 01423 01424 01425 // } 01426 01427 01428 // void checkClassification( char* infoname ) 01429 // { 01430 01431 // FILE* info; 01432 // char fullname[PATH_MAX]; 01433 // char* filename; 01434 // IplImage* img; 01435 01436 // strcpy( fullname, infoname ); 01437 // filename = strrchr( fullname, '\\' ); 01438 // if( filename == NULL ) 01439 // { 01440 // filename = strrchr( fullname, '/' ); 01441 // } 01442 // if( filename == NULL ) 01443 // { 01444 // filename = fullname; 01445 // } 01446 // else 01447 // { 01448 // filename++; 01449 // } 01450 01451 // info = fopen( infoname, "r" ); 01452 // if( info != NULL ) 01453 // { 01454 01455 // while( !feof( info ) ) 01456 // { 01457 01458 01459 // img = cvLoadImage( fullname ); 01460 01461 // if( !img ) continue; 01462 01463 // cvReleaseImage( &img ); 01464 // } 01465 // } 01466 // } 01467 01468 01469 // void runPerformance(UserDef::ConfigClass& config, char* infoname, 01470 // int *category, 01471 // int indexOfFaceCategory, 01472 // int saveDetected, 01473 // int dt, 01474 // int forestSize, 01475 // float maxPosDiff, 01476 // float maxSizeDiff, 01477 // double* scales, 01478 // int nos_scales, 01479 // bool roc_mode ) { 01480 01481 01482 01483 // ofstream detections_dat; 01484 // detections_dat.open( FINAL_DETECTIONS ); 01485 01486 // /* 01487 // * the roc_mode variable is going to turn off irrelevant and irritating output produced from detection 01488 // * accuracy module 01489 // */ 01490 01491 01492 // int i, j; 01493 01494 // // bool theActualImplementation = true; 01495 01496 // // char* samplesdir = NULL; 01497 01498 // vector< Forest::Tree* > forest; 01499 01500 01501 // // int width = 24; 01502 // // int height = 24; 01503 01504 // // double scale_factor = 1.2; 01505 01506 01507 // FILE* info; 01508 01509 // char fullname[PATH_MAX]; 01510 // // char detfilename[PATH_MAX]; 01511 // char* filename; 01512 // // char detname[] = "det-"; 01513 01514 01515 // CvMemStorage* storage; 01516 // // CvSeq* objects; 01517 01518 // double totalTime; 01519 01520 01521 // Forest::Tree* aTree = 0 ; 01522 // int globalNodeIndex = 0 ; 01523 01524 // // The Tree 01525 // aTree = new Forest::Tree( config , 01526 // globalNodeIndex , 01527 // 1 ); 01528 01529 // if( !roc_mode ) 01530 // cout<<infoname; 01531 01532 // storage = cvCreateMemStorage(); 01533 // strcpy( fullname, infoname ); 01534 01535 // filename = strrchr( fullname, '\\' ); 01536 // if( filename == NULL ) 01537 // { 01538 // filename = strrchr( fullname, '/' ); 01539 // } 01540 // if( filename == NULL ) 01541 // { 01542 // filename = fullname; 01543 // } 01544 // else 01545 // { 01546 // filename++; 01547 // } 01548 01549 // info = fopen( infoname, "r" ); 01550 // totalTime = 0.0; 01551 01552 // if( info != NULL ) 01553 // { 01554 // int x, y, width, height; 01555 // IplImage* img; 01556 // int hits, missed, falseAlarms; 01557 // int totalHits, totalMissed, totalFalseAlarms; 01558 // int found; 01559 // float distance; 01560 01561 // int refcount; 01562 // ObjectPos* ref; 01563 // //int detcount; 01564 // ObjectPos* det; 01565 // int error; 01566 01567 // int* pos; 01568 // int* neg; 01569 01570 // pos = (int*) cvAlloc( forestSize * sizeof( *pos ) ); 01571 // neg = (int*) cvAlloc( forestSize * sizeof( *neg ) ); 01572 // for( i = 0; i < forestSize; i++ ) { pos[i] = neg[i] = 0; } 01573 01574 01575 // if( !roc_mode ) { 01576 // printf( "+================================+======+======+======+\n" ); 01577 // printf( "| File Name | Hits |Missed| False|\n" ); 01578 // printf( "+================================+======+======+======+\n" ); 01579 // } 01580 01581 // totalHits = totalMissed = totalFalseAlarms = 0; 01582 // while( !feof( info ) ) 01583 // { 01584 // if( fscanf( info, "%s %d", filename, &refcount ) != 2 || refcount <= 0 ) break; 01585 01586 // img = cvLoadImage( fullname ); 01587 01588 // ref = (ObjectPos*) cvAlloc( refcount * sizeof( *ref ) ); 01589 // if( !img ) continue; 01590 01591 01592 01593 // for( i = 0; i < refcount; i++ ) 01594 // { 01595 01596 // error = (fscanf( info, "%d %d %d %d", &x, &y, &width, &height ) != 4); 01597 // if( error ) break; 01598 // ref[i].x = 0.5F * width + x; 01599 // ref[i].y = 0.5F * height + y; 01600 // ref[i].width = sqrtf( 0.5F * (width * width + height * height) ); 01601 // ref[i].found = 0; 01602 // ref[i].neighbors = 0; 01603 // } 01604 // if( !error ){ 01605 01606 // cvClearMemStorage( storage ); 01607 01608 01609 // totalTime -= time( 0 ); 01610 // // int* integralImageOfSlidingWindow = new int[(img->height)*(img->width)]; 01611 // // config.testImageInfoInstance.setWidth(img->width); 01612 // // config.testImageInfoInstance.setHeight(img->height); 01613 // // calculateIntegralImage(img, integralImageOfSlidingWindow); 01614 // config.windows.clear(); 01615 01616 // getForestDetections(config, 01617 // forestSize, 01618 // forest, 01619 // category, 01620 // indexOfFaceCategory, 01621 // dt, 01622 // fullname, 01623 // saveDetected, 01624 // img, 01625 // scales, 01626 // nos_scales, 01627 // aTree, 01628 // detections_dat); 01629 01630 // cvReleaseImage( &img ); 01631 01632 // // printf("The forestWindow size is :%d\n", config.forestWindows.size() ); 01633 01634 01635 // // cout<< "RandomTree@runPerformance: Config.forestwindows.size() : " << 01636 // // config.forestWindows.size() << 01637 // // endl; 01638 01639 01640 // // UserDef::WindowClass* wc = 0; 01641 // // for( int i = 0 ; i < config.windows.size() ; i++ ) { 01642 // // wc = config.windows.at( i ); 01643 // // delete wc; 01644 // // } 01645 01646 // // config.windows.clear(); 01647 01648 // // for( int q = 0; q < config.forestWindows.size(); q++ ) 01649 // // config.windows.push_back( config.forestWindows.at( q ) ); 01650 01651 01652 // // config.forestWindows.clear(); 01653 01654 // // cout<< "RandomTree@runPerformance: Config.windows.size() : " << 01655 // // config.windows.size() << 01656 // // endl; 01657 01658 // // aTree->slidingWindow(aTree, integralImageOfSlidingWindow, 01659 // // config, category, indexOfFaceCategory, aTree->getForestIndex()); 01660 01661 // totalTime += time( 0 ); 01662 01663 // det = (ObjectPos*) cvAlloc( config.forestWindows.size() * sizeof( *det ) ); 01664 // hits = missed = falseAlarms = 0; 01665 // for( unsigned int i = 0; i < config.forestWindows.size(); i++ ) { 01666 01667 // det[i].x = 0.5F * config.forestWindows.at(i)->getWidth() + config.forestWindows.at(i)->getX(); 01668 // det[i].y = 0.5F * config.forestWindows.at(i)->getHeight() + config.forestWindows.at(i)->getY(); 01669 // det[i].width = sqrtf( 0.5F * (config.forestWindows.at(i)->getHeight() * 01670 // config.forestWindows.at(i)->getWidth() + 01671 // config.forestWindows.at(i)->getWidth() * 01672 // config.forestWindows.at(i)->getHeight()) ); 01673 // // det[i].neghbors = r.neighbors; 01674 // det[i].neighbors = 0; 01675 01676 // // if(theActualImplementation) 01677 // for( j = 0; j < refcount; j++ ) { 01678 01679 01680 // found = 0; 01681 // distance = sqrtf( (det[i].x - ref[j].x) * (det[i].x - ref[j].x) + 01682 // (det[i].y - ref[j].y) * (det[i].y - ref[j].y) ); 01683 // if( (distance < ref[j].width * maxPosDiff) && 01684 // (det[i].width > ref[j].width / maxSizeDiff) && 01685 // (det[i].width < ref[j].width * maxSizeDiff) ) 01686 // { 01687 // ref[j].found = 1; 01688 // ref[j].neighbors = MAX( ref[j].neighbors, det[i].neighbors ); 01689 // found = 1; 01690 // } 01691 01692 // // } /* Not actual implementation */ 01693 // // else { 01694 // // using Auxillary::inRange; 01695 // // for(j=0;j<refcount;j++) { 01696 // // int rangeCheck = config.getDetectionThreshold(); 01697 // // rangeCheck = 5; 01698 // // if( 01699 // // inRange((int)det[i].x,(int) ref[j].x,rangeCheck) 01700 // // && 01701 // // inRange((int)det[i].y, (int)ref[j].y, rangeCheck) 01702 // // ) 01703 // // { 01704 // // ref[j].found = 1; 01705 // // ref[j].neighbors = MAX( ref[j].neighbors, det[i].neighbors ); 01706 // // found = 1; 01707 // // } 01708 // // } 01709 // // } 01710 01711 // if( !found ) 01712 // { 01713 // falseAlarms++; 01714 // neg[MIN(det[i].neighbors, forestSize - 1)]++; 01715 // } 01716 // } 01717 01718 // } 01719 01720 // // cout<<"RandomTree@runPerformance: falseAlarms"<<falseAlarms<<endl; 01721 // for( j = 0; j < refcount; j++ ) 01722 // { 01723 // if( ref[j].found ) 01724 // { 01725 // hits++; 01726 // pos[MIN(ref[j].neighbors, forestSize - 1)]++; 01727 // } 01728 // else 01729 // { 01730 // missed++; 01731 // } 01732 // } 01733 01734 01735 // totalHits += hits; 01736 // totalMissed += missed; 01737 // totalFalseAlarms += falseAlarms; 01738 01739 // if( !roc_mode) { 01740 // printf( "|%32.32s|%6d|%6d|%6d|\n", filename, hits, missed, falseAlarms ); 01741 // printf( "+--------------------------------+------+------+------+\n" ); 01742 // } 01743 01744 // fflush( stdout ); 01745 01746 01747 01748 // // if( saveDetected ) 01749 // // { 01750 // // strcpy( detfilename, detname ); 01751 // // strcat( detfilename, filename ); 01752 // // strcpy( filename, detfilename ); 01753 // // cvvSaveImage( fullname, img ); 01754 // // } 01755 01756 // if( det ) { cvFree( &det ); det = NULL; } 01757 01758 // UserDef::WindowClass* temp; 01759 // for( unsigned int i = 0 ; i < config.forestWindows.size(); i++ ) { 01760 // temp = config.forestWindows.at( i ) ; 01761 // delete temp; 01762 // } 01763 01764 // config.forestWindows.clear(); 01765 01766 // } /* if( !error ) */ 01767 01768 // // cvReleaseImage( &img ); 01769 // cvFree( &ref ); 01770 // } 01771 01772 01773 01774 01775 // fclose( info ); 01776 01777 01778 // if( !roc_mode ) { 01779 // printf( "|%32.32s|%6d|%6d|%6d|\n", "Total", 01780 // totalHits, totalMissed, totalFalseAlarms ); 01781 // printf( "+================================+======+======+======+\n" ); 01782 // printf( "Total time: %f\n", totalTime ); 01783 // } 01784 01785 // for( i = forestSize - 1; i > 0; i-- ) 01786 // { 01787 // pos[i-1] += pos[i]; 01788 // neg[i-1] += neg[i]; 01789 // } 01790 01791 01792 // if( roc_mode ) { 01793 // /* 01794 // * Viola and Jones (http://www.stat.uchicago.edu/~amit/19CRS/DEA/cascade_face_detection.pdf) defines the total number of false subwindows 01795 // * as 350 million or 350 x 10^6 or 350000000. I am going use this to define the fpr. 01796 // * The tpr is as defined : positive_hits / total_number_of_face 01797 // */ 01798 01799 01800 // /* 01801 // * This is the output according to the true definition of ROC 01802 // */ 01803 // //float tpr = ( 1.0 * totalHits)/ ( 1.0 * ( totalHits + totalMissed ) ); 01804 // // float fpr = ( 1.0 * totalFalseAlarms )/350000000.00; 01805 // // fprintf(stderr,"\n%f %f\n", tpr, fpr ); 01806 01807 01808 // /* 01809 // * ROC results as per viola and jones output method 01810 // */ 01811 01812 // fprintf( stderr,"%f %f\n", 01813 // ((float)totalHits/(totalHits+totalMissed)) , 01814 // ((float)totalFalseAlarms/(totalHits+totalMissed)) ); 01815 01816 // } 01817 01818 // /* print ROC to stdout */ 01819 // // for( i = 0; i < forestSize; i++ ) 01820 // // { 01821 01822 // /* Alternate defintion of ROC */ 01823 // /* 01824 // * this is the roc results for viola and jones. It represents the output for each stage. 01825 // * So, in effect, the algorithm considers the output across different stages. 01826 // * pos[0] gives the number of positives as generated by the first stage. 01827 // * pos[1] gives the same for stage 2, but obviously the total number of positive hits = those by pos[0] + those by pos[1] 01828 // * similarly, neg[1] is the negatives by neg[0] + that by neg[1] 01829 // * Thus, the output below can straightaway be plotted for a ROC curve. 01830 // */ 01831 01832 01833 // // fprintf( stderr, "\t%d\t%d\t%f\t%f\n", pos[i], neg[i], 01834 // // ((float)pos[i]) / (totalHits + totalMissed), 01835 // // ((float)neg[i]) / (totalHits + totalMissed) ); 01836 01837 // // } 01838 01839 // cvFree( &pos ); 01840 // cvFree( &neg ); 01841 // } 01842 01843 01844 01845 // delete aTree; 01846 // cvReleaseMemStorage( &storage ); 01847 // detections_dat.close(); 01848 01849 01850 // } 01851 01852 01853 01854 01855 int main( int argc, char** argv ) { 01856 01857 01858 01859 // std::clock_t start, finish, start_detection, end_detection; // time to complete training and execution 01860 // start = std::clock(); 01861 01862 01863 /* The set of default values */ 01864 bool performance = false; 01865 char infoname[PATH_MAX]; 01866 char identityfile[PATH_MAX]; 01867 bool identityfileSupplied = false; 01868 int forestSize = 0; 01869 bool run_classification = RUN_CLASSIFICATION; 01870 bool relearn = false; 01871 int nclasses = NCLASSES; 01872 bool verbose = true; 01873 char trainImages[PATH_MAX]; 01874 bool trainLocGiven = false; 01875 float maxSizeDiff = 1.5F, maxPosDiff = 0.5F; 01876 double fmp = FACE_MAP_THRESHOLD; 01877 int dt = DETECTION_THRESHOLD; 01878 int vote = 1; 01879 char testImagePath[PATH_MAX]; 01880 bool testImageSupplied = false; 01881 bool saveDetected = true; // Default - all detections are saved 01882 int subwindow_range = SUBWINDOW_RANGE; 01883 // int number_of_scale_ranges = NOS_SCALES; 01884 int save_train = false; 01885 bool test_mode = false; 01886 bool roc_mode = false; // Roc curves mode, output is minimal 01887 01888 01889 01890 01891 if( argc==1 ) { 01892 printf("\nUSAGE:%s [arg] [value]\n -relearn <1(true)/0(false)>\n" 01893 "-data <Location_of_train_Images>\n" 01894 "-trees <number_of_extremely_random_trees>\n\n" 01895 "-fmp <face_map_threshold_double_value>\n" 01896 "-dt <detection_threshold_value>\n" 01897 "-nclasses <number of classes expected>\n" 01898 "-classification <1(true)_0(false)>\n" 01899 "-subwindow_range <range_of_subwindows_to_run_for_sliding_window>\n\n" 01900 "DETECTION SCHEME:\n" 01901 "-vote <1_2_3 Sliding Window Preferences>\n\n" 01902 "-info <location_of_test_image>\n\n" 01903 "RUN TYPE:\n" 01904 "-performance <location_of_marked_images>\n" 01905 "\t -maxSizeDiff <max_size_diff = 1.50000> \n" 01906 "\t -maxPosDiff <max_position_diff = 1.2000> \n\n" 01907 "-roc_mode \t enable roc_mode\n" 01908 "-identity \t <identity_file>\n" 01909 "MISC:\n" 01910 "-voff \t <switch_off_verbose_mode>\n" 01911 "-st \t <save_train> \n" 01912 "-ni \t add parameter if no image to be saved\n\n" , argv[0]); 01913 01914 return 0; 01915 } // if( argc==1 ).. 01916 01917 if( strcmp( argv[1],"-relearn" ) ) { 01918 printf( "Please enter relearn status as 1(true) or 0(false). " ); 01919 return 0; 01920 } 01921 01922 else{ 01923 int relearnStatus = atoi(argv[2]); 01924 if( relearnStatus==1 ) relearn = true; 01925 else relearn = false; 01926 } 01927 01928 if( argc > 2 ){ 01929 01930 for( int i = 3; i < argc; i++ ) { 01931 01932 if( !strcmp( argv[i],"-fmp" ) ) fmp = atof(argv[++i]); 01933 01934 else if( !strcmp( argv[i], "-roc_mode" ) ) roc_mode = true; 01935 01936 else if( !strcmp( argv[i], "-subwindow_range" ) ) subwindow_range = atoi( argv[++i] ); 01937 01938 else if( !strcmp( argv[i],"-dt" ) ) dt = atoi(argv[++i]); 01939 01940 else if( !strcmp( argv[i], "-info" ) ) { 01941 strcpy( testImagePath, argv[++i] ); 01942 testImageSupplied = true; 01943 } 01944 01945 else if( !strcmp( argv[i], "-data" ) ) { 01946 strcpy( trainImages, argv[++i] ); 01947 trainLocGiven = true; 01948 } 01949 01950 else if( !strcmp( argv[i], "-classification" ) ) { 01951 int classification_value = atoi ( argv[++i] ); 01952 if( classification_value == 1 ) run_classification = true; 01953 else run_classification = false; 01954 } 01955 01956 else if( !strcmp( argv[i], "-nclasses" ) ) nclasses = atoi( argv[++i] ); 01957 01958 else if( !strcmp( argv[i], "-st" ) ) save_train = true; 01959 01960 else if( !strcmp( argv[i], "-test" ) ) test_mode = true; 01961 01962 else if( !strcmp( argv[i], "-identity" ) ) { 01963 strcpy( identityfile, argv[++i] ); 01964 identityfileSupplied = true; 01965 } 01966 01967 else if( !strcmp( argv[i], "-voff") ) verbose = false; 01968 01969 else if( !strcmp( argv[i], "-trees" ) ) forestSize = atoi(argv[++i]); 01970 01971 else if( !strcmp( argv[i], "-vote" ) ) vote = atoi(argv[++i]); 01972 01973 else if( !strcmp( argv[i], "-ni" ) ) saveDetected = false; 01974 01975 else if( !strcmp( argv[i], "-maxPosDiff" ) ) maxPosDiff = atof( argv[++i] ); 01976 01977 else if( !strcmp( argv[i], "-maxSizeDiff" ) ) maxSizeDiff = atof( argv[++i] ); 01978 01979 else if( !strcmp( argv[i],"-performance" ) ) { 01980 performance = true; 01981 strcpy( infoname, argv[++i] ); 01982 } 01983 01984 } // for(.. 01985 } // if(argc>2).. 01986 01987 01988 int t_height, t_width; 01989 01990 ForestConfigClass config( nclasses ); 01991 //UserDef::ConfigClass config( nclasses ); 01992 IplImage* imageToClassify = cvLoadImage( testImagePath ); //UserDef::getImageFromLocation( testImagePath ); 01993 if( !imageToClassify ) 01994 { 01995 perror("qa_randomtree.cpp: image not found"); 01996 exit(1); 01997 } 01998 01999 if( forestSize == 0 ) //default forest size 02000 forestSize = _FOREST; 02001 02002 02003 /* 02004 Forest::Tree* bTree; 02005 Forest::Tree* cTree; 02006 02007 growTreeFromImages( trainImages, nclasses, bTree, t_height, t_width, config ); 02008 02009 printf("\n %d is the class label for the image. \n", Forest::returnClassLabelForIplImage( bTree, imageToClassify, t_height, t_width, nclasses ) ); 02010 02011 int _globalNodeIndex = 0; 02012 int _numberOfTrees = 1; 02013 cTree = new Tree( config, _globalNodeIndex, _numberOfTrees ); 02014 printf("\n %d is the class label for the image. \n", Forest::returnClassLabelForIplImage( cTree, imageToClassify, t_height, t_width, nclasses ) ); 02015 02016 */ 02017 02018 // vector< Forest::Tree* > _forest; 02019 02020 02021 02022 //UserDef::ConfigClass config( nclasses ); 02023 ForestClass *theForest = new ForestClass( trainImages, nclasses, t_height, t_width, config, forestSize ); 02024 // IplImage* imageToClassify = UserDef::getImageFromLocation( testImagePath ); 02025 02026 /* for( int i = 0; i < _FOREST ; i++ ) 02027 { 02028 printf("\n %d is the forest class label for the image.\n", Forest::returnClassLabelForIplImage( theForest->getTree(i), imageToClassify, t_height, t_width, nclasses ) ); 02029 } 02030 */ 02031 printf("\n the final label is %d.\n", get_class_label_from_forest( theForest, imageToClassify ) ); 02032 02033 delete theForest; 02034 02035 // printf(Òthe final answer is %dÓ, Forest::getClassLabelFromForest( theForest, imageToClassify ) ); 02036 02037 02038 02039 02040 // config.setSubwindowRange( subwindow_range ); 02041 02042 02043 // if( !RUN_SLIDING_WINDOW && 02044 // testImageSupplied ) 02045 02046 // printf("\nSYSALERT: You have supplied the test image without enabling the detection mode: \n" 02047 // "Please enable the detection mode to use this test image\n"); 02048 02049 // if( relearn ) 02050 // config.setRelearnStatusValue( true ); 02051 // else 02052 // config.setRelearnStatusValue( false ); 02053 02054 // config.setFaceMapThreshold( fmp ); 02055 // config.setDetectionThreshold( dt ); 02056 02057 02058 // int* integralImage; 02059 // int *mem_chunk; 02060 // int *tempII; 02061 02062 02063 // if( config.getRelearnStatusValue() ) { // learn from scratch 02064 02065 02066 // /***************************************** 02067 02068 // init images : Read images from Location and keep the integral Images stored 02069 02070 // ****************************************/ 02071 02072 02073 // string* s = 0; 02074 // s = new string( trainImages ); 02075 02076 // if( trainLocGiven) 02077 // config.setTrainImages( *s ); 02078 02079 // else 02080 // config.setTrainImages(); 02081 02082 // delete s; 02083 02084 // IplImage* tempImage = 0; 02085 // DIR* dir = 0; 02086 02087 // // Holds file name for reading images. 02088 // char itoaString[20]; 02089 02090 // int i = 0, j = 0; 02091 // // Set to false if loaded file is not an image. 02092 // bool flagNotImage = false; 02093 // struct dirent *ent = 0; 02094 02095 // string mainDirPath = config.getTrainImages(); 02096 // string fileName , dirPath; 02097 02098 02099 // /** 02100 // * The object images will be populated with images from each 02101 // * directory specified 02102 // * for a class across all classes. ie. 02103 // * images[6] is the vector of IplImages 02104 // * specified for the class 6. 02105 // */ 02106 // for(i = 0;i<config.getNclasses();i++){ 02107 02108 // j = 0; 02109 02110 // sprintf(itoaString,"%d",i); 02111 02112 // dirPath = mainDirPath + itoaString; 02113 02114 // if((dir = opendir(dirPath.c_str())) == NULL){ 02115 // printf("the directory searched for is %s\n", dirPath.c_str() ); 02116 // perror("Directory does not exist"); 02117 // exit(0); 02118 // } 02119 02120 // bool setBorders = false; 02121 02122 // while((ent = readdir(dir))!=NULL){ 02123 // fileName = dirPath + "/" + ent->d_name; 02124 02125 // tempImage = cvLoadImage(fileName.c_str()); 02126 02127 // if(!tempImage){ 02128 // flagNotImage = true; 02129 // } 02130 02131 // if( !flagNotImage ) { 02132 // j++; 02133 02134 // // config.images[i].push_back(tempImage); 02135 02136 // config.imageInfoInstance.setHeight( tempImage->height ); 02137 // config.imageInfoInstance.setWidth( tempImage->width ); 02138 02139 // tempII = new int[ ( tempImage->height ) * ( tempImage->width ) ]; 02140 02141 // UserDef::calculateIntegralImage( tempImage, tempII ); 02142 02143 // config.integralImages[i].iiVector.push_back( tempII ); 02144 02145 // if( !setBorders ) { 02146 02147 // config.integralImages[i].setHeight( tempImage->height ); 02148 // config.integralImages[i].setWidth( tempImage->width ); 02149 // setBorders = true; 02150 // } 02151 02152 02153 // cvReleaseImage( &tempImage ); 02154 // } // if( !flag.. 02155 02156 // flagNotImage = false; 02157 // } // while(.. 02158 // } // for( i.. 02159 02160 02161 02162 // //Write to file 02163 // config.setTrainIntegralImagesLoc(TRAINDT); 02164 02165 02166 // if( verbose && !roc_mode ) { 02167 02168 02169 02170 // for( int i =0; i < config.getNclasses(); i++) 02171 // printf("Total number of training images: class %d has %zu images\n", i , config.integralImages[i].iiVector.size() ); 02172 02173 // } 02174 02175 // // UserDef::VectorOfIntegralImages vectorOfIntegralImages[ NCLASSES ]; 02176 02177 // // for( int i = 0 ; i < NCLASSES ; i++ ) 02178 // // for( int j = 0 ; j < config.integralImages[i].size() ; j++ ) 02179 // // vectorOfIntegralImages[i].iiVector.push_back( &( config.integralImages[i].iiVector.at(j) ) ); 02180 02181 02182 // if( save_train ) 02183 // Auxillary::writeToFile( config.integralImages, 02184 // config.getTrainIntegralImagesLoc(), 02185 // config.imageInfoInstance.getHeight(), 02186 // config.imageInfoInstance.getWidth()); 02187 02188 02189 // dir = 0; 02190 // ent = 0; 02191 02192 02193 02194 02195 // // ************** END initImages ************ 02196 02197 02198 // if( save_train ) 02199 // Auxillary::writeToFile( config ); //Write down config file - for future use of config.getHeight() etc. 02200 // } 02201 02202 02203 02204 // else{ 02205 02206 // /* 02207 // ************* 02208 // LEARNING OFFLINE 02209 // ************ 02210 // */ 02211 02212 02213 // Auxillary::readFromFile( config ); 02214 02215 // config.setTrainIntegralImagesLoc( TRAINDT ); 02216 02217 // // Auxillary::readFromFile( config.integralImages, 02218 // // config.getTrainIntegralImagesLoc(), 02219 // // config.imageInfoInstance.getHeight(), 02220 // // config.imageInfoInstance.getWidth() ); 02221 02222 02223 02224 // /************ Reading from File : Start Section ************/ 02225 02226 02227 // for( int i=0;i<config.getNclasses();i++) 02228 // config.integralImages[i].iiVector.clear(); 02229 02230 // int height = config.imageInfoInstance.getHeight(); 02231 // int width = config.imageInfoInstance.getWidth(); 02232 02233 // string readLoc = config.getTrainIntegralImagesLoc(); 02234 02235 // //Read the file 02236 // std::ifstream in; 02237 02238 // // in.open(readLoc.c_str()); 02239 02240 // // temp variables to hold values read from file 02241 // int size; 02242 // int var; 02243 02244 // for(int i=0;i<config.getNclasses();i++){ //for each vector 02245 02246 // int p = i; 02247 // char s[ 50 ]; 02248 // sprintf( s, "%s-class_%d.dat", readLoc.c_str(), p ); 02249 02250 02251 // in.open( s ); 02252 // size = 0; 02253 // in>>size; 02254 02255 02256 // // mem_chunk = new int[ height * width * size ]; 02257 02258 // if( verbose && !roc_mode ) { 02259 02260 // printf("\n Total number of Images read (training data): \n"); 02261 // printf(" Class %d has %d images.\n", i, size ); 02262 // } 02263 02264 // for(int j=0;j<size;j++){ 02265 02266 // // allocate in one chunk 02267 02268 // integralImage = new int[ height * width ]; 02269 // // integralImage = ( mem_chunk + j * height * width ); 02270 02271 // for(int h=0;h<height;h++) 02272 // for(int w=0;w<width;w++) { 02273 02274 // in>>var; 02275 // integralImage[ h * width + w ] = var; 02276 02277 // } 02278 02279 // // printf("Image %d read.\n", j); 02280 02281 // config.integralImages[i].iiVector.push_back( integralImage ); 02282 // } 02283 02284 02285 // in.close(); 02286 // } 02287 02288 // integralImage = 0; 02289 02290 02291 // //writeOutIntegralImages(integralImages,NCLASSES, height, width, cout); 02292 // // in.close(); 02293 02294 02295 // //************ Reading from File : End Section ************ 02296 02297 // config.setTestIntegralImagesLoc( TESTDT ); 02298 02299 // } // reading offline 02300 02301 02302 02303 // /*********** TEST MODE: Unit testing and to check memory leaks **********/ 02304 // if( test_mode ) 02305 // _tTree( config ); 02306 02307 02308 02309 // /************* SLIDING WINDOW/ PERFORMANCE ************/ 02310 02311 // int* category = new int[config.getNclasses()]; // The category array ( deals with faces) 02312 // category[0] = 0; category[1] = 1; 02313 02314 // int indexOfFaceCategory = 0; 02315 02316 // // double scales[] = {1.0, 2.0, 0.5, 3.0, 0.3, 2.5, 0.25, 0.1, 0.6, 0.8, 1.5}; // Scales used for sliding window detection scheme 02317 02318 // double scales[] = { 1.0, 2.0, 0.5, 3.0, 0.35, 4.0, 0.25, 0.2, 5.0, 0.15, 0.75, 2.5, 3.25, 5.4, 4.7, 1.3}; 02319 02320 // // double scales[] = { 1.0, 2.0, 0.5, 3.0, 0.25}; 02321 02322 // //double scales[] = { 1.0, 2.0, 0.5, 3.0, 3.5, 4.0, 0.7, 0.4 }; 02323 02324 // //double scales[] = {1.0, 0.25 , 0.5, 0.8 , 0.3, 2.0 , 0.6, 0.4 , 0.1}; 02325 02326 // // double scales[] = { 1.0, 1.0, 1.0, 1.0, 1.0 }; // Check if the program gets killed because of diff image size 02327 02328 02329 // // double scales[] = {1.0, 0.9 , 0.8, 0.7 , 0.85 , 0.6 , 0.5, 0.4 , 0.45}; 02330 02331 // Forest::Tree* aTree = 0; 02332 02333 // int numberOfTreesGrown = 0 ; 02334 02335 02336 // if( run_classification ) { 02337 // // if we run classificaiton, the program exits immediately afterwards 02338 02339 // bool make_forest = true; 02340 // using Forest::Tree; 02341 // //Tree *bTree = 0, *cTree = 0, *dTree = 0, *eTree = 0, *fTree = 0; 02342 // int _forest = _FOREST; 02343 // Tree *remaining_trees[ _forest -1 ]; 02344 02345 02346 // string nameLocOfImage; 02347 // int globalNodeIndex = 0; 02348 // int* tempII; 02349 // aTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02350 02351 // if( make_forest ) 02352 // { 02353 02354 // // this is not memory safe :( 02355 // /* 02356 // numberOfTreesGrown = 0; 02357 // globalNodeIndex = 0; 02358 // bTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02359 02360 // numberOfTreesGrown = 0; 02361 // globalNodeIndex = 0; 02362 // cTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02363 02364 // numberOfTreesGrown = 0; 02365 // globalNodeIndex = 0; 02366 // dTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02367 02368 02369 // numberOfTreesGrown = 0; 02370 // globalNodeIndex = 0; 02371 // eTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02372 02373 02374 // numberOfTreesGrown = 0; 02375 // globalNodeIndex = 0; 02376 // fTree = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02377 // */ 02378 02379 // for( int j = 0; j < _forest -1; j++ ) 02380 // { 02381 // numberOfTreesGrown = 0; 02382 // globalNodeIndex = 0; 02383 // remaining_trees[j] = new Tree( config, globalNodeIndex, numberOfTreesGrown++ ); 02384 // } 02385 02386 // } 02387 02388 02389 // if( verbose ) 02390 // { 02391 // // printf("\n Tree%d has %d faces and %d non-faces.\n", numberOfTreesGrown, aTree->theIntegralImages[0].size(), aTree->theIntegralImages[1].size() ); 02392 // } 02393 02394 // if( testImageSupplied ) 02395 // nameLocOfImage = testImagePath; 02396 // else 02397 // nameLocOfImage = TEST_IMAGE_SLIDING_WINDOW; 02398 02399 // IplImage* imageToClassify = UserDef::getImageFromLocation( nameLocOfImage ); 02400 // int* imageToClassifyII = new int[ imageToClassify->height * imageToClassify->width ]; 02401 // calculateIntegralImage( imageToClassify, imageToClassifyII ); 02402 02403 // int recognition_histogram[ _forest ]; 02404 02405 02406 // using Forest::runClassification; 02407 // recognition_histogram[0] = runClassification( aTree, config, imageToClassifyII ); 02408 // printf("The classification number is %d\n", recognition_histogram[0] ) ; 02409 02410 // if( make_forest ) { 02411 // /* 02412 // recognition_histogram[1] = runClassification( bTree, config, imageToClassifyII ); 02413 // printf("The classification number is %d\n", recognition_histogram[1] ); 02414 02415 // recognition_histogram[2] = runClassification( cTree, config, imageToClassifyII ); 02416 // printf("The classification number is %d\n", recognition_histogram[2] ); 02417 02418 // recognition_histogram[3] = runClassification( dTree, config, imageToClassifyII ); 02419 // printf("The classification number is %d\n", recognition_histogram[3] ); 02420 02421 // recognition_histogram[4] = runClassification( eTree, config, imageToClassifyII ); 02422 // printf("The classification number is %d\n", recognition_histogram[4] ); 02423 02424 // recognition_histogram[5] = runClassification( fTree, config, imageToClassifyII ); 02425 // printf("The classification number is %d\n", recognition_histogram[5] ); 02426 // */ 02427 02428 // for( int j =1; j< _forest; j++ ) 02429 // { 02430 // recognition_histogram[j] = runClassification( remaining_trees[j-1], config, imageToClassifyII ); 02431 // printf("the classifiation number is %d\n", recognition_histogram[j] ); 02432 // } 02433 02434 02435 // int votes[ nclasses ]; 02436 02437 // for( int j = 0; j < nclasses; j++ ) 02438 // votes[j] = 0; 02439 02440 // for( int j = 0; j < _forest; j++ ) 02441 // votes[ recognition_histogram[j] ]++; 02442 02443 // int max_identity = -1; 02444 // int index_max_identity = -1; 02445 02446 // for( int j = 0; j < nclasses; j++ ) 02447 // if( votes[j] > max_identity ) 02448 // { 02449 // max_identity = votes[j]; 02450 // index_max_identity = j; 02451 // } 02452 02453 // printf("the classification final result is %d\n", index_max_identity ); 02454 02455 // } 02456 02457 02458 // for( int i = 0; i < config.getNclasses(); i++ ) 02459 // for( int j = 0; j < config.integralImages[i].size(); j++ ) { 02460 02461 // tempII = config.integralImages[i].iiVector.at( j ); 02462 // delete[] tempII; 02463 // } 02464 02465 // delete( aTree ); 02466 02467 // if( make_forest ) 02468 // { 02469 // for( int j =0; j < _forest -1; j++ ) 02470 // delete( remaining_trees[j] ); 02471 // } 02472 02473 // exit(0); 02474 02475 // } 02476 02477 02478 02479 // if( performance ) { 02480 02481 // runPerformance( config, 02482 // infoname, 02483 // category, 02484 // indexOfFaceCategory, 02485 // saveDetected, 02486 // dt, 02487 // forestSize, 02488 // maxPosDiff, 02489 // maxSizeDiff, 02490 // scales, 02491 // number_of_scale_ranges, 02492 // roc_mode ); 02493 02494 02495 02496 // } // if(performance).. 02497 02498 02499 // if( !performance && !test_mode ) { // default run with detections drawn: Ready to use 02500 02501 02502 // // THE TREES SECTION - UNCOMMMENT IF U WANT TO BUILD A FOREST 02503 // // while( numberOfTreesGrown < forestSize ) { 02504 02505 // // The handling of the performance of a forest will be handleded individually 02506 // // depenending on whether its (a) Classification (b) Sliding window detection 02507 02508 // int globalNodeIndex = 0; 02509 // config.forestWindows.clear(); 02510 02511 02512 // if( aTree != 0 ) 02513 // dealloc_mem( aTree ); 02514 02515 02516 02517 // // UserDef::VectorOfIntegralImages vectorOfIntegralImages; 02518 02519 // // for( int i = 0 ; i < NCLASSES ; i++ ) 02520 // // for( int j = 0 ; j < config.integralImages[i].iiVector.size() ; j++ ) 02521 // // vectorOfIntegralImages[i].iiVector.push_back( &( config.integralImages[i].iiVector.at(j) ) ); 02522 02523 02524 // aTree = new Forest::Tree( config, 02525 // globalNodeIndex, 02526 // numberOfTreesGrown++ ); // 1. First grow the tree 02527 02528 02529 02530 // if( verbose ) { 02531 02532 // printf("\n Tree%d has %d faces and %d non-faces.\n", numberOfTreesGrown, 02533 // aTree->theIntegralImages[0].size(), aTree->theIntegralImages[1].size() ); 02534 02535 // /* printf(" The following details pertain to Tree#%d :\n", 02536 // numberOfTreesGrown ); 02537 02538 // printf(" The Treelevel: %d\t Level_: %d\t integralImageSize1: %d\t integral2: %d\t\n", 02539 // aTree->getTreeLevel(), aTree->level_, aTree->theIntegralImages[0].iiVector.size(), 02540 // aTree->theIntegralImages[1].iiVector.size() ); 02541 02542 // */ } 02543 02544 02545 // /* if( identityfile != 0 ) 02546 // { 02547 // */ 02548 02549 // if( identityfileSupplied ) 02550 // { 02551 02552 // IplImage* propagateImage = UserDef::getImageFromLocation( identityfile ); 02553 // int* propagateImageII = new int[ propagateImage->height * propagateImage->width ]; 02554 02555 // calculateIntegralImage( propagateImage, propagateImageII ); 02556 02557 // Forest::propagateTree( aTree, config, propagateImageII, propagateImage->height, 02558 // propagateImage->width ); 02559 02560 02561 // exit(1); 02562 // } 02563 02564 // // } 02565 02566 // start_detection = std::clock(); 02567 // for( int nos_scales = 0 ; nos_scales < number_of_scale_ranges ; nos_scales++ ) 02568 // { 02569 02570 02571 // UserDef::WindowClass* wc = 0; 02572 // for( unsigned int i = 0; i < config.windows.size() ; i++ ) 02573 // { 02574 // wc = config.windows.at( i ); 02575 // delete wc; 02576 // } 02577 02578 // config.windows.clear(); 02579 02580 // string nameLocOfImage; 02581 02582 // if( testImageSupplied ) 02583 // nameLocOfImage = testImagePath; 02584 02585 // else 02586 // nameLocOfImage = TEST_IMAGE_SLIDING_WINDOW; 02587 02588 02589 // if( RUN_SLIDING_WINDOW ) { // ********** sliding_window ******** 02590 02591 02592 02593 02594 // double scaleFactor = scales[ nos_scales ]; 02595 02596 // if( vote==1 ) { // Running through differentlty scaled images 02597 02598 // IplImage* imageToResize = UserDef::getImageFromLocation(nameLocOfImage); 02599 02600 // /* 02601 // * config.getDetectionThreshold might be deprecated. I am using the mergeDetections in place. 02602 // */ config.setDetectionThreshold((int)( scaleFactor * dt ) ); 02603 02604 // // images unable to deal with sizes greater than 1400-1440 02605 // if( (int)( scaleFactor * imageToResize->height ) < 1300 && 02606 // (int)( scaleFactor * imageToResize->width ) < 1300 ) { 02607 02608 // bool security_check = false; 02609 02610 // if( security_check ) { 02611 02612 // if(config.getDetectionThreshold()==0 || config.getDetectionThreshold()<0) 02613 // config.setDetectionThreshold(1); 02614 02615 02616 // if( 02617 // config.imageInfoInstance.getHeight() > 02618 // (int)( scaleFactor * config.testImageInfoInstance.getHeight() ) || 02619 // config.imageInfoInstance.getWidth() > 02620 // (int)( scaleFactor * config.testImageInfoInstance.getWidth() ) 02621 // ) 02622 // scaleFactor = 1.0; // Is scale too magnified? Then, do not resize test Image 02623 02624 // if( 02625 // (int)( scaleFactor * config.testImageInfoInstance.getHeight() ) == 0 02626 // || 02627 // (int)( scaleFactor * config.testImageInfoInstance.getWidth() ) == 0 ) 02628 // scaleFactor = 1.0; // Is scale too minute? Then, do not resize test image 02629 02630 02631 // } 02632 02633 // CvSize resizeImageSize = cvSize( (int)(scaleFactor*imageToResize->width), 02634 // (int) (scaleFactor*imageToResize->height) ); 02635 02636 // IplImage* slidingWindowTestIplImage = cvCreateImage( resizeImageSize, 02637 // imageToResize->depth, 02638 // imageToResize->nChannels ); 02639 02640 // cvResize( imageToResize, 02641 // slidingWindowTestIplImage, 02642 // CV_INTER_LINEAR ); 02643 // cvReleaseImage( &imageToResize ); 02644 02645 // int* integralImageOfSlidingWindow = new int[( slidingWindowTestIplImage->height) * 02646 // (slidingWindowTestIplImage->width)]; 02647 02648 // calculateIntegralImage(slidingWindowTestIplImage, 02649 // integralImageOfSlidingWindow); 02650 02651 // config.testImageInfoInstance.setHeight(slidingWindowTestIplImage->height); //set new height 02652 // config.testImageInfoInstance.setWidth(slidingWindowTestIplImage->width); //set new widht 02653 02654 // aTree->slidingWindow( aTree, 02655 // integralImageOfSlidingWindow, 02656 // config, 02657 // category, 02658 // indexOfFaceCategory, 02659 // aTree->getForestIndex(), 02660 // scaleFactor ); 02661 02662 02663 // config.globalDetectionsFile<<"\n---------End of Tree: Before Merges------------\n"; 02664 // Auxillary::printConfigWindows( config, config.windows ); 02665 02666 02667 // config.globalDetectionsFile<<"\n---------End of Tree: After Merges------------\n"; 02668 02669 // mergeDetections( config.windows, config , scaleFactor ); 02670 02671 // Auxillary::printConfigWindows( config, config.windows ); 02672 02673 02674 // // if( forestSize > 1 ) { // integrate the results of each tree 02675 02676 02677 02678 // UserDef::filterNoise6( config.windows, config ); 02679 02680 02681 // //filterNoise( config.windows, config ); 02682 // dummy_combineVotes(config, 02683 // NEIGHBOR_RANGE, 02684 // scaleFactor ); 02685 // // I am going to try to merge the windows from a scale 02686 // // filterNoise( config.windows, config ); 02687 02688 // // } 02689 02690 // char nameAdd[ 10 ] ; 02691 // sprintf( nameAdd , "%d#%d-" , numberOfTreesGrown , nos_scales ); // Unique name 02692 02693 02694 02695 // if( saveDetected ) 02696 // UserDef::drawDetections( slidingWindowTestIplImage, 02697 // config.windows, 02698 // nameLocOfImage, 02699 // nameAdd, 02700 // saveDetected ); 02701 02702 // cvReleaseImage( &slidingWindowTestIplImage ); 02703 // delete[] integralImageOfSlidingWindow; 02704 02705 // } // height, width < 1400 02706 02707 // } // if(vote==1).. 02708 02709 02710 // if(vote==2){ 02711 02712 // ERR_MODULE("int main: vote==2"); 02713 // exit( 1 ); 02714 02715 02716 // // string nameLocOfImage; 02717 02718 // // if(testImageSupplied) 02719 // // nameLocOfImage = testImagePath; 02720 // // else 02721 // // nameLocOfImage = TEST_IMAGE_SLIDING_WINDOW; 02722 02723 // // double scaleFactor = scales[numberOfTreesGrown-1]; 02724 02725 // // // Since in the case of vote==2, I am increasing expecting bigger and bigger 02726 // // // rectangles, I consider the division inverse of the scale Factor 02727 // // scaleFactor = 1.0F/scaleFactor; 02728 02729 02730 // // IplImage* slidingWindowTestIplImage = UserDef::getImageFromLocation(nameLocOfImage); 02731 02732 02733 // // config.setDetectionThreshold( (int)( scaleFactor * dt ) ); 02734 02735 // // if( config.getDetectionThreshold() == 0 || 02736 // // config.getDetectionThreshold() < 0 ) 02737 02738 // // config.setDetectionThreshold(1); 02739 02740 02741 // // int* integralImageOfSlidingWindow = new int[(slidingWindowTestIplImage->height) 02742 // // *(slidingWindowTestIplImage->width)]; 02743 02744 // // calculateIntegralImage(slidingWindowTestIplImage, integralImageOfSlidingWindow); 02745 02746 02747 // // config.testImageInfoInstance.setHeight(slidingWindowTestIplImage->height); 02748 // // config.testImageInfoInstance.setWidth(slidingWindowTestIplImage->width); 02749 02750 // // if( 02751 // // (int)( scaleFactor * config.imageInfoInstance.getHeight()) > 02752 // // config.testImageInfoInstance.getHeight() 02753 // // || 02754 // // (int)(scaleFactor * config.imageInfoInstance.getWidth()) > 02755 // // config.testImageInfoInstance.getWidth() 02756 // // ) 02757 // // scaleFactor = 1.0; // It could the scale is too high 02758 02759 // // if( 02760 // // (int)( scaleFactor * config.imageInfoInstance.getHeight()) == 0 02761 // // || (int)( scaleFactor * config.imageInfoInstance.getWidth()) == 0 02762 // // ) 02763 // // scaleFactor = 1.0; //the scale is too low 02764 02765 // // aTree->slidingWindowWithScaling(aTree, 02766 // // integralImageOfSlidingWindow, 02767 // // config, 02768 // // category, 02769 // // indexOfFaceCategory, 02770 // // scaleFactor); 02771 02772 // // //!!!!!!!!!!!!!NOT CALLING MERGE WINDOWS 02773 // // /*Before calling drawDetections, I wil merge 02774 // // subwindows to reduce the number of false positives - 02775 // // in line with what has been done in \cite{violaJones2001} */ 02776 // // // UserDef::mergeDetectionSubwindow(config.windows, 02777 // // // config.imageInfoInstance.getHeight(), 02778 // // // config.imageInfoInstance.getWidth(), 02779 // // // config); 02780 02781 02782 02783 02784 // // Auxillary::printConfigWindows( config, config.windows ); 02785 // // config.globalDetectionsFile<<"\n---------End of Tree: Before Merges------------\n"; 02786 02787 // // mergeDetections( config.windows, config , scaleFactor ); 02788 // // Auxillary::printConfigWindows( config, config.windows ); 02789 // // config.globalDetectionsFile<<"\n---------End of Tree: After Merges------------\n"; 02790 02791 // // if( forestSize > 1 ) { // integrate the results of each tree 02792 02793 // // // Here, the combine votes actually rescales the windows. Usually, in case of vote==1 , 02794 // // // the windows are always of the size 19X19 and the images are scaled as well. 02795 // // // So when we send the detections to the combine module, we scale them appropriately. 02796 // // // On the other hand, in the case of vote==2, we simply neither need to scale the window sizes 02797 // // // nor to scale window sizes. So, I call combineVotes with 1/scaleFactor 02798 02799 // // // !! This does not work because although the window coordinates end up being correctly transformed 02800 // // // (i.e. they shoudl remain the same), the actual coordinates get transformed. 02801 02802 // // combineVotes_2( config, 02803 // // NEIGHBOR_RANGE ); 02804 // // } 02805 02806 02807 02808 // // char nameAdd[10]; 02809 // // sprintf(nameAdd,"%d-", numberOfTreesGrown); 02810 02811 // // if( saveDetected ) 02812 // // UserDef::drawDetections(slidingWindowTestIplImage, 02813 // // config.windows, 02814 // // nameLocOfImage, 02815 // // nameAdd, 02816 // // saveDetected); 02817 02818 02819 // // cvReleaseImage(&slidingWindowTestIplImage); 02820 02821 // } // if(vote==2)... 02822 02823 // } //if(RUN_SLIDIN.. 02824 02825 02826 02827 // } // for( nos_scales .. 02828 02829 02830 // // IF SLIDINGWINDOW was called, draw final set of detections 02831 // if(RUN_SLIDING_WINDOW) { 02832 02833 02834 // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-BEFORE TRIM--\n"; 02835 // Auxillary::printConfigWindows( config, config.forestWindows ); 02836 02837 // // am gonna run filternoise5 for the final 02838 02839 // // filterNoise5( config.forestWindows, config, 1.0, 1 ); 02840 // // filterNoise6( config.forestWindows, config ); 02841 02842 // //probably need to run filterNoise3 to get the se of windows removed and then run 02843 // //filternoise5 module 0 The filternoise3 kind of oput everything inside everytghin else within the range of area_windowo_merge and the n filernoise5 module 0 starts to tak eaverages. 02844 02845 // // filterNoise3( config.forestWindows, config, 1.0 ); 02846 // // filterNoise3( config.forestWindows, config, 1.0 ); 02847 02848 // // filterNoise5( config.forestWindows, config, 1.0, 1 ); //first put windows together 02849 // // filterNoise5( config.forestWindows, config, 1.0, 0 ); // then emrge by averaging 02850 02851 // filterNoise5( config.forestWindows, config, 1.0, 1 ); 02852 // filterNoise5( config.forestWindows, config, 1.0, 1 ); 02853 02854 02855 // printf("the number of windows are %zu", config.forestWindows.size() ); 02856 02857 // for(unsigned int q = 0; q < config.forestWindows.size(); q++ ) 02858 // { 02859 02860 // printf("%d %d %d %d %f\n", config.forestWindows.at(q)->getX(), 02861 // config.forestWindows.at(q)->getY(), 02862 // config.forestWindows.at(q)->getHeight(), 02863 // config.forestWindows.at(q)->getWidth(), 02864 // config.forestWindows.at(q)->getFaceProbabilityResult() ); 02865 02866 // } 02867 02868 02869 02870 02871 // IplImage* slidingWindowTestIplImage = UserDef::getImageFromLocation(testImagePath); 02872 // char nameAdd[ 20 ]; 02873 // sprintf( nameAdd, "final_%d-", numberOfTreesGrown ); 02874 02875 // bool step1 = false; 02876 // //bool step2 = false; 02877 02878 // // comparePerformance(); 02879 02880 // end_detection = std::clock(); 02881 // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-AFTER-TRIM--\n"; 02882 // if( saveDetected ) 02883 // UserDef::drawDetections(slidingWindowTestIplImage, 02884 // config.forestWindows, testImagePath, nameAdd, saveDetected); 02885 02886 // Auxillary::printConfigWindows(config, config.forestWindows); 02887 // cvReleaseImage( &slidingWindowTestIplImage ); 02888 02889 // slidingWindowTestIplImage = UserDef::getImageFromLocation( testImagePath ); 02890 02891 02892 // //now we crop the images 02893 // vector< IplImage* > faces; 02894 // for( unsigned int i = 0; i < config.forestWindows.size(); i++ ) 02895 // { 02896 // IplImage* face; 02897 02898 // if( ALLOW_CROP_BOUNDARY_HEURISTIC && config.forestWindows.at(i)->getX() > 5 && 02899 // config.forestWindows.at(i)->getY() > 5 ) 02900 // { 02901 // face = cvCreateImage( cvSize( config.forestWindows.at(i)->getWidth() + 10, 02902 // config.forestWindows.at(i)->getHeight() + 10 ), 02903 // slidingWindowTestIplImage->depth, 02904 // slidingWindowTestIplImage->nChannels ); 02905 02906 // cvSetImageROI( slidingWindowTestIplImage, 02907 // cvRect( config.forestWindows.at(i)->getX() - 5, 02908 // config.forestWindows.at(i)->getY() - 5, 02909 // config.forestWindows.at(i)->getWidth() + 10, 02910 // config.forestWindows.at(i)->getHeight() + 10 ) ); 02911 02912 // } 02913 02914 // else 02915 // { 02916 02917 // face = cvCreateImage( cvSize( config.forestWindows.at(i)->getWidth(), 02918 // config.forestWindows.at(i)->getHeight() ), 02919 // slidingWindowTestIplImage->depth, 02920 // slidingWindowTestIplImage->nChannels ); 02921 02922 // cvSetImageROI( slidingWindowTestIplImage, 02923 // cvRect( config.forestWindows.at(i)->getX(), 02924 // config.forestWindows.at(i)->getY(), 02925 // config.forestWindows.at(i)->getWidth(), 02926 // config.forestWindows.at(i)->getHeight() ) ); 02927 02928 // } 02929 // cvCopy( slidingWindowTestIplImage, face, 0 ); 02930 02931 // char number[10]; 02932 // sprintf( number, "%d", i); 02933 02934 // char face_storage[PATH_MAX + 50]; 02935 // strcpy( face_storage, testImagePath ); 02936 // strcat( face_storage, "-crop-" ); 02937 // strcat( face_storage, number ); 02938 // strcat( face_storage, ".png" ); 02939 02940 02941 // // printf("it will be rescaled to %d %d \n", config.imageInfoInstance.getWidth(), 02942 // // config.imageInfoInstance.getHeight() ); 02943 02944 // CvSize scaledCropImageSize = cvSize( config.imageInfoInstance.getWidth(), 02945 // config.imageInfoInstance.getHeight() 02946 // ); 02947 // IplImage *scaledFace = cvCreateImage( scaledCropImageSize, 02948 // face->depth, 02949 // face->nChannels ); 02950 02951 // cvResize( face, 02952 // scaledFace, 02953 // CV_INTER_LINEAR ); 02954 02955 // cvvSaveImage( face_storage, scaledFace ); 02956 02957 // cvReleaseImage( &scaledFace ); 02958 02959 // cvResetImageROI( slidingWindowTestIplImage ); 02960 02961 02962 // if( step1 ) 02963 // { 02964 02965 // faces.push_back ( face ); 02966 02967 // // now, train ght enew tree 02968 // // run face dwon the new tree 02969 // //and return histogram answer 02970 02971 // } 02972 02973 02974 // cvReleaseImage( &face ); 02975 02976 02977 // } 02978 02979 02980 // // if( saveDetected ) 02981 // // UserDef::drawDetections(slidingWindowTestIplImage, 02982 // // config.forestWindows, testImagePath, nameAdd, saveDetected); 02983 02984 02985 // cvReleaseImage(&slidingWindowTestIplImage); 02986 02987 // } // if(RUN_SLID.. 02988 02989 02990 02991 // // } // while(numberOfTrees..) 02992 02993 // // IF SLIDINGWINDOW was called, draw final set of detections 02994 // // if(RUN_SLIDING_WINDOW) { 02995 02996 02997 // // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-BEFORE TRIM--\n"; 02998 // // Auxillary::printConfigWindows( config, config.forestWindows ); 02999 03000 // // using UserDef::filterNoise1; 03001 // // using UserDef::filterNoise2; 03002 // // filterNoise2( config, config.forestWindows); 03003 03004 // // IplImage* slidingWindowTestIplImage = UserDef::getImageFromLocation(testImagePath); 03005 // // char *nameAdd = "final-"; 03006 03007 // // // comparePerformance(); 03008 03009 // // config.globalDetectionsFile<<"\n---GLOBAL DETECTIONS-AFTER-TRIM--\n"; 03010 // // Auxillary::printConfigWindows(config, config.forestWindows); 03011 03012 03013 03014 // // if( saveDetected ) 03015 // // UserDef::drawDetections(slidingWindowTestIplImage, 03016 // // config.forestWindows, testImagePath, nameAdd, saveDetected); 03017 03018 // // cvReleaseImage(&slidingWindowTestIplImage); 03019 // // } // if(RUN_SLID.. 03020 03021 03022 // if(LEARN_OFFLINE){ 03023 03024 // ERR_NWORKING; 03025 03026 // /* ---------- may not be working ---------- */ 03027 03028 // /* 03029 // std::ofstream treeout, testout; 03030 03031 // treeout.open(TRAINTREE); 03032 // testout.open(TRAINTESTS); 03033 03034 // writeToFile(treeout, testout, *aTree); 03035 03036 // treeout.close(); 03037 // testout.close(); 03038 03039 // Forest::Tree* bTree =0;//= new Forest::Tree(0,0,0,0,0); 03040 // std::ifstream treein, testin; 03041 03042 // treein.open(TRAINTREE); 03043 // testin.open(TRAINTESTS); 03044 03045 // readFromFile(treein, testin, &bTree); 03046 // treein.close(); 03047 // testin.close(); 03048 03049 // std::ofstream treeout1, testout1; 03050 03051 // treeout1.open("../info/traintree1.dat"); 03052 // testout1.open("../info/traintests.dat"); 03053 03054 // writeToFile(treeout1, testout1, *bTree); 03055 // treeout1.close(); 03056 // testout1.close(); 03057 // */ 03058 // } 03059 03060 // } // if(!performance) 03061 03062 03063 03064 03065 // /* File write 03066 // FILE* fp; 03067 // fp = fopen( "../info/tree.dat", "w" ); 03068 03069 03070 // Forest::writeToFile( aTree, fp ); 03071 03072 // fclose( fp ); 03073 // */ 03074 03075 03076 // /* dealloc the mem */ 03077 // // if( config.getRelearnStatusValue() ) { 03078 03079 // for( int i = 0; i < config.getNclasses(); i++ ) 03080 // for( int j = 0; j < config.integralImages[i].size(); j++ ) { 03081 03082 03083 // tempII = config.integralImages[i].iiVector.at( j ); 03084 // delete[] tempII; 03085 // } 03086 03087 // // } 03088 03089 03090 // // else 03091 // // delete[] mem_chunk; 03092 03093 // // } 03094 03095 // tempII = 0; 03096 // mem_chunk = 0; 03097 03098 // delete[] category; 03099 03100 // delete aTree; 03101 03102 // final(config); 03103 03104 // finish = std::clock(); 03105 03106 // if( !roc_mode) 03107 // cout<<endl<<"The time taken for complete execution in seconds:"<<(double)(finish - start)/( 1.0* CLOCKS_PER_SEC)<<endl; 03108 03109 // if( !roc_mode ) 03110 // cout<<endl<<"The time taken for detection alone in seconds:"<<(double)(end_detection - start_detection)/(1.0* CLOCKS_PER_SEC)<<endl; 03111 03112 return 0; 03113 } 03114 03115 /// @endcond 03116

