00001 /*!@file VFAT/test-NPclassify.C Test the non-parametric classifier */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: T Nathan Mundhenk <mundhenk@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/VFAT/test-NPclassify.C $ 00035 // $Id: test-NPclassify.C 14376 2011-01-11 02:44:34Z pez $ 00036 // 00037 00038 // ############################################################ 00039 // ############################################################ 00040 // ##### ---NPclassify--- 00041 // ##### non-parametric classifier: 00042 // ##### T. Nathan Mundhenk nathan@mundhenk.com 00043 // ##### Vidhya Navalpakkam - navalpak@usc.edu 00044 // ##### partners full name - email 00045 // ############################################################ 00046 // ############################################################ 00047 00048 //This is the start of the execution path for the NPclassify test alg. 00049 00050 #include "VFAT/NPclassify2.H" 00051 #include "Raster/Raster.H" 00052 #include "VFAT/covEstimate.H" 00053 #include "VFAT/findColorIndex.H" 00054 #include "Util/Timer.H" 00055 00056 #define INVALS 31 00057 00058 //! This is the configFile name 00059 char* configFile; 00060 //! This is the configFile object 00061 readConfig configIn(25); 00062 readConfig polySet(25); 00063 //! generic pixel 00064 PixRGB<float> pix; 00065 //! number of items if training 00066 int itemNumber; 00067 float inVals[INVALS]; 00068 00069 int main(int argc, char* argv[]) 00070 { 00071 // start timer 00072 Timer tim; 00073 tim.reset(); 00074 uint64 t0 = tim.get(); // to measure display time 00075 // get test image 00076 Image<byte> input = Raster::ReadGray(argv[1]); 00077 if(argc > 2) 00078 itemNumber = atoi(argv[2]); 00079 else 00080 itemNumber = -666; 00081 Image<float> finput = input; 00082 Image<float> outputDensity; 00083 Image<PixRGB<float> > outputLinks; 00084 outputDensity.resize(input.getWidth(),input.getHeight()); 00085 outputLinks.resize((input.getWidth()*2),input.getHeight()); 00086 // create operating objects 00087 configIn.openFile("NPclassify.conf"); 00088 polySet.openFile("polySet.conf"); 00089 00090 std::vector<float> feature(2,0); 00091 std::vector<std::vector<float> > features(50,feature); 00092 std::vector<int*> roots; 00093 std::vector<int*> parents; 00094 std::vector<float> density; 00095 std::vector<int> priorItemVal(50,0); 00096 00097 long featureCount = 0; 00098 00099 // convert test image to vector format 00100 for(int x = 0; x < finput.getWidth(); x++) 00101 { 00102 for(int y = 0; y < finput.getHeight();y++) 00103 { 00104 if(finput.getVal(x,y) < 254.0F) 00105 { 00106 // resize vector if needed 00107 if((unsigned)(featureCount) == features.size()) 00108 { 00109 features.resize((featureCount+10),feature); 00110 priorItemVal.resize((featureCount+10),0); 00111 //LINFO("RESIZED SPACE %d",featureCount+50); 00112 } 00113 // insert x and y into vector 00114 priorItemVal[featureCount] = (int)finput.getVal(x,y); 00115 features[featureCount][0] = x; 00116 features[featureCount][1] = y; 00117 featureCount++; 00118 } 00119 } 00120 } 00121 00122 00123 std::vector<int> priorItemMember(featureCount,0); 00124 std::vector<int> priorClassVal(featureCount,0); 00125 std::vector<int> priorClassSize(featureCount,0); 00126 int priorClasses = 0; 00127 00128 for(int i = 0; i < featureCount; i++) 00129 { 00130 bool add = true; 00131 for(int j = 0; j < priorClasses; j++) 00132 { 00133 if(priorItemVal[i] == priorClassVal[j]) 00134 { 00135 LINFO("ADDED %d to class %d, value %d",i,j, 00136 priorItemVal[i]); 00137 priorItemMember[i] = j; 00138 priorClassSize[j]++; 00139 add = false; 00140 } 00141 } 00142 if(add == true) 00143 { 00144 priorClassVal[priorClasses] = priorItemVal[i]; 00145 priorItemMember[i] = priorClasses; 00146 priorClassSize[priorClasses]++; 00147 priorClasses++; 00148 } 00149 } 00150 00151 00152 00153 00154 LINFO("Number of feature vectors %ld",featureCount); 00155 00156 // (1) create the NP classify object, input conf files, specify if settings 00157 // are from command line 00158 NPclassify2<float> NP(configIn,polySet,false); 00159 // (2) input any command line arguments if any 00160 if(argc > 3) 00161 { 00162 for(int i = 0; i < INVALS; i++) 00163 { 00164 inVals[i] = atof(argv[4+i]); 00165 } 00166 NP.NPinputCommandLineSettings(inVals); 00167 } 00168 00169 00170 //----------------------------------------------------------------// 00171 00172 // classify space using density webs 00173 // (3) specify the size of your space in samples and dimensions 00174 NP.NPresizeSpace(featureCount,2); 00175 // (4) input the current vector into the NP clusterer 00176 NP.NPaddSpace(features); 00177 // (5) start the alg. 00178 NP.NPclassifySpaceNew(false); 00179 std::cerr << "DONE\n"; 00180 00181 uint64 t1 = tim.get(); 00182 t0 = t1 - t0; 00183 //LINFO("classifySpaceNew took %dms for image", t0); 00184 LINFO("Get Return Info"); 00185 roots = NP.NPgetStems(); 00186 parents = NP.NPgetParents(); 00187 density = NP.NPgetDensity(); 00188 00189 std::vector<std::vector<int*> > theReturn = NP.NPgetChildren(); 00190 00191 std::vector<std::vector<int> > BB = NP.NPgetBoundingBoxes(false); 00192 LINFO("Draw Links"); 00193 // draw links 00194 for(int i = 0; i < featureCount; i++) 00195 { 00196 float setValue = (float)(density[i]/NP.NPgetMaxDensity())*255; 00197 outputDensity.setVal((int)features[i][0],(int)features[i][1],setValue); 00198 00199 pix.setRed(255),pix.setGreen(0);pix.setBlue(0); 00200 outputLinks.setVal(((int)features[i][0]+input.getWidth()) 00201 ,((int)features[i][1]),pix); 00202 pix.setRed(0),pix.setGreen(255);pix.setBlue(0); 00203 00204 if(*parents[i] != -1) 00205 { 00206 for(int j = 0; j < NP.NPgetStemNumber(); j++) 00207 { 00208 if(*roots[j] == i) 00209 { 00210 pix.setRed(255); 00211 pix.setGreen(0); 00212 } 00213 } 00214 drawLine(outputLinks, Point2D<int>((int)features[i][0],(int)features[i][1]), 00215 Point2D<int>((int)features[*parents[i]][0] 00216 ,(int)features[*parents[i]][1]),pix,1); 00217 } 00218 } 00219 LINFO("Draw Bounding Box"); 00220 // draw bounding boxes 00221 pix.setRed(0),pix.setGreen(0);pix.setBlue(255); 00222 NP.NPdrawBoundingBoxes(&BB,&outputLinks,0,0,pix,true); 00223 //draw each class in a seperate image 00224 if(NP.NP_doClassMap == 1) 00225 { 00226 LINFO("Draw Class Images"); 00227 Image<PixRGB<float> > classMap; 00228 Image<PixRGB<float> > priorClassMap; 00229 00230 PixRGB<float> mrPixel; 00231 classMap.resize(finput.getWidth(),finput.getHeight()); 00232 priorClassMap.resize(finput.getWidth(),finput.getHeight()); 00233 findColorIndex FAC; 00234 00235 pix.setRed(255),pix.setGreen(0);pix.setBlue(0); 00236 std::vector<int> sortClassSize(NP.NPgetStemNumber()+1,0); 00237 std::vector<int> sortClassMember(NP.NPgetStemNumber()+1,0); 00238 int sortCount = 0; 00239 int realClasses = 0; 00240 00241 //********************************************************** 00242 // draw cov matrix 00243 Image<float> final; 00244 final.resize(finput.getWidth(),finput.getWidth()); 00245 for(int i = 0; i < NP.NPgetStemNumber(); i++) 00246 { 00247 int classSize = NP.NPgetClassSize(i); 00248 if(NP.NPgetMinClassSize() <= classSize) 00249 { 00250 realClasses++; 00251 float t = 0.0F; 00252 float* tfloat = &t; 00253 std::vector<float> _vinput(classSize,0); 00254 std::vector<float*> _vTinput(classSize,tfloat); 00255 std::vector<std::vector<float> > NPclass(2,_vinput); 00256 std::vector<std::vector<float*> > NPclassP(2,_vTinput); 00257 for(int cs = 0; cs < classSize; cs++) 00258 { 00259 long item = NP.NPgetClass(i,cs); 00260 NPclass[0][cs] = finput.getWidth() - NP.NPgetFeature(item,0); 00261 std::cerr << "INPUTING " << NPclass[0][cs] << "\n"; 00262 NPclass[1][cs] = NP.NPgetFeature(item,1); 00263 std::cerr << "INPUTING " << NPclass[1][cs] << "\n"; 00264 NPclassP[0][cs] = &NPclass[0][cs]; 00265 NPclassP[1][cs] = &NPclass[1][cs]; 00266 } 00267 covHolder<float> covh; 00268 covh.resize(2,classSize,0.0F); 00269 covEstimate<float> CE(NPclassP,covh); 00270 CE.run(); 00271 final = CE.returnCovSlice(0,1,final); 00272 } 00273 } 00274 Raster::VisuFloat(final,0,sformat("%s.outputCov.pgm",argv[1])); 00275 00276 00277 for(int i = 0; i < featureCount; i++) 00278 { 00279 unsigned int foo = (unsigned)priorItemMember[i]; 00280 FAC.FACgetColor12(&foo,&mrPixel); 00281 priorClassMap.setVal((int)features[i][0] 00282 ,(int)features[i][1],mrPixel); 00283 } 00284 00285 //******************************************************* 00286 // sort classes based upon size 00287 for(int i = 0; i < NP.NPgetStemNumber(); i++) 00288 { 00289 // if I am the first class, set me as number 1 00290 if(sortCount == 0) 00291 { 00292 //LINFO("SETTING first class at %d size %d",i,NP.NPgetClassSize(i)); 00293 sortClassSize[0] = NP.NPgetClassSize(i); 00294 sortClassMember[0] = i; 00295 sortCount++; 00296 } 00297 else 00298 { 00299 bool setThis = false; 00300 // look through the entire list in order 00301 int initSC = sortCount; 00302 for(int j = 0; j < initSC; j++) 00303 { 00304 // if I am bigger than someone, bump him and 00305 // everyone else back one, insert me 00306 if(NP.NPgetClassSize(i) > sortClassSize[j]) 00307 { 00308 //LINFO("SETTING CLASS %d as larger than %d",i,sortClassMember[j]); 00309 ///LINFO("...SIZE %d > %d",NP.NPgetClassSize(i),sortClassSize[j]); 00310 //LINFO("...COUNT %d",j); 00311 setThis = true; 00312 int tempClassSize; 00313 int tempClassNum; 00314 int newClassSize = NP.NPgetClassSize(i); 00315 int newClassNum = i; 00316 for(int k = j; k <= sortCount; k++) 00317 { 00318 tempClassSize = sortClassSize[k]; 00319 tempClassNum = sortClassMember[k]; 00320 //LINFO("SETTING %d as class %d",k,newClassNum); 00321 sortClassSize[k] = newClassSize; 00322 sortClassMember[k] = newClassNum; 00323 newClassSize = tempClassSize; 00324 newClassNum = tempClassNum; 00325 } 00326 break; 00327 } 00328 } 00329 if(setThis == false) 00330 { 00331 00332 //LINFO("SETTING default at %d size %d",i,NP.NPgetClassSize(i)); 00333 //LINFO("...NUMBER %d",sortCount); 00334 sortClassSize[sortCount] = NP.NPgetClassSize(i); 00335 sortClassMember[sortCount] = i; 00336 } 00337 sortCount++; 00338 } 00339 } 00340 00341 //******************************************************* 00342 // draw output images 00343 for(int i = 0; i < sortCount; i++) 00344 { 00345 unsigned int number; 00346 int *thisClass = &sortClassMember[i]; 00347 LINFO("%d : CLASS %d SIZE %d",i,*thisClass 00348 ,NP.NPgetClassSize(*thisClass)); 00349 number = (unsigned)(i + 1); 00350 00351 //if(NP.NPisStem(*thisClass) == true) 00352 // number = 0; 00353 LINFO("SETTING PIXEL %d",number); 00354 FAC.FACgetColor12(&number,&mrPixel); 00355 Image<PixRGB<float> > outputClass; 00356 outputClass.resize(finput.getWidth(),finput.getHeight()); 00357 std::cerr << "class size " << NP.NPgetClassSize(*thisClass) << "\n"; 00358 for(int j = 0; j < NP.NPgetClassSize(*thisClass); j++) 00359 { 00360 long item = NP.NPgetClass(*thisClass,j); 00361 outputClass.setVal((int)NP.NPgetFeature(item,0) 00362 ,(int)NP.NPgetFeature(item,1),pix); 00363 classMap.setVal((int)NP.NPgetFeature(item,0) 00364 ,(int)NP.NPgetFeature(item,1),mrPixel); 00365 if(j == 0) 00366 { 00367 if(NP.NPgetMinClassSize() <= NP.NPgetClassSize(*thisClass)) 00368 { 00369 char foo; 00370 sprintf(&foo,"%d",*thisClass); 00371 writeText(classMap, Point2D<int>((int)NP.NPgetFeature(item,0), 00372 (int)NP.NPgetFeature(item,1)), 00373 &foo,PixRGB<float>(255),PixRGB<float>(0)); 00374 } 00375 } 00376 } 00377 //Raster::VisuRGB(outputClass,sformat("CLASS_%d.ppm",i)); 00378 } 00379 00380 //******************************************************* 00381 // stats on variance 00382 std::vector<float> meanInterClassVar(priorClasses,0.0F); 00383 std::vector<float> stdInterClassVar(priorClasses,0.0F); 00384 std::vector<int> nInterClassVar(priorClasses,0); 00385 00386 std::vector<float> meanInterClusterVar(sortCount,0.0F); 00387 std::vector<float> stdInterClusterVar(sortCount,0.0F); 00388 00389 LINFO("DOING STATS"); 00390 for(int i = 0; i < sortCount; i++) 00391 { 00392 int *thisClass = &sortClassMember[i]; 00393 if(NP.NPgetClassSize(*thisClass) > NP.NPgetMinClassSize()) 00394 { 00395 for(int j = 0; j < NP.NPgetClassSize(*thisClass); j++) 00396 { 00397 long item = NP.NPgetClass(*thisClass,j); 00398 for(int f = 0; f < featureCount; f++) 00399 { 00400 if(((int)features[f][0] == (int)NP.NPgetFeature(item,0)) && 00401 ((int)features[f][1] == (int)NP.NPgetFeature(item,1))) 00402 { 00403 if((NP.NPgetClassSize(*thisClass) > 0) && 00404 (priorClassSize[priorItemMember[f]] > 0)) 00405 { 00406 meanInterClassVar[priorItemMember[f]] += (float)i; 00407 stdInterClassVar[priorItemMember[f]] += pow((float)i,2); 00408 nInterClassVar[priorItemMember[f]]++; 00409 meanInterClusterVar[i] += (float)priorItemMember[f]; 00410 stdInterClusterVar[i] += pow((float) 00411 priorItemMember[f],2); 00412 } 00413 } 00414 } 00415 } 00416 } 00417 } 00418 float totalInterClassVar = 0.0F; 00419 float sumInterClassVar = 0.0F; 00420 float totalInterClusterVar = 0.0F; 00421 float sumInterClusterVar = 0.0F; 00422 for(int i = 0; i < priorClasses; i++) 00423 { 00424 if((nInterClassVar[i]-1) > 0) 00425 { 00426 meanInterClassVar[i] = meanInterClassVar[i]/nInterClassVar[i]; 00427 stdInterClassVar[i] = (stdInterClassVar[i]/(nInterClassVar[i])) 00428 - pow(meanInterClassVar[i],2); 00429 totalInterClassVar += stdInterClassVar[i]*nInterClassVar[i]; 00430 sumInterClassVar += nInterClassVar[i]; 00431 LINFO("INTERCLASS MEAN for class %d is %f",i,meanInterClassVar[i]); 00432 LINFO("INTERCLASS VAR for class %d is %f",i,stdInterClassVar[i]); 00433 LINFO("N = %d",priorClassSize[i]); 00434 } 00435 } 00436 LINFO("STATS 1 done"); 00437 float totalMeanInterClassVar; 00438 if(sumInterClassVar > 0) 00439 totalMeanInterClassVar = totalInterClassVar/sumInterClassVar; 00440 else 00441 totalMeanInterClassVar = 0; 00442 std::cout << "Total interclass variance is " 00443 << totalMeanInterClassVar << "\n"; 00444 00445 for(int i = 0; i < sortCount; i++) 00446 { 00447 int *thisClass = &sortClassMember[i]; 00448 if((NP.NPgetClassSize(*thisClass)-1) > 0) 00449 { 00450 meanInterClusterVar[i] = meanInterClusterVar[i] 00451 /NP.NPgetClassSize(*thisClass); 00452 stdInterClusterVar[i] = (stdInterClusterVar[i]/ 00453 (NP.NPgetClassSize(*thisClass))) 00454 - pow(meanInterClusterVar[i],2); 00455 totalInterClusterVar += stdInterClusterVar[i] 00456 *NP.NPgetClassSize(*thisClass); 00457 sumInterClusterVar += NP.NPgetClassSize(*thisClass); 00458 LINFO("INTERCLUSTER MEAN for class %d is %f", 00459 i,meanInterClusterVar[i]); 00460 LINFO("INTERCLUSTER VAR for class %d is %f", 00461 i,stdInterClusterVar[i]); 00462 LINFO("N = %d",NP.NPgetClassSize(*thisClass)); 00463 } 00464 } 00465 float totalMeanInterClusterVar; 00466 if(sumInterClusterVar > 0) 00467 totalMeanInterClusterVar = totalInterClusterVar/ 00468 sumInterClusterVar; 00469 else 00470 totalMeanInterClusterVar = 0; 00471 std::cout << "Total intercluster variance is " 00472 << totalMeanInterClusterVar << "\n"; 00473 LINFO("STATS 2 done"); 00474 //******************************************************* 00475 Raster::VisuRGB(classMap, 00476 sformat("%s.CLASS_MAP.ppm",argv[1])); 00477 Raster::VisuRGB(priorClassMap, 00478 sformat("%s.PRIOR_CLASS_MAP.ppm",argv[1])); 00479 } 00480 std::cerr << "a\n"; 00481 uint64 t2 = tim.get(); 00482 t0 = t2 - t0; 00483 if(NP.NP_doDensityMap == 1) 00484 Raster::VisuFloat(outputDensity,0,sformat("%s.outputDensity.pgm",argv[1])); 00485 std::cerr << "b\n"; 00486 if(NP.NP_doLinkMap == 1) 00487 Raster::VisuRGB(outputLinks, 00488 sformat("%s.outputLinks.ppm",argv[1])); 00489 LINFO("classify took %llums", t0); 00490 00491 //----------------------------------------------------------------// 00492 00493 //classify using Kmeans 00494 00495 //----------------------------------------------------------------// 00496 00497 00498 NP.NPresetSpace(featureCount,2); 00499 NP.NPaddSpace(features); 00500 unsigned int K = 4; 00501 float minDiff = 0.001F; 00502 unsigned int stopIter = 5; 00503 NP.NPclassifySpaceKmeans(&K,&minDiff,&stopIter); 00504 LINFO("Fetching Bounding Boxes"); 00505 BB = NP.NPgetBoundingBoxes(true); 00506 LINFO("Draw Bounding Box"); 00507 pix.setRed(255),pix.setGreen(255);pix.setBlue(0); 00508 NP.NPdrawBoundingBoxes(&BB,&outputLinks,0,0,pix,true); 00509 if(NP.NP_doLinkMap == 1) 00510 Raster::VisuRGB(outputLinks, 00511 sformat("%s.outputLinksK.ppm",argv[1])); 00512 // draw bounding boxes 00513 if(NP.NP_doClassMap == 1) 00514 { 00515 LINFO("Draw Class Images"); 00516 Image<PixRGB<float> > classMap; 00517 PixRGB<float> mrPixel; 00518 classMap.resize(finput.getWidth(),finput.getHeight()); 00519 findColorIndex FAC; 00520 00521 pix.setRed(255),pix.setGreen(0);pix.setBlue(0); 00522 for(int i = 0; i < NP.NPgetStemNumber(); i++) 00523 { 00524 unsigned int number; 00525 number = (unsigned)i + 1; 00526 //if(NP.NPisStem(i) == true) 00527 // number = 0; 00528 FAC.FACgetColor12(&number,&mrPixel); 00529 Image<PixRGB<float> > outputClass; 00530 outputClass.resize(finput.getWidth(),finput.getHeight()); 00531 std::cerr << "class size " << NP.NPgetClassSize(i) << "\n"; 00532 for(int j = 0; j < NP.NPgetClassSize(i); j++) 00533 { 00534 00535 long item = NP.NPgetClass(i,j); 00536 outputClass.setVal((int)NP.NPgetFeature(item,0) 00537 ,(int)NP.NPgetFeature(item,1),pix); 00538 classMap.setVal((int)NP.NPgetFeature(item,0) 00539 ,(int)NP.NPgetFeature(item,1),mrPixel); 00540 if(j == 0) 00541 { 00542 char foo; 00543 sprintf(&foo,"%d",(int)item); 00544 writeText(classMap, Point2D<int>((int)NP.NPgetFeature(item,0), 00545 (int)NP.NPgetFeature(item,1)), 00546 &foo,PixRGB<float>(255),PixRGB<float>(0)); 00547 } 00548 } 00549 //Raster::VisuRGB(outputClass,sformat("CLASS_%d.ppm",i)); 00550 } 00551 00552 //********************************************************** 00553 // draw cov matrix 00554 Image<float> final; 00555 final.resize(finput.getWidth(),finput.getWidth()); 00556 for(int i = 0; i < NP.NPgetStemNumber(); i++) 00557 { 00558 int classSize = NP.NPgetClassSize(i); 00559 if(NP.NPgetMinClassSize() <= classSize) 00560 { 00561 float t = 0.0F; 00562 float* tfloat = &t; 00563 std::vector<float> _vinput(classSize,0); 00564 std::vector<float*> _vTinput(classSize,tfloat); 00565 std::vector<std::vector<float> > NPclass(2,_vinput); 00566 std::vector<std::vector<float*> > NPclassP(2,_vTinput); 00567 for(int cs = 0; cs < classSize; cs++) 00568 { 00569 long item = NP.NPgetClass(i,cs); 00570 NPclass[0][cs] = finput.getWidth() - NP.NPgetFeature(item,0); 00571 std::cerr << "INPUTING " << NPclass[0][cs] << "\n"; 00572 NPclass[1][cs] = NP.NPgetFeature(item,1); 00573 std::cerr << "INPUTING " << NPclass[1][cs] << "\n"; 00574 NPclassP[0][cs] = &NPclass[0][cs]; 00575 NPclassP[1][cs] = &NPclass[1][cs]; 00576 } 00577 covHolder<float> covh; 00578 covh.resize(2,classSize,0.0F); 00579 covEstimate<float> CE(NPclassP,covh); 00580 CE.run(); 00581 final = CE.returnCovSlice(0,1,final); 00582 } 00583 } 00584 Raster::VisuFloat(final,0,sformat("%s.outputCovK.pgm",argv[1])); 00585 00586 //******************************************************* 00587 std::vector<float> meanInterClassVar(priorClasses,0.0F); 00588 std::vector<float> stdInterClassVar(priorClasses,0.0F); 00589 std::vector<int> nInterClassVar(priorClasses,0); 00590 00591 std::vector<float> meanInterClusterVar(K,0.0F); 00592 std::vector<float> stdInterClusterVar(K,0.0F); 00593 00594 00595 for(int i = 0; i < (signed)K; i++) 00596 { 00597 for(int j = 0; j < NP.NPgetClassSize(i); j++) 00598 { 00599 long item = NP.NPgetClass(i,j); 00600 for(int f = 0; f < featureCount; f++) 00601 { 00602 if(((int)features[f][0] == (int)NP.NPgetFeature(item,0)) && 00603 ((int)features[f][1] == (int)NP.NPgetFeature(item,1))) 00604 { 00605 if((NP.NPgetClassSize(i) > 0) && 00606 (priorClassSize[priorItemMember[f]] > 0)) 00607 { 00608 meanInterClassVar[priorItemMember[f]] += (float)i; 00609 stdInterClassVar[priorItemMember[f]] += pow((float)i,2); 00610 nInterClassVar[priorItemMember[f]]++; 00611 meanInterClusterVar[i] += (float)priorItemMember[f]; 00612 stdInterClusterVar[i] += pow((float) 00613 priorItemMember[f],2); 00614 } 00615 } 00616 } 00617 } 00618 } 00619 float totalInterClassVar = 0.0F; 00620 float sumInterClassVar = 0.0F; 00621 float totalInterClusterVar = 0.0F; 00622 float sumInterClusterVar = 0.0F; 00623 for(int i = 0; i < priorClasses; i++) 00624 { 00625 if((nInterClassVar[i]-1) > 0) 00626 { 00627 meanInterClassVar[i] = meanInterClassVar[i]/nInterClassVar[i]; 00628 stdInterClassVar[i] = (stdInterClassVar[i]/(nInterClassVar[i])) 00629 - pow(meanInterClassVar[i],2); 00630 totalInterClassVar += stdInterClassVar[i]*nInterClassVar[i]; 00631 sumInterClassVar += nInterClassVar[i]; 00632 LINFO("K INTERCLASS MEAN for class %d is %f",i,meanInterClassVar[i]); 00633 LINFO("K INTERCLASS VAR for class %d is %f",i,stdInterClassVar[i]); 00634 LINFO("K N = %d",priorClassSize[i]); 00635 } 00636 } 00637 float totalMeanInterClassVar = totalInterClassVar/sumInterClassVar; 00638 std::cout << "K Total interclass variance is " 00639 << totalMeanInterClassVar << "\n"; 00640 00641 for(int i = 0; i < (signed)K; i++) 00642 { 00643 if((NP.NPgetClassSize(i)-1) > 0) 00644 { 00645 meanInterClusterVar[i] = meanInterClusterVar[i] 00646 /NP.NPgetClassSize(i); 00647 stdInterClusterVar[i] = (stdInterClusterVar[i]/ 00648 (NP.NPgetClassSize(i))) 00649 - pow(meanInterClusterVar[i],2); 00650 totalInterClusterVar += stdInterClusterVar[i] 00651 *NP.NPgetClassSize(i); 00652 sumInterClusterVar += NP.NPgetClassSize(i); 00653 LINFO("K INTERCLUSTER MEAN for class %d is %f", 00654 i,meanInterClusterVar[i]); 00655 LINFO("K INTERCLUSTER VAR for class %d is %f", 00656 i,stdInterClusterVar[i]); 00657 LINFO("K N = %d",NP.NPgetClassSize(i)); 00658 } 00659 } 00660 float totalMeanInterClusterVar = totalInterClusterVar/ 00661 sumInterClusterVar; 00662 std::cout << "K Total intercluster variance is " 00663 << totalMeanInterClusterVar << "\n"; 00664 Raster::VisuRGB(classMap, 00665 sformat("%s.CLASS_MAP_K.ppm",argv[1])); 00666 } 00667 } 00668 00669 00670 00671 00672 00673 // ###################################################################### 00674 /* So things look consistent in everyone's emacs... */ 00675 /* Local Variables: */ 00676 /* indent-tabs-mode: nil */ 00677 /* End: */