cmpp.cpp

00001 
00002 /***************************************************************************
00003  *  cmpp.cpp - Colormap Postprocessor. Extends the regions in the colormap
00004  *
00005  *  Created: Tue April 23 17:42:14 2009
00006  *  Copyright  2009  Daniel Beck
00007  *             2009  Stefan Schiffer
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL file in the doc directory.
00022  */
00023 
00024 #include <utils/system/argparser.h>
00025 #include <fvutils/colormap/colormap.h>
00026 #include <fvutils/colormap/yuvcm.h>
00027 #include <fvutils/colormap/cmfile.h>
00028 
00029 #include <cstring>
00030 #include <cmath>
00031 #include <cstdio>
00032 
00033 using namespace fawkes;
00034 using namespace firevision;
00035 
00036 int main( int argc, char** argv )
00037 {
00038   ArgumentParser* argp = new ArgumentParser( argc, argv, "i:o:" );
00039 
00040   char* in_file  = NULL;
00041   char* out_file = NULL;
00042 
00043   if ( argp->has_arg( "i" ) )
00044   {
00045     in_file = strdup( argp->arg( "i" ) );
00046   }
00047 
00048   if ( argp->has_arg( "o" ) )
00049   {
00050     out_file = strdup( argp->arg( "o" ) );
00051   }
00052 
00053   if ( !in_file || !out_file )
00054   {
00055     printf("Usage: argv[0] -i <input colormap> -o <output colormap>\n");
00056   }
00057   else
00058   {
00059     printf("Reading colormap from file %s.\n", in_file);
00060     printf("Writing modified colormap to file %s.\n", out_file);
00061     
00062     ColormapFile cmfile;
00063     cmfile.read( in_file );
00064     Colormap *cm = cmfile.get_colormap();
00065     unsigned int cm_width  = cm->width();
00066     unsigned int cm_height = cm->height();
00067     unsigned int cm_depth  = cm->depth();
00068 
00069     unsigned char* cm_buffer = (unsigned char*) malloc( cm->size() );
00070     memcpy( (void*) cm_buffer, cm->get_buffer(), cm->size() );
00071 
00072     YuvColormap* cmpp = new YuvColormap( cm_depth, cm_width, cm_height );
00073     cmpp->set( cm_buffer );
00074 
00075     for ( unsigned int d = 0; d < cm_depth; ++d )
00076     {
00077       for ( unsigned int w = 0; w < cm_width; ++w )
00078       {
00079         for ( unsigned int h = 0; h < cm_height; ++h )
00080         {
00081           float yuvfac = cm->deepness() / (float) cm->depth();
00082           unsigned int y = (unsigned int) (d * yuvfac);
00083           
00084           color_t cur_color = cm->determine(y, w, h);
00085 
00086           // skip current cell if it already has a color
00087           if ( cur_color != C_OTHER )
00088           { continue; }
00089 
00090           unsigned int cm_counter[ C_OTHER + 1 ];
00091 
00092           for ( unsigned int i = 0; i <= C_OTHER; ++i )
00093           { cm_counter[ i ] = 0; }
00094 
00095           unsigned int tst_radius_dp = 1;
00096           unsigned int tst_radius_uv = 4;
00097 
00098           unsigned int num_neighbours = 0;
00099 
00100           for ( unsigned int dd = (unsigned int) fmax(d - tst_radius_dp, 0); 
00101                 dd <= fmin( d + tst_radius_dp, cm_depth - 1);
00102                 ++dd ) 
00103           {
00104             for ( unsigned int ww = (unsigned int) fmax(w - tst_radius_uv, 0);
00105                   ww <= fmin( w + tst_radius_uv, cm_width - 1 );
00106                   ++ww )
00107             {
00108               for ( unsigned int hh = (unsigned int) fmax(h - tst_radius_uv, 0);
00109                     hh <= fmin( h + tst_radius_uv, cm_height - 1);
00110                     ++hh ) 
00111               {
00112                 color_t cur_color = cm->determine( (unsigned int) (dd * yuvfac), ww, hh );
00113                 ++cm_counter[ cur_color ];
00114                 
00115                 ++num_neighbours;
00116               }
00117             }
00118           }
00119           
00120           unsigned int max  = 0;
00121           color_t max_color = C_OTHER;
00122           
00123           for ( unsigned int i = 0; i < C_OTHER; ++i )
00124           {
00125             if ( cm_counter[ i ] > max )
00126             { 
00127               max = cm_counter[ i ];
00128               max_color = (color_t) i;
00129             }
00130           }
00131           
00132           if ( max > num_neighbours * 0.1 && max_color != C_OTHER )
00133           {
00134             printf("max=%d  max_color=%d  num_neighbours=%d\n", max, max_color, num_neighbours);
00135             cmpp->set( y, w, h, max_color );
00136           }
00137         }  // h
00138       }  // w
00139     }  // d
00140 
00141     ColormapFile cmout( cm_depth, cm_width, cm_height );
00142     cmout.add_colormap( cmpp );
00143     printf( "Writing modified colormap.\n" );
00144     cmout.write( out_file );
00145   }
00146 
00147   return 0;
00148 }

Generated on Tue Feb 22 13:31:17 2011 for Fawkes API by  doxygen 1.4.7