examples/example_erode_disk.cxx

00001 // This is tbl/vipl/examples/example_erode_disk.cxx
00002 
00003 //:
00004 // \file
00005 //  This example program shows a typical use of a morphological IP class on
00006 //  a ubyte image.  The input image (argv[1]) must be ubyte, and in that
00007 //  case is eroded (circular kernel, default 3x3 square) to argv[2]
00008 //  which is always a PGM file image.
00009 //  Uses vipl_erode_disk<section<ubyte,2>,section<ubyte,2>,ubyte,ubyte>.
00010 //  The conversion between vil_image_view<ubyte> and the in-memory section<ubyte,2>
00011 //  is done explicitly.
00012 //
00013 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00014 // \date   15 nov. 1997
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 <section/section.h>
00023 #include <vipl/vipl_with_section/accessors/vipl_accessors_section.h>
00024 
00025 #include <vipl/vipl_erode_disk.h>
00026 
00027 #include <vxl_config.h> // for vxl_byte
00028 typedef section<vxl_byte,2> img_type;
00029 
00030 // for I/O:
00031 #include <vil/vil_image_view.h>
00032 #include <vil/vil_load.h>
00033 #include <vil/vil_save.h>
00034 #include <vcl_iostream.h>
00035 #include <vcl_cstdlib.h> // for atof()
00036 #include <vcl_cstring.h> // for memcpy()
00037 
00038 int
00039 main(int argc, char** argv)
00040 {
00041   if (argc < 3) { vcl_cerr << "Syntax: example_erode_disk file_in file_out [radius]\n"; return 1; }
00042 
00043   // The input image:
00044   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00045   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00046 
00047   // The output image:
00048   vil_image_view<vxl_byte> out(in.ni(),in.nj(),in.nplanes());
00049 
00050   // The image sizes:
00051   int xs = in.ni();
00052   int ys = in.nj();
00053 
00054   // The radius: (default is 3x3 square)
00055   float radius = (argc < 4) ? 1.5f : (float)vcl_atof(argv[3]);
00056 
00057   img_type src(xs,ys); // in-memory 2D images
00058   img_type dst(xs,ys);
00059 
00060   // set the input image:
00061   vcl_memcpy(src.buffer, in.memory_chunk()->const_data(), in.size_bytes());
00062 
00063   // The filter:
00064   vipl_erode_disk<img_type,img_type,vxl_byte,vxl_byte> op(radius);
00065   op.put_in_data_ptr(&src);
00066   op.put_out_data_ptr(&dst);
00067   op.filter();
00068 
00069   // Write output:
00070   vcl_memcpy(out.memory_chunk()->data(), dst.buffer, out.size_bytes());
00071   vil_save(out, argv[2], "pnm");
00072   vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
00073 
00074   return 0;
00075 }

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