examples/example_std_dev.cxx

00001 //:
00002 // \file
00003 //  This example program shows a typical use of the vipl_moment IP class on
00004 //  a ubyte image.  The input image (argv[1]) must be ubyte, and in that
00005 //  case is converted to an image where each pixel is the standard
00006 //  deviation of a 5x5 neighbourhood of the corresponding source pixel.
00007 //  The output is written to a PGM file image.
00008 //  Uses vipl_moment<vil_image_view<ubyte>,vil_image_view<ubyte>,ubyte,float>,
00009 //  vipl_monadic<vil_image_view<ubyte>,vil_image_view<ubyte>,float,float>,
00010 //  vipl_dyadic<vil_image_view<ubyte>,vil_image_view<ubyte>,float,float> and
00011 //  vipl_convert<vil_image_view<ubyte>,vil_image_view<ubyte>,float,ubyte> .
00012 //
00013 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00014 // \date   15 sept. 1999
00015 //
00016 // \verbatim
00017 // Modifications:
00018 //   Peter Vanroose, Aug.2000 - adapted to vxl
00019 //   Peter Vanroose, Feb.2004 - replaced vil1_image by vil2_image_view<T>
00020 // \endverbatim
00021 //
00022 #include <vipl/accessors/vipl_accessors_vil_image_view.h>
00023 #include <vil/vil_image_view.h>
00024 #include <vipl/vipl_moment.h>
00025 #include <vipl/vipl_monadic.h>
00026 #include <vipl/vipl_dyadic.h>
00027 #include <vipl/vipl_convert.h>
00028 
00029 // for I/O:
00030 #include <vil/vil_load.h>
00031 #include <vil/vil_save.h>
00032 #include <vcl_iostream.h>
00033 #include <vcl_cmath.h> // for vcl_sqrt()
00034 
00035 #include <vxl_config.h> // for vxl_byte
00036 float square(float const& x) { return x*x; }
00037 void is_minus(float& x, float const& y) { x-=y; }
00038 float squareroot(float const& x) { return vcl_sqrt(x); }
00039 
00040 int
00041 main(int argc, char** argv)
00042 {
00043   if (argc < 3) { vcl_cerr << "Syntax: example_vipl_moment file_in file_out\n"; return 1; }
00044 
00045   // The input image:
00046   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00047   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00048 
00049   // The output image:
00050   vil_image_view<float> out(in.ni(),in.nj(),in.nplanes());
00051 
00052   // Intermediate image:
00053   vil_image_view<float> tmp(in.ni(),in.nj(),in.nplanes());
00054 
00055   // The second moment filter.  result: E(X*X), into out.
00056   vipl_moment<vil_image_view<vxl_byte>,vil_image_view<float>,vxl_byte,float> scnd_moment(2,5,5);
00057   scnd_moment.put_in_data_ptr(&in);
00058   scnd_moment.put_out_data_ptr(&out);
00059   scnd_moment.filter();
00060 
00061   // The first moment filter.  result: E(X), into tmp.
00062   vipl_moment<vil_image_view<vxl_byte>,vil_image_view<float>,vxl_byte,float> frst_moment(1,5,5);
00063   frst_moment.put_in_data_ptr(&in);
00064   frst_moment.put_out_data_ptr(&tmp);
00065   frst_moment.filter();
00066 
00067   // The monadic "square" point operator (input=output).  result: E(X)*E(X)
00068   vipl_monadic<vil_image_view<float>,vil_image_view<float>,float,float> square_op(square);
00069   square_op.put_in_data_ptr(&tmp);
00070   square_op.put_out_data_ptr(&tmp);
00071   square_op.filter();
00072 
00073   // The dyadic "is_minus" point operator.  result: E(X*X) - E(X)*E(X)
00074   vipl_dyadic<vil_image_view<float>,vil_image_view<float>,float,float> minus_op(is_minus);
00075   minus_op.put_in_data_ptr(&tmp);
00076   minus_op.put_out_data_ptr(&out);
00077   minus_op.filter();
00078 
00079   // The monadic "square root" point operator (input = output)
00080   vipl_monadic<vil_image_view<float>,vil_image_view<float>,float,float> sqrt_op(squareroot);
00081   sqrt_op.put_in_data_ptr(&out);
00082   sqrt_op.put_out_data_ptr(&out);
00083   sqrt_op.filter();
00084 
00085   // vipl_convert to vxl_byte and write to PGM file:
00086   vil_image_view<vxl_byte> pgm(in.ni(),in.nj(),in.nplanes());
00087   vipl_convert<vil_image_view<float>,vil_image_view<vxl_byte>,float,vxl_byte> op;
00088   op.put_in_data_ptr(&out);
00089   op.put_out_data_ptr(&pgm);
00090   op.filter();
00091   vil_save(pgm, argv[2], "pnm");
00092   vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
00093 
00094   return 0;
00095 }

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