SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noise.hpp
Go to the documentation of this file.
1 /*
2  * Copyright(c):
3  * Signal Image and Communications (SIC) Department
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * - University of Poitiers, France http://www.univ-poitiers.fr
6  * - XLIM Institute UMR CNRS 7252 http://www.xlim.fr/
7  *
8  * and
9  *
10  * D2 Fluid, Thermic and Combustion
11  * - University of Poitiers, France http://www.univ-poitiers.fr
12  * - PPRIME Institute - UPR CNRS 3346 http://www.pprime.fr
13  * - ISAE-ENSMA http://www.ensma.fr
14  *
15  * Contributor(s):
16  * The SLIP team,
17  * Benoit Tremblais <tremblais_AT_sic.univ-poitiers.fr>,
18  * Laurent David <laurent.david_AT_lea.univ-poitiers.fr>,
19  * Ludovic Chatellier <ludovic.chatellier_AT_univ-poitiers.fr>,
20  * Lionel Thomas <lionel.thomas_AT_univ-poitiers.fr>,
21  * Denis Arrivault <arrivault_AT_sic.univ-poitiers.fr>,
22  * Julien Dombre <julien.dombre_AT_univ-poitiers.fr>.
23  *
24  * Description:
25  * The Simple Library of Image Processing (SLIP) is a new image processing
26  * library. It is written in the C++ language following as much as possible
27  * the ISO/ANSI C++ standard. It is consequently compatible with any system
28  * satisfying the ANSI C++ complience. It works on different Unix , Linux ,
29  * Mircrosoft Windows and Mac OS X plateforms. SLIP is a research library that
30  * was created by the Signal, Image and Communications (SIC) departement of
31  * the XLIM, UMR 7252 CNRS Institute in collaboration with the Fluids, Thermic
32  * and Combustion departement of the P', UPR 3346 CNRS Institute of the
33  * University of Poitiers.
34  *
35  * The SLIP Library source code has been registered to the APP (French Agency
36  * for the Protection of Programs) by the University of Poitiers and CNRS,
37  * under registration number IDDN.FR.001.300034.000.S.P.2010.000.21000.
38 
39  * http://www.sic.sp2mi.univ-poitiers.fr/slip/
40  *
41  * This software is governed by the CeCILL-C license under French law and
42  * abiding by the rules of distribution of free software. You can use,
43  * modify and/ or redistribute the software under the terms of the CeCILL-C
44  * license as circulated by CEA, CNRS and INRIA at the following URL
45  * http://www.cecill.info.
46  * As a counterpart to the access to the source code and rights to copy,
47  * modify and redistribute granted by the license, users are provided only
48  * with a limited warranty and the software's author, the holder of the
49  * economic rights, and the successive licensors have only limited
50  * liability.
51  *
52  * In this respect, the user's attention is drawn to the risks associated
53  * with loading, using, modifying and/or developing or reproducing the
54  * software by the user in light of its specific status of free software,
55  * that may mean that it is complicated to manipulate, and that also
56  * therefore means that it is reserved for developers and experienced
57  * professionals having in-depth computer knowledge. Users are therefore
58  * encouraged to load and test the software's suitability as regards their
59  * requirements in conditions enabling the security of their systems and/or
60  * data to be ensured and, more generally, to use and operate it in the
61  * same conditions as regards security.
62  *
63  * The fact that you are presently reading this means that you have had
64  * knowledge of the CeCILL-C license and that you accept its terms.
65  */
66 
67 
68 
75 #ifndef SLIP_NOISE_HPP
76 #define SLIP_NOISE_HPP
77 
78 
79 #include <ctime>
80 #include <boost/random.hpp>
81 
82 namespace slip
83 {
84 
86  /* @{ */
107  template<typename InputIterator, typename OutputIterator, typename Real>
108  inline
109  void add_gaussian_noise(InputIterator first,
110  InputIterator last,
111  OutputIterator result,
112  const Real mean = Real(0.0), const Real var = Real(0.01))
113  {
114  assert(first != last);
115  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
116  boost::normal_distribution<> distrib(mean,var);
117  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
118  for (;first!=last;++first,++result)
119  {
120  *result = *first + deg();
121  }
122  }
148  template<typename InputIterator, typename OutputIterator, typename Real,typename MaskIterator>
149  inline
150  void add_gaussian_noise_mask(InputIterator first,
151  InputIterator last,
152  MaskIterator mask_first,
153  OutputIterator result,
154  const Real mean = Real(0.0), const Real var = Real(0.01), typename std::iterator_traits<MaskIterator>::value_type value=typename std::iterator_traits<MaskIterator>::value_type(1))
155  {
156  assert(first != last);
157  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
158  boost::normal_distribution<> distrib(mean,var);
159  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
160  for (;first!=last;++first,++result,++mask_first)
161  {
162 
163  if(*mask_first == value)
164  {
165  *result = *first + deg();
166  }
167  else
168  {
169  *result = *first;
170  }
171  }
172  }
173 
202  template<typename InputIterator, typename OutputIterator, typename Real,typename Predicate>
203  inline
204  void add_gaussian_noise_if(InputIterator first,
205  InputIterator last,
206  OutputIterator result,
207  Predicate pred,
208  const Real mean = Real(0.0), const Real var = Real(0.01))
209  {
210  assert(first != last);
211  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
212  boost::normal_distribution<> distrib(mean,var);
213  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
214  for (;first!=last;++first,++result)
215  {
216 
217  if(pred(*first))
218  {
219  *result = *first + deg();
220  }
221  else
222  {
223  *result = *first;
224  }
225  }
226  }
227 
228  /* @} */
229 
231  /* @{ */
253  template<typename InputIterator, typename OutputIterator, typename Real>
254  inline
255  void add_speckle_noise(InputIterator first,
256  InputIterator last,
257  OutputIterator result,
258  const Real var = 0.04)
259  {
260  assert(first != last);
261  assert (var <= Real(1));
262  assert (var >= Real(0));
263  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
264  boost::normal_distribution<> distrib(-1.0,1.0);
265  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
266  Real sqrt_value=std::sqrt(var);
267  for (;first!=last;++first,++result)
268  {
269  *result = *first * (1 + deg() * sqrt_value);
270  }
271  }
298 template<typename InputIterator, typename OutputIterator, typename Real,typename MaskIterator>
299  inline
300  void add_speckle_noise_mask(InputIterator first,
301  InputIterator last,
302  MaskIterator mask_first,
303  OutputIterator result,
304  const Real var = 0.04,
305  typename std::iterator_traits<MaskIterator>::value_type value=typename std::iterator_traits<MaskIterator>::value_type(1))
306 {
307  assert(first != last);
308  assert (var <= Real(1));
309  assert (var >= Real(0));
310  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
311  boost::normal_distribution<> distrib(-1.0,1.0);
312  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
313  Real sqrt_value=std::sqrt(var);
314  for (;first!=last;++first,++result,++mask_first)
315  {
316  if(*mask_first == value)
317  {
318  *result = *first * (1 + deg() * sqrt_value);
319  }
320  else
321  {
322  *result = *first;
323  }
324  }
325 }
326 
356 template<typename InputIterator, typename OutputIterator, typename Real, typename Predicate>
357  inline
358  void add_speckle_noise_if(InputIterator first,
359  InputIterator last,
360  OutputIterator result,
361  Predicate pred,
362  const Real var = 0.04)
363 {
364  assert(first != last);
365  assert (var <= Real(1));
366  assert (var >= Real(0));
367  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
368  boost::normal_distribution<> distrib(-1.0,1.0);
369  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > deg(generator, distrib);
370  Real sqrt_value=std::sqrt(var);
371  for (;first!=last;++first,++result)
372  {
373  if(pred(*first))
374  {
375  *result = *first * (1 + deg() * sqrt_value);
376  }
377  else
378  {
379  *result = *first;
380  }
381  }
382 }
383 
384  /* @} */
385 
387  /* @{ */
408  template<typename InputIterator, typename OutputIterator, typename Real>
409  inline
410  void add_uniform_noise(InputIterator first,
411  InputIterator last,
412  OutputIterator result,
413  const Real min = Real(0.0),
414  const Real max = Real(1.0))
415  {
416  assert(first != last);
417  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
418  boost::uniform_real<> distrib(min,max);
419  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
420  for (;first!=last;++first,++result)
421  {
422  *result = *first + deg();
423  }
424  }
451 template<typename InputIterator, typename OutputIterator, typename Real,typename MaskIterator>
452  inline
453  void add_uniform_noise_mask(InputIterator first,
454  InputIterator last,
455  MaskIterator mask_first,
456  OutputIterator result,
457  const Real min = Real(0.0),
458  const Real max = Real(1.0),
459  typename std::iterator_traits<MaskIterator>::value_type value=typename std::iterator_traits<MaskIterator>::value_type(1))
460 {
461  assert(first != last);
462  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
463  boost::uniform_real<> distrib(min,max);
464  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
465  for (;first!=last;++first,++result,++mask_first)
466  {
467  if(*mask_first == value)
468  {
469  *result = *first + deg();
470  }
471  else
472  {
473  *result = *first;
474  }
475  }
476 }
505 template<typename InputIterator, typename OutputIterator, typename Real,typename Predicate>
506  inline
507  void add_uniform_noise_if(InputIterator first,
508  InputIterator last,
509  OutputIterator result,
510  Predicate pred,
511  const Real min = Real(0.0),
512  const Real max = Real(1.0))
513 {
514  assert(first != last);
515  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
516  boost::uniform_real<> distrib(min,max);
517  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
518  for (;first!=last;++first,++result)
519  {
520  if(pred(*first))
521  {
522  *result = *first + deg();
523  }
524  else
525  {
526  *result = *first;
527  }
528  }
529 }
530  /* @} */
531 
533  /* @{ */
556 template<typename InputIterator, typename OutputIterator, typename Real>
557  inline
558  void add_salt_and_pepper_noise(InputIterator first,
559  InputIterator last,
560  OutputIterator result,
561  const Real density = Real(0.05),
562  const typename std::iterator_traits<OutputIterator>::value_type salt_value = typename std::iterator_traits<OutputIterator>::value_type(1.0),
563  const typename std::iterator_traits<OutputIterator>::value_type pepper_value = typename std::iterator_traits<OutputIterator>::value_type(0.0))
564  {
565  assert (density <= Real(1));
566  assert (density >= Real(0));
567 
568  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
569  boost::uniform_real<> distrib(0.0,1.0);
570  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
571  for (;first!=last;++first,++result)
572  {
573  Real rand = deg();
574  // salt or pepper
575  if (rand < density)
576  {
577  // Salt case
578  if ((2 * rand) < density)
579  {
580  *result = salt_value;
581  }
582  else
583  {
584  // Pepper case
585  *result = pepper_value;
586  }
587  }
588  else
589  {
590  *result = *first;
591  }
592  }
593  }
594 
623 template<typename InputIterator, typename OutputIterator, typename Real,typename MaskIterator >
624  inline
625  void add_salt_and_pepper_noise_mask(InputIterator first,
626  InputIterator last,
627  MaskIterator mask_first,
628  OutputIterator result,
629  const Real density = Real(0.05),
630  const Real salt_value = Real(1.0),
631  const Real pepper_value = Real(0.0),
632  typename std::iterator_traits<MaskIterator>::value_type value=typename std::iterator_traits<MaskIterator>::value_type(1))
633  {
634  assert (density <= Real(1));
635  assert (density >= Real(0));
636 
637  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
638  boost::uniform_real<> distrib(0.0,1.0);
639  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
640  for (;first!=last;++first,++result,++mask_first)
641  {
642  if(*mask_first==value)
643  {
644  Real rand = deg();
645  // salt or pepper
646  if (rand < density)
647  {
648  // Salt case
649  if ((2 * rand) < density)
650  {
651  *result = salt_value;
652  }
653  else
654  {
655  // Pepper case
656  *result = pepper_value;
657  }
658  }
659  else
660  {
661  *result = *first;
662  }
663  }
664  else
665  {
666  *result = *first;
667  }
668  }
669  }
670 
701 template<typename InputIterator, typename OutputIterator, typename Real,typename Predicate >
702  inline
703  void add_salt_and_pepper_noise_if(InputIterator first,
704  InputIterator last,
705  OutputIterator result,
706  Predicate pred,
707  const Real density = Real(0.05),
708  const Real salt_value = Real(1.0),
709  const Real pepper_value = Real(0.0))
710  {
711  assert (density <= Real(1));
712  assert (density >= Real(0));
713 
714  boost::mt19937 generator(static_cast<unsigned int>(std::time(0)));
715  boost::uniform_real<> distrib(0.0,1.0);
716  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > deg(generator, distrib);
717  for (;first!=last;++first,++result)
718  {
719  if(pred(*first))
720  {
721  Real rand = deg();
722  // salt or pepper
723  if (rand < density)
724  {
725  // Salt case
726  if ((2 * rand) < density)
727  {
728  *result = salt_value;
729  }
730  else
731  {
732  // Pepper case
733  *result = pepper_value;
734  }
735  }
736  else
737  {
738  *result = *first;
739  }
740  }
741  else
742  {
743  *result = *first;
744  }
745  }
746  }
747 /* @} */
748 }//slip::
749 
750 
751 
752 #endif //SLIP_NOISE_HPP
void add_speckle_noise_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const Real var=0.04)
Add a speckle noise to a container according to a predicate.
Definition: noise.hpp:358
void add_salt_and_pepper_noise(InputIterator first, InputIterator last, OutputIterator result, const Real density=Real(0.05), const typename std::iterator_traits< OutputIterator >::value_type salt_value=typename std::iterator_traits< OutputIterator >::value_type(1.0), const typename std::iterator_traits< OutputIterator >::value_type pepper_value=typename std::iterator_traits< OutputIterator >::value_type(0.0))
Add a salt and pepper noise to a container.
Definition: noise.hpp:558
T & min(const GrayscaleImage< T > &M1)
Returns the min element of a GrayscaleImage.
void add_uniform_noise_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator result, const Real min=Real(0.0), const Real max=Real(1.0), typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Add a uniform noise to a container according to mask sequence.
Definition: noise.hpp:453
void add_uniform_noise(InputIterator first, InputIterator last, OutputIterator result, const Real min=Real(0.0), const Real max=Real(1.0))
Add a uniform noise to a container.
Definition: noise.hpp:410
void add_salt_and_pepper_noise_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator result, const Real density=Real(0.05), const Real salt_value=Real(1.0), const Real pepper_value=Real(0.0), typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Add a salt and pepper noise to a container according to a mask sequence.
Definition: noise.hpp:625
void add_salt_and_pepper_noise_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const Real density=Real(0.05), const Real salt_value=Real(1.0), const Real pepper_value=Real(0.0))
Add a salt and pepper noise to a container according to a predicate.
Definition: noise.hpp:703
void add_gaussian_noise_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const Real mean=Real(0.0), const Real var=Real(0.01))
Add a gaussian noise to a container according to a predicate.
Definition: noise.hpp:204
void add_speckle_noise_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator result, const Real var=0.04, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Add a speckle noise to a container according to a mask sequence.
Definition: noise.hpp:300
Value_T mean(InputIterator first, InputIterator last)
Computes the mean value of a range .
Definition: statistics.hpp:314
HyperVolume< T > sqrt(const HyperVolume< T > &M)
void add_gaussian_noise(InputIterator first, InputIterator last, OutputIterator result, const Real mean=Real(0.0), const Real var=Real(0.01))
Add a gaussian noise to a container.
Definition: noise.hpp:109
void add_gaussian_noise_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator result, const Real mean=Real(0.0), const Real var=Real(0.01), typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Add a gaussian noise to a container according to a mask sequence.
Definition: noise.hpp:150
void add_uniform_noise_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const Real min=Real(0.0), const Real max=Real(1.0))
Add a uniform noise to a container according to a predicate.
Definition: noise.hpp:507
T & max(const GrayscaleImage< T > &M1)
Returns the max element of a GrayscaleImage.
void add_speckle_noise(InputIterator first, InputIterator last, OutputIterator result, const Real var=0.04)
Add a speckle noise to a container.
Definition: noise.hpp:255