Fawkes API  Fawkes Development Version
cmpp.cpp
1 
2 /***************************************************************************
3  * cmpp.cpp - Colormap Postprocessor. Extends the regions in the colormap
4  *
5  * Created: Tue April 23 17:42:14 2009
6  * Copyright 2009 Daniel Beck
7  * 2009 Stefan Schiffer
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 #include <fvutils/colormap/cmfile.h>
25 #include <fvutils/colormap/colormap.h>
26 #include <fvutils/colormap/yuvcm.h>
27 #include <utils/system/argparser.h>
28 
29 #include <cmath>
30 #include <cstdio>
31 #include <cstring>
32 
33 using namespace fawkes;
34 using namespace firevision;
35 
36 int
37 main(int argc, char **argv)
38 {
39  ArgumentParser *argp = new ArgumentParser(argc, argv, "i:o:");
40 
41  char *in_file = NULL;
42  char *out_file = NULL;
43 
44  if (argp->has_arg("i")) {
45  in_file = strdup(argp->arg("i"));
46  }
47 
48  if (argp->has_arg("o")) {
49  out_file = strdup(argp->arg("o"));
50  }
51 
52  if (!in_file || !out_file) {
53  printf("Usage: argv[0] -i <input colormap> -o <output colormap>\n");
54  } else {
55  printf("Reading colormap from file %s.\n", in_file);
56  printf("Writing modified colormap to file %s.\n", out_file);
57 
58  ColormapFile cmfile;
59  cmfile.read(in_file);
60  Colormap * cm = cmfile.get_colormap();
61  unsigned int cm_width = cm->width();
62  unsigned int cm_height = cm->height();
63  unsigned int cm_depth = cm->depth();
64 
65  unsigned char *cm_buffer = (unsigned char *)malloc(cm->size());
66  memcpy((void *)cm_buffer, cm->get_buffer(), cm->size());
67 
68  YuvColormap *cmpp = new YuvColormap(cm_depth, cm_width, cm_height);
69  cmpp->set(cm_buffer);
70 
71  for (unsigned int d = 0; d < cm_depth; ++d) {
72  for (unsigned int w = 0; w < cm_width; ++w) {
73  for (unsigned int h = 0; h < cm_height; ++h) {
74  float yuvfac = cm->deepness() / (float)cm->depth();
75  unsigned int y = (unsigned int)(d * yuvfac);
76 
77  color_t cur_color = cm->determine(y, w, h);
78 
79  // skip current cell if it already has a color
80  if (cur_color != C_OTHER) {
81  continue;
82  }
83 
84  unsigned int cm_counter[C_OTHER + 1];
85 
86  for (unsigned int i = 0; i <= C_OTHER; ++i) {
87  cm_counter[i] = 0;
88  }
89 
90  unsigned int tst_radius_dp = 1;
91  unsigned int tst_radius_uv = 4;
92 
93  unsigned int num_neighbours = 0;
94 
95  for (unsigned int dd = (unsigned int)fmax(d - tst_radius_dp, 0);
96  dd <= fmin(d + tst_radius_dp, cm_depth - 1);
97  ++dd) {
98  for (unsigned int ww = (unsigned int)fmax(w - tst_radius_uv, 0);
99  ww <= fmin(w + tst_radius_uv, cm_width - 1);
100  ++ww) {
101  for (unsigned int hh = (unsigned int)fmax(h - tst_radius_uv, 0);
102  hh <= fmin(h + tst_radius_uv, cm_height - 1);
103  ++hh) {
104  color_t cur_color = cm->determine((unsigned int)(dd * yuvfac), ww, hh);
105  ++cm_counter[cur_color];
106 
107  ++num_neighbours;
108  }
109  }
110  }
111 
112  unsigned int max = 0;
113  color_t max_color = C_OTHER;
114 
115  for (unsigned int i = 0; i < C_OTHER; ++i) {
116  if (cm_counter[i] > max) {
117  max = cm_counter[i];
118  max_color = (color_t)i;
119  }
120  }
121 
122  if (max > num_neighbours * 0.1 && max_color != C_OTHER) {
123  printf("max=%u max_color=%d num_neighbours=%u\n", max, max_color, num_neighbours);
124  cmpp->set(y, w, h, max_color);
125  }
126  } // h
127  } // w
128  } // d
129 
130  ColormapFile cmout(cm_depth, cm_width, cm_height);
131  cmout.add_colormap(cmpp);
132  printf("Writing modified colormap.\n");
133  cmout.write(out_file);
134  }
135 
136  return 0;
137 }
firevision::Colormap::determine
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const =0
firevision::Colormap::size
virtual size_t size()=0
firevision::YuvColormap
Definition: yuvcm.h:41
firevision::Colormap::height
virtual unsigned int height() const =0
firevision::FireVisionDataFile::read
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:290
firevision::ColormapFile::get_colormap
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:170
firevision::Colormap::depth
virtual unsigned int depth() const =0
fawkes::ArgumentParser::has_arg
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:171
firevision::Colormap::deepness
virtual unsigned int deepness() const =0
firevision::Colormap::get_buffer
virtual unsigned char * get_buffer() const =0
fawkes
fawkes::ArgumentParser
Definition: argparser.h:69
fawkes::ArgumentParser::arg
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:183
firevision::Colormap
Definition: colormap.h:42
firevision::ColormapFile
Definition: cmfile.h:60
firevision::YuvColormap::set
virtual void set(unsigned int y, unsigned int u, unsigned int v, color_t c)
Definition: yuvcm.cpp:194
firevision::Colormap::width
virtual unsigned int width() const =0