automatic commit
[folded-ctf.git] / sample_set.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
16  *  folded-ctf is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 #include "sample_set.h"
27
28 SampleSet::SampleSet(int nb_features, int nb_samples) {
29   _nb_features = nb_features;
30   _nb_samples = nb_samples;
31   _shared_feature_values = new SharedResponses(_nb_features, _nb_samples);
32   _shared_feature_values->grab();
33
34   _labels = new int[_nb_samples];
35   _feature_values = new scalar_t *[_nb_samples];
36   for(int s = 0; s < _nb_samples; s++)
37     _feature_values[s] = _shared_feature_values->_responses + _nb_features * s;
38
39 }
40
41 SampleSet::SampleSet(SampleSet *father, int nb, int *indexes) {
42   _nb_features = father->_nb_features;
43   _nb_samples = nb;
44   _shared_feature_values = father->_shared_feature_values;
45   _shared_feature_values->grab();
46
47   _labels = new int[_nb_samples];
48   _feature_values = new scalar_t *[_nb_samples];
49   for(int s = 0; s < _nb_samples; s++) {
50     _feature_values[s] = father->_feature_values[indexes[s]];
51     _labels[s] = father->_labels[indexes[s]];
52   }
53 }
54
55 SampleSet::~SampleSet() {
56   _shared_feature_values->release();
57   delete[] _feature_values;
58   delete[] _labels;
59 }
60
61 void SampleSet::set_sample(int n,
62                            PiFeatureFamily *pi_feature_family,
63                            RichImage *image,
64                            PoseCell *cell, int label) {
65   ASSERT(n >= 0 && n < _nb_samples);
66   _labels[n] = label;
67   PiReferential referential(cell);
68   for(int f = 0; f < _nb_features; f++) {
69     _feature_values[n][f] = pi_feature_family->get_feature(f)->response(image, &referential);
70     ASSERT(!isnan(_feature_values[n][f]));
71   }
72 }