examples/example4_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<vil1_image,vil1_image,ubyte,ubyte>.
00008 //
00009 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00010 // \date   15 nov. 1997
00011 //
00012 // \verbatim
00013 // Modifications:
00014 //   Peter Vanroose, Aug.2000 - adapted to vxl
00015 // \endverbatim
00016 //
00017 #include <vil1/vil1_memory_image_of.h>
00018 #include <vil1/vil1_pixel.h>
00019 #include <vipl/accessors/vipl_accessors_vil1_image.h>
00020 #include <vipl/vipl_threshold.h>
00021 
00022 #include <vxl_config.h> // for vxl_byte
00023 typedef vil1_image img_type;
00024 
00025 // for I/O:
00026 #include <vil1/vil1_load.h>
00027 #include <vil1/vil1_save.h>
00028 #include <vcl_iostream.h>
00029 #include <vcl_cstdlib.h> // for atoi()
00030 
00031 int
00032 main(int argc, char** argv) {
00033   if (argc < 3) { vcl_cerr << "Syntax: example4_threshold file_in file_out [threshold]\n"; return 1; }
00034 
00035   // The input image:
00036   vil1_image in = vil1_load(argv[1]);
00037   if (vil1_pixel_format(in) != VIL1_BYTE) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00038 
00039   // The output image:
00040   vil1_memory_image_of<vxl_byte> out(in);
00041 
00042   // The image sizes:
00043   int xs = in.width();
00044   int ys = in.height();
00045 
00046   // The threshold value:
00047   vxl_byte threshold = (argc < 4) ? 64 : vcl_atoi(argv[3]);
00048 
00049   vil1_memory_image_of<vxl_byte> src(in); // in-memory vil1_image
00050   vxl_byte* buf = new vxl_byte[in.get_size_bytes()];
00051 
00052   // set the input image:
00053   in.get_section(buf,0,0,xs,ys);
00054   src.put_section(buf,0,0,xs,ys);
00055 
00056   // perform thresholding:
00057   vipl_threshold<img_type,img_type,vxl_byte,vxl_byte> op(threshold,0,255);
00058   op.put_in_data_ptr(&src);
00059   op.put_out_data_ptr(&src); // NOTE THAT dst == src
00060   op.filter();
00061 
00062   // Write output:
00063   src.get_section(buf,0,0,xs,ys);
00064   out.put_section(buf,0,0,xs,ys);
00065   vil1_save(out, argv[2], "pnm");
00066   vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
00067 
00068   delete[] buf;
00069   return 0;
00070 }

Generated on Tue Oct 14 05:13:26 2008 for contrib/tbl/vipl by  doxygen 1.5.1