automatic commit
[folded-ctf.git] / labelled_image.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 "labelled_image.h"
27
28 LabelledImage::LabelledImage() : RichImage() {
29   _target_poses = 0;
30 }
31
32 LabelledImage::LabelledImage(int width, int height, int nb_targets) : RichImage(width, height) {
33   _nb_targets = nb_targets;
34   _target_poses = new Pose[_nb_targets];
35 }
36
37 LabelledImage::~LabelledImage() {
38   delete[] _target_poses;
39 }
40
41 int LabelledImage::pose_cell_label(PoseCell *cell) {
42   int positive = 0;
43   int negative = 1;
44
45   for(int t = 0; t < _nb_targets; t++) {
46     if(cell->contains(_target_poses + t))
47       positive = 1;
48     if(!cell->negative_for_train(_target_poses + t))
49       negative = 0;
50   }
51
52   if(positive) return 1;
53   if(negative) return -1;
54   return 0;
55 }
56
57 void LabelledImage::write(ostream *out) {
58   int v = file_format_version;
59   write_var(out, &v);
60   RichImage::write(out);
61   write_var(out, &_nb_targets);
62   for(int t = 0; t < _nb_targets; t++)
63     _target_poses[t].write(out);
64 }
65
66 void LabelledImage::read(istream *in) {
67   int v;
68   read_var(in, &v);
69   if(v != file_format_version) {
70     cerr << "Pool file format version " << file_format_version << " expected,"
71          << " the file is version " << v
72          << endl;
73     exit(1);
74   }
75   RichImage::read(in);
76   delete[] _target_poses;
77   read_var(in, &_nb_targets);
78   _target_poses = new Pose[_nb_targets];
79   for(int t = 0; t < _nb_targets; t++)
80     _target_poses[t].read(in);
81 }