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. //
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. //
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/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
19 #include "labelled_image.h"
21 LabelledImage::LabelledImage() : RichImage() {
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];
30 LabelledImage::~LabelledImage() {
31 delete[] _target_poses;
34 int LabelledImage::pose_cell_label(PoseCell *cell) {
38 for(int t = 0; t < _nb_targets; t++) {
39 if(cell->contains(_target_poses + t))
41 if(!cell->negative_for_train(_target_poses + t))
45 if(positive) return 1;
46 if(negative) return -1;
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);
57 void LabelledImage::reduce() {
58 int xmin = _width, xmax = 0, ymin = _height, ymax = 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));
68 xmax = 640; ymax = 480;
72 xmax = min(_width, xmax);
73 ymax = min(_height, ymax);
74 crop(xmin, ymin, xmax - xmin, ymax - ymin);
77 void LabelledImage::write(ostream *out) {
78 int v = file_format_version;
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);
86 void LabelledImage::read(istream *in) {
89 if(v != file_format_version) {
90 cerr << "Pool file format version " << file_format_version << " expected,"
91 << " the file is version " << v
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);