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 <utils/system/argparser.h>
25 #include <fvutils/colormap/colormap.h>
26 #include <fvutils/colormap/yuvcm.h>
27 #include <fvutils/colormap/cmfile.h>
28 
29 #include <cstring>
30 #include <cmath>
31 #include <cstdio>
32 
33 using namespace fawkes;
34 using namespace firevision;
35 
36 int main( int argc, char** argv )
37 {
38  ArgumentParser* argp = new ArgumentParser( argc, argv, "i:o:" );
39 
40  char* in_file = NULL;
41  char* out_file = NULL;
42 
43  if ( argp->has_arg( "i" ) )
44  {
45  in_file = strdup( argp->arg( "i" ) );
46  }
47 
48  if ( argp->has_arg( "o" ) )
49  {
50  out_file = strdup( argp->arg( "o" ) );
51  }
52 
53  if ( !in_file || !out_file )
54  {
55  printf("Usage: argv[0] -i <input colormap> -o <output colormap>\n");
56  }
57  else
58  {
59  printf("Reading colormap from file %s.\n", in_file);
60  printf("Writing modified colormap to file %s.\n", out_file);
61 
62  ColormapFile cmfile;
63  cmfile.read( in_file );
64  Colormap *cm = cmfile.get_colormap();
65  unsigned int cm_width = cm->width();
66  unsigned int cm_height = cm->height();
67  unsigned int cm_depth = cm->depth();
68 
69  unsigned char* cm_buffer = (unsigned char*) malloc( cm->size() );
70  memcpy( (void*) cm_buffer, cm->get_buffer(), cm->size() );
71 
72  YuvColormap* cmpp = new YuvColormap( cm_depth, cm_width, cm_height );
73  cmpp->set( cm_buffer );
74 
75  for ( unsigned int d = 0; d < cm_depth; ++d )
76  {
77  for ( unsigned int w = 0; w < cm_width; ++w )
78  {
79  for ( unsigned int h = 0; h < cm_height; ++h )
80  {
81  float yuvfac = cm->deepness() / (float) cm->depth();
82  unsigned int y = (unsigned int) (d * yuvfac);
83 
84  color_t cur_color = cm->determine(y, w, h);
85 
86  // skip current cell if it already has a color
87  if ( cur_color != C_OTHER )
88  { continue; }
89 
90  unsigned int cm_counter[ C_OTHER + 1 ];
91 
92  for ( unsigned int i = 0; i <= C_OTHER; ++i )
93  { cm_counter[ i ] = 0; }
94 
95  unsigned int tst_radius_dp = 1;
96  unsigned int tst_radius_uv = 4;
97 
98  unsigned int num_neighbours = 0;
99 
100  for ( unsigned int dd = (unsigned int) fmax(d - tst_radius_dp, 0);
101  dd <= fmin( d + tst_radius_dp, cm_depth - 1);
102  ++dd )
103  {
104  for ( unsigned int ww = (unsigned int) fmax(w - tst_radius_uv, 0);
105  ww <= fmin( w + tst_radius_uv, cm_width - 1 );
106  ++ww )
107  {
108  for ( unsigned int hh = (unsigned int) fmax(h - tst_radius_uv, 0);
109  hh <= fmin( h + tst_radius_uv, cm_height - 1);
110  ++hh )
111  {
112  color_t cur_color = cm->determine( (unsigned int) (dd * yuvfac), ww, hh );
113  ++cm_counter[ cur_color ];
114 
115  ++num_neighbours;
116  }
117  }
118  }
119 
120  unsigned int max = 0;
121  color_t max_color = C_OTHER;
122 
123  for ( unsigned int i = 0; i < C_OTHER; ++i )
124  {
125  if ( cm_counter[ i ] > max )
126  {
127  max = cm_counter[ i ];
128  max_color = (color_t) i;
129  }
130  }
131 
132  if ( max > num_neighbours * 0.1 && max_color != C_OTHER )
133  {
134  printf("max=%d max_color=%d num_neighbours=%d\n", max, max_color, num_neighbours);
135  cmpp->set( y, w, h, max_color );
136  }
137  } // h
138  } // w
139  } // d
140 
141  ColormapFile cmout( cm_depth, cm_width, cm_height );
142  cmout.add_colormap( cmpp );
143  printf( "Writing modified colormap.\n" );
144  cmout.write( out_file );
145  }
146 
147  return 0;
148 }
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:182
virtual unsigned int depth() const =0
Get depth of colormap.
Fawkes library namespace.
Parse command line arguments.
Definition: argparser.h:66
Colormap interface.
Definition: colormap.h:38
virtual unsigned int height() const =0
Get height of colormap.
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:169
YUV Colormap.
Definition: yuvcm.h:39
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:308
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const =0
Determine color class for given YUV value.
Colormap file.
Definition: cmfile.h:55
virtual size_t size()=0
Size in bytes of buffer returned by get_buffer().
virtual unsigned int width() const =0
Get width of colormap.
virtual unsigned int deepness() const =0
Get deepness of colormap.
virtual unsigned char * get_buffer() const =0
Get the raw buffer of this colormap.
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169