contrib/tbl/vepl/vepl_add_random_noise.cxx
Go to the documentation of this file.
00001 // This is tbl/vepl/vepl_add_random_noise.cxx
00002 #include "vepl_add_random_noise.h"
00003 //:
00004 // \file
00005 
00006 #include <vepl/accessors/vipl_accessors_vil_image_view_base.h>
00007 #include <vipl/vipl_add_random_noise.h>
00008 #include <vil/vil_image_view.h>
00009 #include <vil/vil_pixel_format.h>
00010 #include <vil/vil_plane.h>
00011 #include <vil/vil_new.h>
00012 #include <vxl_config.h> // for vxl_byte
00013 
00014 vil_image_resource_sptr vepl_add_random_noise(vil_image_resource_sptr image, double maxdev)
00015 {
00016   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00017 
00018   // multi-planar image
00019   // since vipl does not know the concept of planes, run filter on each plane
00020   if (image->nplanes() > 1) {
00021     if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00022       vil_image_view<vxl_byte> in = image->get_view();
00023       vil_image_view<vxl_byte> out = image->get_copy_view();
00024       vipl_add_random_noise<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(GAUSSIAN_NOISE,maxdev);
00025       for (unsigned int p=0; p<image->nplanes(); ++p) {
00026         vil_image_view<vxl_byte> i = vil_plane(in,p), o = vil_plane(out,p);
00027         op.put_in_data_ptr(&i); op.put_out_data_ptr(&o); op.filter();
00028       }
00029       img_out->put_view(out);
00030     }
00031     else
00032       vcl_cerr << __FILE__ ": vepl_dilate_disk() not implemented for multi-planar " << image << '\n';
00033   }
00034 
00035   // byte greyscale
00036   else if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00037     vil_image_view<vxl_byte> in = image->get_view();
00038     vil_image_view<vxl_byte> out = image->get_copy_view();
00039     vipl_add_random_noise<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(GAUSSIAN_NOISE,maxdev);
00040     op.put_in_data_ptr(&in);
00041     op.put_out_data_ptr(&out);
00042     op.filter();
00043     img_out->put_view(out);
00044   }
00045 
00046   // byte rgb
00047   else if (image->pixel_format() == VIL_PIXEL_FORMAT_RGB_BYTE) {
00048     img_out = vil_new_image_resource(image->ni(), image->nj()*image->nplanes(), 1, image->pixel_format());
00049     vil_image_view<vxl_byte> in = image->get_view(); // in will have 3 planes but 1 component
00050     vil_image_view<vxl_byte> out = image->get_copy_view();
00051     vipl_add_random_noise<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(GAUSSIAN_NOISE,maxdev);
00052     op.put_in_data_ptr(&in);
00053     op.put_out_data_ptr(&out);
00054     op.filter();
00055     img_out->put_view(out);
00056   }
00057 
00058   // short
00059   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
00060     vil_image_view<vxl_uint_16> in = image->get_view();
00061     vil_image_view<vxl_uint_16> out = image->get_copy_view();
00062     vipl_add_random_noise<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16> op(GAUSSIAN_NOISE,maxdev);
00063     op.put_in_data_ptr(&in);
00064     op.put_out_data_ptr(&out);
00065     op.filter();
00066     img_out->put_view(out);
00067   }
00068 
00069   // float
00070   else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00071     vil_image_view<float> in = image->get_view();
00072     vil_image_view<float> out = image->get_copy_view();
00073     vipl_add_random_noise<vil_image_view_base,vil_image_view_base,float,float> op(GAUSSIAN_NOISE,maxdev);
00074     op.put_in_data_ptr(&in);
00075     op.put_out_data_ptr(&out);
00076     op.filter();
00077     img_out->put_view(out);
00078   }
00079 
00080   // double
00081   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00082     vil_image_view<double> in = image->get_view();
00083     vil_image_view<double> out = image->get_copy_view();
00084     vipl_add_random_noise<vil_image_view_base,vil_image_view_base,double,double> op(GAUSSIAN_NOISE,maxdev);
00085     op.put_in_data_ptr(&in);
00086     op.put_out_data_ptr(&out);
00087     op.filter();
00088     img_out->put_view(out);
00089   }
00090 
00091   //
00092   else
00093     vcl_cerr << __FILE__ ": vepl_add_random_noise() not implemented for " << image << '\n';
00094 
00095   return img_out;
00096 }
00097