examples/example_sobel.cxx

00001 //:
00002 // \file
00003 //  This example program shows a typical use of a convolution filter, namely
00004 //  the vipl_sobel (gradient) operator on a greyscale image.  The input image
00005 //  (argv[1]) must be a ubyte image, and in that case its vipl_sobel image is
00006 //  written to argv[2] which is always a PGM file image.
00007 //  Uses vipl_sobel<vbl_array_2d<ubyte>,vbl_array_2d<ubyte>,ubyte,ubyte>.
00008 //
00009 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00010 // \date   29 may 1998.
00011 //
00012 // \verbatim
00013 // Modifications:
00014 //   Peter Vanroose, Aug.2000 - adapted to vxl
00015 //   Peter Vanroose, Feb.2004 - replaced vil1_load by vil2_load
00016 // \endverbatim
00017 //
00018 #include <vbl/vbl_array_2d.h>
00019 
00020 #include <vipl/vipl_with_vbl_array_2d/accessors/vipl_accessors_vbl_array_2d.h>
00021 #include <vipl/vipl_sobel.h>
00022 
00023 #include <vxl_config.h> // for vxl_byte
00024 typedef vbl_array_2d<vxl_byte> img_type;
00025 
00026 // for I/O:
00027 #include <vil/vil_image_view.h>
00028 #include <vil/vil_load.h>
00029 #include <vil/vil_save.h>
00030 #include <vcl_iostream.h>
00031 #include <vcl_cstring.h> // for memcpy()
00032 
00033 int
00034 main(int argc, char** argv)
00035 {
00036   if (argc < 3) { vcl_cerr << "Syntax: example_sobel file_in file_out\n"; return 1; }
00037 
00038   // The input image:
00039   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00040   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00041 
00042   // The output image:
00043   vil_image_view<vxl_byte> out(in.ni(),in.nj(),in.nplanes());
00044 
00045   // The image sizes:
00046   int xs = in.ni();
00047   int ys = in.nj();
00048 
00049   img_type src(xs, ys);
00050   img_type dst(xs, ys);
00051 
00052   // set the input image:
00053   vcl_memcpy(src.begin(), in.memory_chunk()->const_data(), in.size_bytes());
00054 
00055   // The filter:
00056   vipl_sobel<img_type,img_type,vxl_byte,vxl_byte> op;
00057   op.put_in_data_ptr(&src);
00058   op.put_out_data_ptr(&dst);
00059   op.filter();
00060 
00061   // Write output:
00062   vcl_memcpy(out.memory_chunk()->data(), dst.begin(), out.size_bytes());
00063   vil_save(out, argv[2], "pnm");
00064   vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
00065 
00066   return 0;
00067 }

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