00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "Image/OpenCVUtil.H"
00044
00045
00046 #include "Neuro/GistEstimatorTexton.H"
00047
00048
00049 #include "Neuro/StdBrain.H"
00050 #include "Neuro/NeuroOpts.H"
00051 #include "Neuro/NeuroSimEvents.H"
00052
00053 #include "Media/SimFrameSeries.H"
00054 #include "Media/MediaOpts.H"
00055
00056 #include "Simulation/SimEventQueue.H"
00057 #include "Simulation/SimEventQueueConfigurator.H"
00058
00059 #include "Channels/ChannelOpts.H"
00060 #include "Component/ModelManager.H"
00061 #include "Component/ModelOptionDef.H"
00062
00063 #include "Image/Point2D.H"
00064
00065 #include "nub/ref.h"
00066
00067 #ifndef HAVE_OPENCV // fake OpenCV API so as to not break builds
00068 namespace {
00069
00070 struct CvMat {int rows, cols, type ;} ;
00071
00072 inline CvMat* cvCreateMat(int, int, int) {return 0 ;}
00073 inline void cvZero(CvMat *) {}
00074 inline void cvReleaseMat(CvMat**) {}
00075 inline double cvmGet(CvMat*, int, int) {return 0 ;}
00076 inline void cvmSet(CvMat*, int, int, double) {}
00077 inline int cvTermCriteria(int, int, double) {return 0 ;}
00078 inline void cvKMeans2(CvMat*, int, CvMat*, int) {}
00079
00080 #define CV_32FC1 0
00081 #define CV_32SC1 0
00082 inline int CV_MAT_TYPE(int) {return 0 ;}
00083 #define CV_MAT_ELEM(matrix, type, row, col) (type(0))
00084
00085 #define CV_TERMCRIT_EPS 0
00086 #define CV_TERMCRIT_ITER 0
00087
00088 }
00089
00090 #endif // OpenCV availability check
00091
00092
00093 #include <fstream>
00094 #include <sstream>
00095 #include <ios>
00096 #include <numeric>
00097 #include <algorithm>
00098 #include <functional>
00099 #include <map>
00100 #include <vector>
00101 #include <iterator>
00102 #include <stdexcept>
00103 #include <utility>
00104 #include <limits>
00105 #include <cmath>
00106
00107
00108
00109 namespace {
00110
00111
00112
00113
00114
00115
00116 template<typename T>
00117 std::string to_string(const T& t)
00118 {
00119 std::ostringstream str ;
00120 str << t ;
00121 return str.str() ;
00122 }
00123
00124
00125 int count_lines(const std::string& file_name)
00126 {
00127 int n = -1 ;
00128 std::ifstream ifs(file_name.c_str()) ;
00129
00130 std::string dummy ;
00131 while (ifs) {
00132 getline(ifs, dummy) ;
00133 ++n ;
00134 }
00135 return n ;
00136 }
00137
00138
00139 bool is_zero(double d)
00140 {
00141 return std::fabs(d) <= std::numeric_limits<double>::epsilon() ;
00142 }
00143
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 namespace {
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 typedef GistEstimatorTexton::ImageType Texton ;
00187
00188
00189
00190
00191 class textons_accumulator {
00192 static std::string out_file ;
00193
00194 textons_accumulator() ;
00195 ~textons_accumulator() ;
00196 public :
00197 static void output_file(const std::string& file_name) ;
00198 static void write(const Texton&) ;
00199 } ;
00200
00201
00202
00203
00204 std::string textons_accumulator::out_file ;
00205
00206
00207
00208
00209 void textons_accumulator::output_file(const std::string& file_name)
00210 {
00211 out_file = file_name ;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 void textons_accumulator::write(const Texton& textons)
00232 {
00233 if (out_file.empty())
00234 throw std::runtime_error("textons accumulator output file "
00235 "not specified") ;
00236
00237 std::ofstream ofs(out_file.c_str(), std::ios::out | std::ios::app) ;
00238 for (int y = 0; y < textons.getHeight(); ++y) {
00239 for (int x = 0; x < textons.getWidth(); ++x)
00240 ofs << textons.getVal(x, y) << ' ' ;
00241 ofs << '\n' ;
00242 }
00243 }
00244
00245
00246
00247
00248
00249
00250 void accumulate_textons(const Texton& textons)
00251 {
00252 textons_accumulator::write(textons) ;
00253 }
00254
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 namespace {
00268
00269
00270 class OpenCVMatrix {
00271 CvMat* matrix ;
00272 public :
00273 OpenCVMatrix(int num_rows, int num_cols, int type) ;
00274 OpenCVMatrix(CvMat*) ;
00275 ~OpenCVMatrix() ;
00276
00277 int num_rows() const {return matrix->rows ;}
00278 int num_cols() const {return matrix->cols ;}
00279 int type() const {return CV_MAT_TYPE(matrix->type) ;}
00280
00281 template<typename T>
00282 T get(int i, int j) const {return CV_MAT_ELEM(*matrix, T, i, j) ;}
00283
00284 operator CvMat*() const {return matrix ;}
00285 } ;
00286
00287 OpenCVMatrix::OpenCVMatrix(int num_rows, int num_cols, int type)
00288 : matrix(cvCreateMat(num_rows, num_cols, type))
00289 {
00290 if (! matrix)
00291 throw std::runtime_error("unable to create OpenCV matrix") ;
00292 }
00293
00294 OpenCVMatrix::OpenCVMatrix(CvMat* M)
00295 : matrix(M)
00296 {
00297 if (! matrix)
00298 throw std::runtime_error("cannot create empty/null matrix") ;
00299 }
00300
00301 OpenCVMatrix::~OpenCVMatrix()
00302 {
00303 cvReleaseMat(& matrix) ;
00304 }
00305
00306
00307
00308 CvMat* load_training_textons(const std::string& file_name, int num_lines)
00309 {
00310 CvMat* M =
00311 cvCreateMat(num_lines, GistEstimatorTexton::NUM_FILTERS, CV_32FC1) ;
00312
00313 double d ;
00314 std::ifstream ifs(file_name.c_str()) ;
00315 for (int i = 0; i < num_lines; ++i)
00316 for (int j = 0; j < int(GistEstimatorTexton::NUM_FILTERS); ++j) {
00317 if (! ifs) {
00318 cvReleaseMat(& M) ;
00319 throw std::runtime_error(file_name + ": out of data?!?") ;
00320 }
00321 ifs >> d ;
00322 cvmSet(M, i, j, d) ;
00323 }
00324
00325 return M ;
00326 }
00327
00328
00329
00330
00331 CvMat* compute_centroids(int K, const OpenCVMatrix& data,
00332 const OpenCVMatrix& cluster_assignments)
00333 {
00334 CvMat* centroids = cvCreateMat(K, data.num_cols(), data.type()) ;
00335 cvZero(centroids) ;
00336
00337 std::vector<int> cluster_counts(K) ;
00338 std::fill(cluster_counts.begin(), cluster_counts.end(), 0) ;
00339
00340 for (int i = 0; i < data.num_rows(); ++i)
00341 {
00342 int C = cluster_assignments.get<int>(i, 0) ;
00343 ++cluster_counts[C] ;
00344
00345
00346 for (int j = 0; j < data.num_cols(); ++j)
00347 cvmSet(centroids, C, j,
00348 cvmGet(centroids, C, j) + data.get<float>(i, j)) ;
00349 }
00350
00351
00352
00353 for (int C = 0; C < K; ++C)
00354 for (int j = 0; j < data.num_cols(); ++j)
00355 cvmSet(centroids, C, j,
00356 cvmGet(centroids, C, j) / cluster_counts[C]) ;
00357
00358 return centroids ;
00359 }
00360
00361
00362 #ifndef TT_KMEANS_ITERATIONS
00363 #define TT_KMEANS_ITERATIONS (100)
00364 #endif
00365 #ifndef TT_KMEANS_PRECISION
00366 #define TT_KMEANS_PRECISION (.01)
00367 #endif
00368
00369
00370
00371 CvMat* kmeans(int K, const OpenCVMatrix& data)
00372 {
00373 OpenCVMatrix cluster_assignments(data.num_rows(), 1, CV_32SC1) ;
00374
00375 LINFO("MVN: computing K-means cluster assignments with OpenCV") ;
00376 cvKMeans2(data, K, cluster_assignments,
00377 cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER,
00378 TT_KMEANS_ITERATIONS, TT_KMEANS_PRECISION)) ;
00379
00380 LINFO("MVN: cluster assignments done; computing centroids...") ;
00381 return compute_centroids(K, data, cluster_assignments) ;
00382 }
00383
00384
00385 void save_universal_textons(const OpenCVMatrix& universal_textons,
00386 const std::string& file_name)
00387 {
00388 std::ofstream ofs(file_name.c_str()) ;
00389 for (int i = 0; i < universal_textons.num_rows(); ++i) {
00390 for (int j = 0; j < universal_textons.num_cols(); ++j)
00391 ofs << universal_textons.get<float>(i, j) << ' ' ;
00392 ofs << '\n' ;
00393 }
00394 }
00395
00396
00397 Texton load_universal_textons(const std::string& file_name)
00398 {
00399 const int M = count_lines(file_name) ;
00400 const int N = GistEstimatorTexton::NUM_FILTERS ;
00401 Texton U(N, M, ZEROS) ;
00402
00403 float f ;
00404 std::ifstream ifs(file_name.c_str()) ;
00405 for (int j = 0; j < M; ++j)
00406 for (int i = 0; i < N; ++i) {
00407 if (! ifs)
00408 throw std::runtime_error(file_name + ": out of data?!?") ;
00409 ifs >> f ;
00410 U.setVal(i, j, f) ;
00411 }
00412
00413 return U ;
00414 }
00415
00416
00417
00418
00419
00420
00421
00422 #ifndef TT_NUM_UNIVERSAL_TEXTONS
00423 #define TT_NUM_UNIVERSAL_TEXTONS 100
00424 #endif
00425
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435 namespace {
00436
00437
00438 typedef Image<double> Histogram ;
00439 typedef std::map<std::string, Histogram> HistogramMap ;
00440 typedef HistogramMap::value_type HistogramMapEntry ;
00441
00442
00443
00444
00445
00446
00447
00448
00449 void save_histogram(const Histogram& histogram,
00450 const std::string& hist_name,
00451 const std::string& file_name)
00452 {
00453 LINFO("MVN: saving histogram %s to %s",
00454 hist_name.c_str(), file_name.c_str()) ;
00455 std::ofstream ofs(file_name.c_str(), std::ios::out | std::ios::app) ;
00456 ofs << hist_name << ' ' ;
00457 for (int y = 0; y < histogram.getHeight(); ++y)
00458 for (int x = 0; x < histogram.getWidth(); ++x)
00459 ofs << histogram.getVal(x, y) << ' ' ;
00460 ofs << '\n' ;
00461 }
00462
00463
00464
00465
00466
00467 HistogramMap load_training_histograms(const std::string& file_name)
00468 {
00469 HistogramMap histograms ;
00470
00471 std::ifstream ifs(file_name.c_str()) ;
00472 for(;;)
00473 {
00474 std::string str ;
00475 std::getline(ifs, str) ;
00476 if (! ifs || str.empty())
00477 break ;
00478 std::istringstream line(str) ;
00479
00480 std::string histogram_name ;
00481 line >> histogram_name ;
00482
00483 Histogram H(TT_NUM_UNIVERSAL_TEXTONS, 1, ZEROS) ;
00484 double d ; int i = 0 ;
00485 while (line >> d)
00486 H.setVal(i++, 0, d) ;
00487
00488 histograms.insert(std::make_pair(histogram_name, H)) ;
00489 }
00490
00491 return histograms ;
00492 }
00493
00494 }
00495
00496
00497
00498
00499
00500
00501
00502 namespace {
00503
00504
00505
00506
00507
00508
00509 typedef std::pair<std::string, double> HistogramDistance ;
00510
00511
00512
00513 bool chi_square_cmp(const HistogramDistance& L, const HistogramDistance& R)
00514 {
00515 return L.second < R.second ;
00516 }
00517
00518
00519
00520
00521 std::ostream& operator<<(std::ostream& os, const HistogramDistance& D)
00522 {
00523 return os << D.first ;
00524 }
00525
00526
00527
00528
00529 class chi_square {
00530 const Histogram& input ;
00531 double distance(const Histogram&, const Histogram&) const ;
00532 public :
00533 chi_square(const Histogram& H) ;
00534 HistogramDistance operator()(const HistogramMapEntry& E) const {
00535 return std::make_pair(E.first, distance(input, E.second)) ;
00536 }
00537 } ;
00538
00539 chi_square::chi_square(const Histogram& H)
00540 : input(H)
00541 {}
00542
00543 double chi_square::distance(const Histogram& L, const Histogram& R) const
00544 {
00545 const int n = L.getWidth() ;
00546 double sum = 0 ;
00547 for (int i = 0; i < n; ++i)
00548 {
00549 double l = L.getVal(i, 0) ;
00550 double r = R.getVal(i, 0) ;
00551 double l_minus_r = l - r ;
00552 double l_plus_r = l + r ;
00553 if (is_zero(l_minus_r) || is_zero(l_plus_r))
00554 continue ;
00555 sum += (l_minus_r * l_minus_r)/l_plus_r ;
00556 }
00557 return sum/2 ;
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 void classify_image(const HistogramMapEntry& input,
00571 const HistogramMap& training_histograms,
00572 const std::string& results_file)
00573 {
00574 std::vector<HistogramDistance> chi_square_distances ;
00575 std::transform(training_histograms.begin(), training_histograms.end(),
00576 std::back_inserter(chi_square_distances),
00577 chi_square(input.second)) ;
00578 std::sort(chi_square_distances.begin(), chi_square_distances.end(),
00579 chi_square_cmp) ;
00580
00581 std::ofstream ofs(results_file.c_str(), std::ios::out | std::ios::app) ;
00582 ofs << input.first << ' ' ;
00583
00584
00585 for (unsigned int i = 0; i < chi_square_distances.size() && i < 5; ++i)
00586 ofs << chi_square_distances[i] << ' ' ;
00587 ofs << '\n' ;
00588 }
00589
00590 }
00591
00592
00593
00594
00595
00596
00597
00598
00599 namespace {
00600
00601 const ModelOptionCateg MOC_TEXTONS = {
00602 MOC_SORTPRI_3,
00603 "Options specific to the Renninger-Malik textons program",
00604 } ;
00605
00606
00607
00608 #ifndef TT_DEFAULT_TRAINING_TEXTONS_FILE
00609 #define TT_DEFAULT_TRAINING_TEXTONS_FILE "training_textons.txt"
00610 #endif
00611
00612 const ModelOptionDef OPT_TrainingTextons = {
00613 MODOPT_ARG_STRING, "TrainingTextons", & MOC_TEXTONS, OPTEXP_CORE,
00614 "This option specifies the name of the file where training textons\n"
00615 "should be accumulated or read from. This is a plain text file containing\n"
00616 "the training textons matrix that will be fed into the K-means procedure\n"
00617 "during the texton training phase. Each line of this file will contain a\n"
00618 "row of training textons.\n",
00619 "training-textons", '\0', "training-textons-file",
00620 TT_DEFAULT_TRAINING_TEXTONS_FILE,
00621 } ;
00622
00623
00624
00625 #ifndef TT_DEFAULT_UNIVERSAL_TEXTONS_FILE
00626 #define TT_DEFAULT_UNIVERSAL_TEXTONS_FILE "universal_textons.txt"
00627 #endif
00628
00629 const ModelOptionDef OPT_UniversalTextons = {
00630 MODOPT_ARG_STRING, "UniversalTextons", & MOC_TEXTONS, OPTEXP_CORE,
00631 "This option specifies the name of the file in which the universal\n"
00632 "textons are (or are to be) stored. This is a plain text file containing\n"
00633 "the universal_textons matrix that is used for image classification.\n",
00634 "universal-textons", '\0', "universal-textons-file",
00635 TT_DEFAULT_UNIVERSAL_TEXTONS_FILE,
00636 } ;
00637
00638
00639
00640
00641
00642
00643
00644
00645 #ifndef TT_DEFAULT_TRAINING_HISTOGRAM_NAME
00646 #define TT_DEFAULT_TRAINING_HISTOGRAM_NAME "training_image"
00647 #endif
00648
00649 const ModelOptionDef OPT_HistogramName = {
00650 MODOPT_ARG_STRING, "HistogramName", & MOC_TEXTONS, OPTEXP_CORE,
00651 "This option specifies the \"root\" name of the histogram entry in\n"
00652 "the training histograms database. The histogram number will be\n"
00653 "appended to this \"root\" name. The training histograms database\n"
00654 "is a plain text file containing one histogram entry per line. The\n"
00655 "first field specifies the name plus number of the entry (e.g.,\n"
00656 "foo_1, foo_2, bar_1, and so on). The remaining fields are simply the\n"
00657 "hundred numbers making up the image's universal textons histogram.\n\n"
00658 "In classification mode, this option specifies the name of the input\n"
00659 "image's histogram that is written to the results file.\n",
00660 "histogram-name", '\0', "histogram-name-root",
00661 TT_DEFAULT_TRAINING_HISTOGRAM_NAME,
00662 } ;
00663
00664 #ifndef TT_DEFAULT_TRAINING_HISTOGRAMS_FILE
00665 #define TT_DEFAULT_TRAINING_HISTOGRAMS_FILE "training_histograms.txt"
00666 #endif
00667
00668 const ModelOptionDef OPT_HistogramFile = {
00669 MODOPT_ARG_STRING, "HistogramFile", & MOC_TEXTONS, OPTEXP_CORE,
00670 "This option specifies the name of the training histograms database,\n"
00671 "a plain text file containing one histogram entry per line. The\n"
00672 "first field specifies the name plus number of the entry (e.g.,\n"
00673 "foo_1, foo_2, bar_1, and so on). The remaining fields are simply the\n"
00674 "hundred numbers making up the image's universal textons histogram.\n",
00675 "histogram-file", '\0', "training-histograms-file",
00676 TT_DEFAULT_TRAINING_HISTOGRAMS_FILE,
00677 } ;
00678
00679
00680
00681 #ifndef TT_DEFAULT_CLASSIFICATION_RESULTS_FILE
00682 #define TT_DEFAULT_CLASSIFICATION_RESULTS_FILE "texton_classifications.txt"
00683 #endif
00684
00685 const ModelOptionDef OPT_ResultsFile = {
00686 MODOPT_ARG_STRING, "ResultsFile", & MOC_TEXTONS, OPTEXP_CORE,
00687 "This option specifies the name of the classification results file,\n"
00688 "a plain text file containing one result entry per line. The first\n"
00689 "field specifies the name of the input image plus number of the entry,\n"
00690 "(e.g., foo_1, foo_2, bar_1, and so on). Then come the names of the\n"
00691 "top five matching images from the training set.\n",
00692 "results-file", '\0', "classification-results-file",
00693 TT_DEFAULT_CLASSIFICATION_RESULTS_FILE,
00694 } ;
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724 #ifndef TT_ACCUMULATE_CMD
00725 #define TT_ACCUMULATE_CMD "accumulate"
00726 #endif
00727 #ifndef TT_KMEANS_CMD
00728 #define TT_KMEANS_CMD "kmeans"
00729 #endif
00730 #ifndef TT_HISTOGRAM_CMD
00731 #define TT_HISTOGRAM_CMD "histogram"
00732 #endif
00733 #ifndef TT_CLASSIFY_CMD
00734 #define TT_CLASSIFY_CMD "classify"
00735 #endif
00736
00737
00738 #ifndef TT_ACTIONS
00739 #define TT_ACTIONS ("{"TT_ACCUMULATE_CMD"|"TT_KMEANS_CMD"|"\
00740 TT_HISTOGRAM_CMD"|"TT_CLASSIFY_CMD"}")
00741 #endif
00742
00743 }
00744
00745
00746
00747
00748
00749
00750 namespace {
00751
00752 class TextonSimulation {
00753 ModelManager model_manager ;
00754 nub::soft_ref<SimEventQueueConfigurator> configurator ;
00755 nub::soft_ref<StdBrain> brain ;
00756 nub::ref<SimInputFrameSeries> input_frame_series ;
00757
00758
00759 OModelParam<std::string> training_option ;
00760 OModelParam<std::string> universal_option ;
00761 OModelParam<std::string> hist_name_option ;
00762 OModelParam<std::string> hist_file_option ;
00763 OModelParam<std::string> results_option ;
00764
00765 public :
00766 TextonSimulation(const std::string& model_name) ;
00767 void parse_command_line(int argc, const char* argv[]) ;
00768 void run() ;
00769 ~TextonSimulation() ;
00770
00771 private :
00772
00773 typedef void (TextonSimulation::*Action)() ;
00774 typedef std::map<std::string, Action> ActionMap ;
00775 ActionMap action_map ;
00776
00777 void accumulate_training_textons() ;
00778 void compute_universal_textons() ;
00779 void compute_training_histograms() ;
00780 void classify_input_images() ;
00781
00782
00783 std::string training_textons_file() {return training_option.getVal() ;}
00784 std::string universal_textons_file() {return universal_option.getVal() ;}
00785 std::string histogram_name() {return hist_name_option.getVal() ;}
00786 std::string histogram_file() {return hist_file_option.getVal() ;}
00787 std::string results_file() {return results_option.getVal() ;}
00788 } ;
00789
00790
00791
00792 TextonSimulation::TextonSimulation(const std::string& model_name)
00793 : model_manager(model_name),
00794 configurator(new SimEventQueueConfigurator(model_manager)),
00795 brain(new StdBrain(model_manager)),
00796 input_frame_series(new SimInputFrameSeries(model_manager)),
00797 training_option(& OPT_TrainingTextons, & model_manager),
00798 universal_option(& OPT_UniversalTextons, & model_manager),
00799 hist_name_option(& OPT_HistogramName, & model_manager),
00800 hist_file_option(& OPT_HistogramFile, & model_manager),
00801 results_option(& OPT_ResultsFile, & model_manager)
00802 {
00803 model_manager.addSubComponent(configurator) ;
00804 model_manager.addSubComponent(brain) ;
00805 model_manager.addSubComponent(input_frame_series) ;
00806
00807 typedef TextonSimulation me ;
00808 action_map[TT_ACCUMULATE_CMD] = & me::accumulate_training_textons ;
00809 action_map[TT_KMEANS_CMD] = & me::compute_universal_textons ;
00810 action_map[TT_HISTOGRAM_CMD] = & me::compute_training_histograms ;
00811 action_map[TT_CLASSIFY_CMD] = & me::classify_input_images ;
00812 }
00813
00814
00815
00816
00817
00818 void TextonSimulation::parse_command_line(int argc, const char* argv[])
00819 {
00820 model_manager.setOptionValString(& OPT_SingleChannelSaveRawMaps, "true") ;
00821 model_manager.setOptionValString(& OPT_GistEstimatorType, "Texton") ;
00822 model_manager.setOptionValString(& OPT_NumOrientations, "6") ;
00823
00824 model_manager.setOptionValString(& OPT_TrainingTextons,
00825 TT_DEFAULT_TRAINING_TEXTONS_FILE) ;
00826 model_manager.setOptionValString(& OPT_UniversalTextons,
00827 TT_DEFAULT_UNIVERSAL_TEXTONS_FILE) ;
00828
00829 model_manager.setOptionValString(& OPT_HistogramName,
00830 TT_DEFAULT_TRAINING_HISTOGRAM_NAME) ;
00831 model_manager.setOptionValString(& OPT_HistogramFile,
00832 TT_DEFAULT_TRAINING_HISTOGRAMS_FILE) ;
00833
00834 model_manager.setOptionValString(& OPT_ResultsFile,
00835 TT_DEFAULT_CLASSIFICATION_RESULTS_FILE) ;
00836
00837 if (! model_manager.parseCommandLine(argc, argv, TT_ACTIONS, 1, 1))
00838 throw std::runtime_error("command line parse error") ;
00839 }
00840
00841
00842
00843 void TextonSimulation::run()
00844 {
00845 std::string cmd(model_manager.getExtraArg(0)) ;
00846 ActionMap::iterator action = action_map.find(cmd) ;
00847 if (action == action_map.end())
00848 throw std::runtime_error(cmd + ": sorry, unknown action") ;
00849 (this->*(action->second))() ;
00850 }
00851
00852
00853
00854
00855
00856 class ModelManagerStarter {
00857 ModelManager& mgr ;
00858 public :
00859 ModelManagerStarter(ModelManager& m) : mgr(m) {mgr.start() ;}
00860 ~ModelManagerStarter() {mgr.stop() ;}
00861 } ;
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874 void TextonSimulation::accumulate_training_textons()
00875 {
00876 ModelManagerStarter M(model_manager) ;
00877
00878 LFATAL("sorry, this gist program is broken and needs to be fixed") ;
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904 }
00905
00906
00907
00908
00909 void TextonSimulation::compute_universal_textons()
00910 {
00911 LINFO("MVN: counting lines in %s", training_textons_file().c_str()) ;
00912 int num_rows = count_lines(training_textons_file()) ;
00913
00914 LINFO("MVN: reading %d training textons from %s",
00915 num_rows, training_textons_file().c_str()) ;
00916 OpenCVMatrix training_textons =
00917 load_training_textons(training_textons_file(), num_rows) ;
00918
00919 const int K = TT_NUM_UNIVERSAL_TEXTONS ;
00920 LINFO("MVN: doing K-means on training textons to get %d clusters", K) ;
00921 OpenCVMatrix universal_textons = kmeans(K, training_textons) ;
00922
00923 LINFO("MVN: K-means done; saving universal textons to %s",
00924 universal_textons_file().c_str()) ;
00925 save_universal_textons(universal_textons, universal_textons_file()) ;
00926 }
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936 void TextonSimulation::compute_training_histograms()
00937 {
00938 ModelManagerStarter M(model_manager) ;
00939
00940 LFATAL("sorry, this gist program is broken and needs to be fixed") ;
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976 }
00977
00978
00979
00980
00981
00982 void TextonSimulation::classify_input_images()
00983 {
00984 ModelManagerStarter M(model_manager) ;
00985
00986 LFATAL("sorry, this gist program is broken and needs to be fixed") ;
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028 }
01029
01030
01031
01032
01033 TextonSimulation::~TextonSimulation() {}
01034
01035 }
01036
01037
01038
01039 #ifdef HAVE_OPENCV
01040
01041 int main(int argc, const char* argv[])
01042 {
01043 MYLOGVERB = LOG_INFO ;
01044 try
01045 {
01046 TextonSimulation S("train-texton Model") ;
01047 S.parse_command_line(argc, argv) ;
01048 S.run() ;
01049 }
01050 catch (std::exception& e)
01051 {
01052 LFATAL("%s", e.what()) ;
01053 return 1 ;
01054 }
01055 return 0 ;
01056 }
01057
01058 #else
01059
01060 int main()
01061 {
01062 LINFO("Sorry, this program needs OpenCV.") ;
01063 return 1 ;
01064 }
01065
01066 #endif
01067
01068
01069
01070
01071
01072
01073