examples/example_median.cxx

00001 // This is tbl/vipl/examples/example_median.cxx
00002 
00003 //:
00004 // \file
00005 //  This example program shows a typical use of the median filter on
00006 //  a ubyte image.  The input image (argv[1]) must be ubyte, and in that
00007 //  case its median smoothed version (with circular kernel, default the
00008 //  3x3 square) is written to argv[2] which is always a PGM file image.
00009 //  Uses vipl_median<vnl_matrix<ubyte>,vnl_matrix<ubyte>,ubyte,ubyte>.
00010 //  The conversion between vil_image_view<ubyte> and the in-memory vnl_matrix<ubyte>
00011 //  is done explicitly.
00012 //
00013 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00014 // \date   18 dec. 1998
00015 //
00016 // \verbatim
00017 // Modifications:
00018 //   Peter Vanroose, Aug.2000 - adapted to vxl
00019 //   Peter Vanroose, Feb.2004 - replaced vil1_load by vil2_load
00020 // \endverbatim
00021 //
00022 #include <vnl/vnl_matrix.h>
00023 
00024 #include <vipl/vipl_with_vnl_matrix/accessors/vipl_accessors_vnl_matrix.h>
00025 #include <vipl/vipl_median.h>
00026 
00027 #include <vxl_config.h> // for vxl_byte
00028 
00029 typedef vnl_matrix<vxl_byte> img_type;
00030 
00031 // for I/O:
00032 #include <vil/vil_image_view.h>
00033 #include <vil/vil_load.h>
00034 #include <vil/vil_save.h>
00035 #include <vcl_iostream.h>
00036 #include <vcl_cstdlib.h> // for atof()
00037 #include <vcl_cstring.h> // for memcpy()
00038 
00039 int
00040 main(int argc, char** argv)
00041 {
00042   if (argc < 3) { vcl_cerr << "Syntax: example_median file_in file_out [radius]\n"; return 1; }
00043 
00044   // The input image:
00045   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00046   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00047 
00048   // The output image:
00049   vil_image_view<vxl_byte> out(in.ni(),in.nj(),in.nplanes());
00050 
00051   // The image sizes:
00052   int xs = in.ni();
00053   int ys = in.nj();
00054 
00055   // The radius: (default is 3x3 square)
00056   float radius = (argc < 4) ? 1.5f : (float)vcl_atof(argv[3]);
00057 
00058   img_type src(xs,ys);
00059   img_type dst(xs,ys);
00060 
00061   // set the input image:
00062   vcl_memcpy(src.begin(), in.memory_chunk()->const_data(), in.size_bytes());
00063 
00064   // The filter:
00065   vipl_median<img_type,img_type,vxl_byte,vxl_byte> op(radius);
00066   op.put_in_data_ptr(&src);
00067   op.put_out_data_ptr(&dst);
00068   op.filter();
00069 
00070   // Write output:
00071   vcl_memcpy(out.memory_chunk()->data(), dst.begin(), out.size_bytes());
00072   vil_save(out, argv[2], "pnm");
00073   vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
00074 
00075   return 0;
00076 }

Generated on Sat Nov 22 05:13:27 2008 for contrib/tbl/vipl by  doxygen 1.5.1