SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
histo.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_HISTO_HPP
76 #define SLIP_HISTO_HPP
77 
78 #include <iterator>
79 #include <algorithm>
80 #include <numeric>
81 #include <cassert>
82 #include <cmath>
83 #include <iostream>
84 
85 namespace slip
86 {
87 
111  template<typename InputIterator, typename RandomAccessIterator>
112  inline
113  void histogram(InputIterator first,
114  InputIterator last,
115  RandomAccessIterator histo_first,
116  RandomAccessIterator histo_last,
117  typename std::iterator_traits<InputIterator>::value_type minval,
118  typename std::iterator_traits<InputIterator>::value_type maxval,
119  typename std::iterator_traits<InputIterator>::value_type step = 1)
120  {
121  typedef typename std::iterator_traits<InputIterator>::value_type T;
122  typedef typename std::iterator_traits<RandomAccessIterator>::value_type histo_T;
123 
124  assert(first != last);
125  assert(step > 0);
126  assert((histo_last - histo_first) == (int)((maxval - minval + 1)/step));
127 
128  // Initialize result and check values
129  std::fill(histo_first,histo_last,histo_T());
130  int maxi = static_cast<int>((histo_last-histo_first) - 1);
131 
132  InputIterator first2 = first;
133  bool error = false;
134  for (; !error && first2 != last; ++first2)
135  {
136  if ( (*first2 < minval) || (*first2 > maxval) )
137  {
138  error = true;
139  }
140  }
141  if (error)
142  {
143  std::cerr << "Some values of the input datas are outside the defined interval: ["<<minval<<","<<maxval<<"]"<<std::endl;
144  }
145  // Store the histogram
146  while(first != last)
147  {
148  int i = (int) trunc((*first - minval) / step);
149  if ((i > maxi) || (i < 0))
150  {
151  if (*first == maxval)
152  {
153  (*(histo_first + maxi))++;
154  }
155  // Else not take into account
156  }
157  else
158  {
159  (*(histo_first + i))++;
160  }
161  ++first;
162  }
163  }
164 
204  template<typename InputIterator,
205  typename RandomAccessIterator,
206  typename MaskIterator>
207  inline
208  void histogram_mask(InputIterator first,
209  InputIterator last,
210  MaskIterator mask_first,
211  RandomAccessIterator histo_first,
212  RandomAccessIterator histo_last,
213  typename std::iterator_traits<InputIterator>::value_type minval,
214  typename std::iterator_traits<InputIterator>::value_type maxval,
215  typename std::iterator_traits<InputIterator>::value_type step = 1,
216  typename std::iterator_traits<MaskIterator>::value_type value=typename std::iterator_traits<MaskIterator>::value_type(1))
217  {
218  assert(first != last);
219  assert(step > 0);
220  assert((histo_last - histo_first) == (int)((maxval - minval + 1)/step));
221 
222  typedef typename std::iterator_traits<InputIterator>::value_type T;
223  typedef typename std::iterator_traits<RandomAccessIterator>::value_type histo_T;
224 
225  // Initialize result and check values
226  std::fill(histo_first,histo_last,histo_T());
227  int maxi = static_cast<int>((histo_last-histo_first) - 1);
228 
229  InputIterator first2 = first;
230  MaskIterator mask_first2 = mask_first;
231  bool error = false;
232  for (; !error && first2 != last; ++first2, ++mask_first2)
233  {
234  if(*mask_first2 == value)
235  {
236  if ( (*first2 < minval) || (*first2 > maxval) )
237  {
238  error = true;
239  }
240  }
241  }
242  if (error)
243  {
244  std::cerr << "Some values of the input datas are outside the defined interval: ["<<minval<<","<<maxval<<"]"<<std::endl;
245  }
246  // Store the histogram
247 
248  while(first != last)
249  {
250  if(*mask_first==value)
251  {
252  int i = (int) trunc((*first - minval) / step);
253 
254  if ((i > maxi) || (i < 0))
255  {
256  if (*first == maxval)
257  {
258  (*(histo_first + maxi))++;
259 
260  }
261  // Else not take into account
262  }
263  else
264  {
265  (*(histo_first + i))++;
266  }
267  }
268  ++first;
269  ++mask_first;
270  }
271 
272  }
273 
274 
275 
314  template<typename InputIterator,
315  typename RandomAccessIterator,
316  typename Predicate>
317  inline
318  void histogram_if(InputIterator first,
319  InputIterator last,
320  RandomAccessIterator histo_first,
321  RandomAccessIterator histo_last,
322  Predicate pred,
323  typename std::iterator_traits<InputIterator>::value_type minval,
324  typename std::iterator_traits<InputIterator>::value_type maxval,
325  typename std::iterator_traits<InputIterator>::value_type step = 1
326  )
327  {
328  assert(first != last);
329  assert(step > 0);
330  assert((histo_last - histo_first) == (int)((maxval - minval + 1)/step));
331  typedef typename std::iterator_traits<InputIterator>::value_type T;
332  typedef typename std::iterator_traits<RandomAccessIterator>::value_type histo_T;
333 
334 
335 
336 
337  // Initialize result and check values
338  std::fill(histo_first,histo_last,histo_T());
339  int maxi = static_cast<int>((histo_last-histo_first) - 1);
340 
341  InputIterator first2 = first;
342  bool error = false;
343  for (; !error && first2 != last; ++first2)
344  {
345  if(pred(*first2))
346  {
347  if ( (*first2 < minval) || (*first2 > maxval) )
348  {
349  error = true;
350  }
351  }
352  }
353  if (error)
354  {
355  std::cerr << "Some values of the input datas are outside the defined interval: ["<<minval<<","<<maxval<<"]"<<std::endl;
356  }
357  // Store the histogram
358  while(first != last)
359  {
360  if(pred(*first))
361  {
362  int i = (int) trunc((*first - minval) / step);
363 
364  if ((i > maxi) || (i < 0))
365  {
366  if (*first == maxval)
367  {
368  (*(histo_first + maxi))++;
369 
370  }
371  // Else not take into account
372  }
373  else
374  {
375  (*(histo_first + i))++;
376 
377  }
378  }
379  ++first;
380 
381  }
382  }
383 
404  template<typename InputIterator, typename InputIterator2, typename OutputIterator>
405  inline
406  void real_histogram(InputIterator first,
407  InputIterator last,
408  InputIterator2 stepfirst,
409  InputIterator2 steplast,
410  OutputIterator result)
411  {
412  assert(first != last);
413  assert(stepfirst != steplast);
414 
415  InputIterator2 runsteps;
416  InputIterator first2 = first;
417  bool error = false;
418  bool end = false;
419  int i = 0;
420  // Initialize result and check values
421 
422  for (runsteps=stepfirst+1,i=0;runsteps!=steplast;i++,runsteps++)
423  {
424  *(result + i) = 0;
425  }
426 
427  for (; !error && (first2 != last); ++first2){
428  if ( (*first2 < *stepfirst) || (*first2 > *(steplast-1)))
429  {
430  error=true;
431  }
432  }
433  if (error)
434  {
435  std::cerr << "Some values of the input datas are outside the defined interval: ["<<*stepfirst<<","<<*(steplast-1)<<"]"<<std::endl;
436  }
437 
438 
439  // Store the histogram
440  while(first != last)
441  {
442  i = -1;
443  end = false;
444 
445  for (runsteps=stepfirst;!end && runsteps != steplast; runsteps++)
446  {
447  if (*first >= *runsteps)
448  {
449  i++;
450  }
451  else
452  {
453  end = true;
454  }
455  }
456  // Over value : do not take into account
457  if (!end && (*first >= *(steplast-1)))
458  {
459  // High Bound case
460  if (*first == *(steplast-1))
461  {
462  i--;
463  }
464  else
465  {
466  i = -1;
467  }
468  }
469 
470 
471  if (i >= 0)
472  {
473  (*(result+i))++;
474  }
475  ++first;
476  }
477  }
478 
479 
480 
506 template<typename RandomAccessIterator1, typename RandomAccessIterator2>
507  inline
508  void cumulative_histogram(RandomAccessIterator1 histo_first,
509  RandomAccessIterator1 histo_last,
510  RandomAccessIterator2 cum_histo_first,
511  RandomAccessIterator2 cum_histo_last)
512  {
513 
514  assert(histo_last != histo_first);
515  assert((histo_last - histo_first) == (cum_histo_last - cum_histo_first));
516 
517  typedef typename std::iterator_traits<RandomAccessIterator2>::value_type value_type;
518  //std::copy(histo_first,histo_last,cum_histo_first);
519  *cum_histo_first++ = *histo_first++;
520  for(; histo_first != histo_last; ++histo_first, ++cum_histo_first)
521  {
522  *cum_histo_first = *(cum_histo_first-1) + *histo_first;
523  }
524  }
525 
526 }//slip::
527 
528 
529 #endif //SLIP_HISTO_HPP
void histogram(InputIterator first, InputIterator last, RandomAccessIterator histo_first, RandomAccessIterator histo_last, typename std::iterator_traits< InputIterator >::value_type minval, typename std::iterator_traits< InputIterator >::value_type maxval, typename std::iterator_traits< InputIterator >::value_type step=1)
Simple histogram algorithm (uniform step)
Definition: histo.hpp:113
Computes the maximum value between two values.
Definition: macros.hpp:214
void histogram_mask(InputIterator first, InputIterator last, MaskIterator mask_first, RandomAccessIterator histo_first, RandomAccessIterator histo_last, typename std::iterator_traits< InputIterator >::value_type minval, typename std::iterator_traits< InputIterator >::value_type maxval, typename std::iterator_traits< InputIterator >::value_type step=1, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Simple histogram algorithm (uniform step) according to a mask sequence.
Definition: histo.hpp:208
void real_histogram(InputIterator first, InputIterator last, InputIterator2 stepfirst, InputIterator2 steplast, OutputIterator result)
Complex histogram algorithm (variable step)
Definition: histo.hpp:406
void cumulative_histogram(RandomAccessIterator1 histo_first, RandomAccessIterator1 histo_last, RandomAccessIterator2 cum_histo_first, RandomAccessIterator2 cum_histo_last)
Computes the cumulative histogram from a histogram.
Definition: histo.hpp:508
void histogram_if(InputIterator first, InputIterator last, RandomAccessIterator histo_first, RandomAccessIterator histo_last, Predicate pred, typename std::iterator_traits< InputIterator >::value_type minval, typename std::iterator_traits< InputIterator >::value_type maxval, typename std::iterator_traits< InputIterator >::value_type step=1)
Simple histogram algorithm (uniform step) according to a predicate.
Definition: histo.hpp:318