examples/example_add_random_noise.cxx

00001 //:
00002 // \file
00003 //  This example program shows a typical use of the vipl_add_random_noise IP class
00004 //  on a ubyte image.  The input image (argv[1]) must be ubyte, and in that
00005 //  case random noise is added to it (Gaussian, with sigma argv[3], default
00006 //  1) to argv[2] which is always a PGM file image.
00007 //  Uses vipl_add_random_noise<vil_image_view<ubyte>,vil_image_view<ubyte>,ubyte,ubyte>.
00008 //  The input and output vil_image_views are directly passed to the filter.
00009 //
00010 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00011 // \date   28 may 1998
00012 //
00013 // \verbatim
00014 // Modifications:
00015 //   Peter Vanroose, Aug.2000 - adapted to vxl
00016 //   Peter Vanroose, Feb.2004 - replaced vil1_image by vil2_image_view<T>
00017 // \endverbatim
00018 //
00019 #include <vil/vil_image_view.h>
00020 #include <vil/vil_load.h>
00021 #include <vil/vil_save.h>
00022 
00023 #include <vipl/accessors/vipl_accessors_vil_image_view.h>
00024 #include <vipl/vipl_add_random_noise.h>
00025 #include <vcl_iostream.h>
00026 #include <vcl_cstdlib.h> // for atof()
00027 #include <vxl_config.h> // for vxl_byte
00028 
00029 int
00030 main(int argc, char** argv)
00031 {
00032   if (argc < 3) { vcl_cerr << "Syntax: example_add_random_noise file_in file_out [width]\n"; return 1; }
00033 
00034   // The input image:
00035   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00036   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00037 
00038   // The output image:
00039   vil_image_view<vxl_byte> out(in.ni(),in.nj(),in.nplanes());
00040 
00041   // The noise `width':
00042   double sigma = (argc < 4) ? 5.0 : vcl_atof(argv[3]);
00043   vxl_byte s = (vxl_byte)(sigma+0.5); // round to integer
00044 
00045   // The filter:
00046   vipl_add_random_noise<vil_image_view<vxl_byte>,vil_image_view<vxl_byte>,vxl_byte,vxl_byte> op(GAUSSIAN_NOISE,s);
00047   op.put_in_data_ptr(&in);
00048   op.put_out_data_ptr(&out);
00049   op.filter();
00050 
00051   vil_save(out, argv[2]);
00052   vcl_cout << "Noisy image written to " << argv[2] << vcl_endl;
00053   return 0;
00054 }

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