examples/example_histogram.cxx

00001 //:
00002 // \file
00003 //  This example program shows a typical use of the vipl_histogram IP class on
00004 //  an int image.  The input image (argv[1]) can be any scalar type (i.e.,
00005 //  it should be mappable to int: [[un]signed] char, [unsigned]short, int),
00006 //  and its histogram is calculated and written to stdout.
00007 //  Uses vipl_histogram<section<ubyte,2>,section<int,2>,ubyte,int>.
00008 //  The conversion between vil_image_view<ubyte> and the in-memory section<ubyte,2>
00009 //  is done explicitly, pixel per pixel (because of possibly different types).
00010 //
00011 //  Note that it seems to be impossible with some compilers (notably
00012 //  VisualC++ 5.0) to specify different types for input and output images!
00013 //  That explains why I use section<int,2> and not section<ubyte,2>
00014 //  as input image type.
00015 //
00016 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00017 // \date   15 nov. 1997
00018 //
00019 // \verbatim
00020 // Modifications:
00021 //   Peter Vanroose, Aug.2000 - adapted to vxl
00022 //   Peter Vanroose, Feb.2004 - replaced vil1_load by vil2_load
00023 // \endverbatim
00024 //
00025 #include <section/section.h>
00026 #include <vipl/vipl_with_section/accessors/vipl_accessors_section.h>
00027 #include <vipl/vipl_histogram.h>
00028 
00029 // for I/O:
00030 #include <vil/vil_load.h>
00031 #include <vil/vil_image_view.h>
00032 #include <vcl_iostream.h>
00033 #include <vcl_cstring.h> // for memcpy()
00034 
00035 #include <vxl_config.h> // for vxl_byte
00036 
00037 #ifdef VCL_VC_5
00038 #define vxl_byte int // this is a hack!!!  See the Description.
00039 #endif
00040 
00041 int
00042 main(int argc, char** argv)
00043 {
00044   if (argc < 2) { vcl_cerr << "Syntax: example_histogram file_in\n"; return 1; }
00045 
00046   // The input image:
00047   vil_image_view<vxl_byte> in = vil_load(argv[1]);
00048 
00049   section<vxl_byte,2> src(in.ni(),in.nj()); // in-memory 2D image
00050   section<int,2> dst(1,256);
00051 
00052   // set the input image:
00053   if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
00054   vcl_memcpy(src.buffer, in.memory_chunk()->const_data(), in.size_bytes());
00055 
00056   // The filter:
00057   vipl_histogram<section<vxl_byte,2>,section<int,2>,vxl_byte,int> op;
00058   op.put_in_data_ptr(&src);
00059   op.put_out_data_ptr(&dst);
00060   op.filter();
00061 
00062   // Write output:
00063   {for (int i=0; i<256; ++i) if (src.buffer[i] != 0)
00064      vcl_cout << i << ": " << int(src.buffer[i]) << vcl_endl;
00065   }
00066 
00067   return 0;
00068 }

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