examples/example3_threshold.cxx

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

Generated on Fri Aug 29 05:13:31 2008 for contrib/tbl/vipl by  doxygen 1.5.1