d02313d0c31d7a607abc06fe19e6c3b38c92538f
[folded-ctf.git] / labelled_image.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "labelled_image.h"
20
21 LabelledImage::LabelledImage() : RichImage() {
22   _target_poses = 0;
23 }
24
25 LabelledImage::LabelledImage(int width, int height, int nb_targets) : RichImage(width, height) {
26   _nb_targets = nb_targets;
27   _target_poses = new Pose[_nb_targets];
28 }
29
30 LabelledImage::~LabelledImage() {
31   delete[] _target_poses;
32 }
33
34 int LabelledImage::pose_cell_label(PoseCell *cell) {
35   int positive = 0;
36   int negative = 1;
37
38   for(int t = 0; t < _nb_targets; t++) {
39     if(cell->contains(_target_poses + t))
40       positive = 1;
41     if(!cell->negative_for_train(_target_poses + t))
42       negative = 0;
43   }
44
45   if(positive) return 1;
46   if(negative) return -1;
47   return 0;
48 }
49
50 void LabelledImage::crop(int xmin, int ymin, int width, int height) {
51   RichImage::crop(xmin, ymin, width, height);
52   for(int t = 0; t < _nb_targets; t++) {
53     _target_poses[t].translate(- xmin, - ymin);
54   }
55 }
56
57 void LabelledImage::reduce() {
58   int xmin = _width, xmax = 0, ymin = _height, ymax = 0;
59   if(_nb_targets > 0) {
60     for(int t = 0; t < _nb_targets; t++) {
61       xmin = min(xmin, int(_target_poses[t]._bounding_box_xmin));
62       ymin = min(ymin, int(_target_poses[t]._bounding_box_ymin));
63       xmax = max(xmax, int(_target_poses[t]._bounding_box_xmax));
64       ymax = max(ymax, int(_target_poses[t]._bounding_box_ymax));
65     }
66   } else {
67     xmin = 0; ymin = 0;
68     xmax = 640; ymax = 480;
69   }
70   xmin = max(0, xmin);
71   ymin = max(0, ymin);
72   xmax = min(_width, xmax);
73   ymax = min(_height, ymax);
74   crop(xmin, ymin, xmax - xmin, ymax - ymin);
75 }
76
77 void LabelledImage::write(ostream *out) {
78   int v = file_format_version;
79   write_var(out, &v);
80   RichImage::write(out);
81   write_var(out, &_nb_targets);
82   for(int t = 0; t < _nb_targets; t++)
83     _target_poses[t].write(out);
84 }
85
86 void LabelledImage::read(istream *in) {
87   int v;
88   read_var(in, &v);
89   if(v != file_format_version) {
90     cerr << "Pool file format version " << file_format_version << " expected,"
91          << " the file is version " << v
92          << endl;
93     exit(1);
94   }
95   RichImage::read(in);
96   delete[] _target_poses;
97   read_var(in, &_nb_targets);
98   _target_poses = new Pose[_nb_targets];
99   for(int t = 0; t < _nb_targets; t++)
100     _target_poses[t].read(in);
101 }