contrib/tbl/vipl/vipl_add_random_noise.h
Go to the documentation of this file.
00001 #ifndef vipl_add_random_noise_h_
00002 #define vipl_add_random_noise_h_
00003 //:
00004 // \file
00005 // \brief add random noise to all pixels
00006 //
00007 //   This image processing class is implemented using the vipl filters,
00008 //   which means that it can be used with any image class (IUE or not,
00009 //   TargetJr or not, vil or vil1 or not) of any pixel data type.
00010 //
00011 //   This class actually implements three random noise addition filters:
00012 //   uniform noise, Gaussian noise and exponential noise.
00013 //
00014 //   The constructor takes up to 4 arguments: the noise type (default Gaussian),
00015 //   the `width' (default 5), the initial seed for the random number generator,
00016 //   which defaults to using a "random" seed (based on the current time clock),
00017 //   and finally whether clipping should occur at zero or maxval, or not
00018 //   (default: no clipping; value is set to 0 or maxval respectively).
00019 //
00020 //   Note that the input image pixel type must have a constructor taking
00021 //   `double' as argument.
00022 //
00023 // \author Peter Vanroose, K.U.Leuven (ESAT/PSI)
00024 // \date   28 may 1998.
00025 //
00026 // \verbatim
00027 // Modifications:
00028 //   Peter Vanroose, Aug.2000 - adapted to vxl
00029 //   Peter Vanroose, Nov.2002 - now use vnl_sample for RNG, and avoid clipping
00030 // \endverbatim
00031 //
00032 // \example examples/example_add_random_noise.cxx
00033 
00034 #include <vipl/filter/vipl_filter_2d.h> // parent class
00035 #include <vnl/vnl_sample.h> // random number generator
00036 
00037 enum vipl_random_noise_type { UNIFORM_NOISE, GAUSSIAN_NOISE, EXPONENTIAL_NOISE };
00038 
00039 //: add random noise to all pixels
00040 template <class ImgIn,class ImgOut,class DataIn,class DataOut, VCL_DFL_TYPE_PARAM_STLDECL(PixelItr, vipl_trivial_pixeliter) >
00041 class vipl_add_random_noise : public vipl_filter_2d<ImgIn,ImgOut,DataIn,DataOut,PixelItr>
00042 {
00043   // -+-+- data members: -+-+-
00044  private: vipl_random_noise_type type_;
00045  public: vipl_random_noise_type type() const { return type_; }
00046  private: double maxdev_;
00047  public: double maxdev() const { return maxdev_; }
00048  private: bool clipping_;
00049  public: bool clipping() const { return clipping_; }
00050 
00051   // -+-+- constructors/destructors: -+-+-
00052  public:
00053   inline vipl_add_random_noise(vipl_random_noise_type t = GAUSSIAN_NOISE, double m=5, int s=12345, bool clip=false)
00054     : vipl_filter_2d<ImgIn,ImgOut,DataIn,DataOut,PixelItr>(), type_(t), maxdev_(m), clipping_(clip)
00055   { if (s==12345) vnl_sample_reseed(); else vnl_sample_reseed(s); }
00056 
00057   inline vipl_add_random_noise(vipl_add_random_noise const& A)
00058     : vipl_filter_2d<ImgIn,ImgOut,DataIn,DataOut,PixelItr>(A), type_(A.type()), maxdev_(A.maxdev()), clipping_(A.clipping()) {}
00059 
00060   // -+-+- required method for filters: -+-+-
00061   bool section_applyop();
00062 };
00063 
00064 #ifdef INSTANTIATE_TEMPLATES
00065 #include "vipl_add_random_noise.txx"
00066 #endif
00067 
00068 #endif // vipl_add_random_noise_h_