automatic commit
[folded-ctf.git] / labelled_image.cc
diff --git a/labelled_image.cc b/labelled_image.cc
new file mode 100644 (file)
index 0000000..d02313d
--- /dev/null
@@ -0,0 +1,101 @@
+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify  //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation.                         //
+//                                                                       //
+// This program is distributed in the hope that it will be useful, but   //
+// WITHOUT ANY WARRANTY; without even the implied warranty of            //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
+// General Public License for more details.                              //
+//                                                                       //
+// You should have received a copy of the GNU General Public License     //
+// along with this program. If not, see <http://www.gnu.org/licenses/>.  //
+//                                                                       //
+// Written by Francois Fleuret, (C) IDIAP                                //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
+///////////////////////////////////////////////////////////////////////////
+
+#include "labelled_image.h"
+
+LabelledImage::LabelledImage() : RichImage() {
+  _target_poses = 0;
+}
+
+LabelledImage::LabelledImage(int width, int height, int nb_targets) : RichImage(width, height) {
+  _nb_targets = nb_targets;
+  _target_poses = new Pose[_nb_targets];
+}
+
+LabelledImage::~LabelledImage() {
+  delete[] _target_poses;
+}
+
+int LabelledImage::pose_cell_label(PoseCell *cell) {
+  int positive = 0;
+  int negative = 1;
+
+  for(int t = 0; t < _nb_targets; t++) {
+    if(cell->contains(_target_poses + t))
+      positive = 1;
+    if(!cell->negative_for_train(_target_poses + t))
+      negative = 0;
+  }
+
+  if(positive) return 1;
+  if(negative) return -1;
+  return 0;
+}
+
+void LabelledImage::crop(int xmin, int ymin, int width, int height) {
+  RichImage::crop(xmin, ymin, width, height);
+  for(int t = 0; t < _nb_targets; t++) {
+    _target_poses[t].translate(- xmin, - ymin);
+  }
+}
+
+void LabelledImage::reduce() {
+  int xmin = _width, xmax = 0, ymin = _height, ymax = 0;
+  if(_nb_targets > 0) {
+    for(int t = 0; t < _nb_targets; t++) {
+      xmin = min(xmin, int(_target_poses[t]._bounding_box_xmin));
+      ymin = min(ymin, int(_target_poses[t]._bounding_box_ymin));
+      xmax = max(xmax, int(_target_poses[t]._bounding_box_xmax));
+      ymax = max(ymax, int(_target_poses[t]._bounding_box_ymax));
+    }
+  } else {
+    xmin = 0; ymin = 0;
+    xmax = 640; ymax = 480;
+  }
+  xmin = max(0, xmin);
+  ymin = max(0, ymin);
+  xmax = min(_width, xmax);
+  ymax = min(_height, ymax);
+  crop(xmin, ymin, xmax - xmin, ymax - ymin);
+}
+
+void LabelledImage::write(ostream *out) {
+  int v = file_format_version;
+  write_var(out, &v);
+  RichImage::write(out);
+  write_var(out, &_nb_targets);
+  for(int t = 0; t < _nb_targets; t++)
+    _target_poses[t].write(out);
+}
+
+void LabelledImage::read(istream *in) {
+  int v;
+  read_var(in, &v);
+  if(v != file_format_version) {
+    cerr << "Pool file format version " << file_format_version << " expected,"
+         << " the file is version " << v
+         << endl;
+    exit(1);
+  }
+  RichImage::read(in);
+  delete[] _target_poses;
+  read_var(in, &_nb_targets);
+  _target_poses = new Pose[_nb_targets];
+  for(int t = 0; t < _nb_targets; t++)
+    _target_poses[t].read(in);
+}