numeric
MatchTemplate.hpp
Go to the documentation of this file.
1 #ifndef __NUMERIC_MATCH_TEMPLATE_HPP__
2 #define __NUMERIC_MATCH_TEMPLATE_HPP__
3 
4 #include <vector>
5 #include <limits>
6 #include <iostream>
7 
8 namespace numeric
9 {
10  //matches the 1D template against the given sequence by sliding the template over the sequence and
11  //calculating the total square difference for each position
12  //
13  //saves the best position in pos or -1 if the sequence is too small to accommodate the template
14  //saves the square difference of the best position in best_match
15  template<typename TIter>
16  void matchTemplate1D(TIter start,TIter end,TIter template_start, TIter template_end,int &pos,double &best_match,bool remove_offset=true)
17  {
18  best_match = std::numeric_limits<double>::max();
19  pos = -1;
20  for(int i=0;;++i,++start)
21  {
22  double match = 0;
23  double offset = 0;
24  if(remove_offset)
25  offset = *template_start - *start;
26  TIter temp_start = start;
27  TIter temp_tstart = template_start;
28  for(;temp_tstart != template_end && temp_start != end;++temp_tstart,++temp_start)
29  match += pow((*temp_start) - (*temp_tstart) + offset,2);
30 
31  //could not compare hole template
32  if(temp_start == end && temp_tstart != template_end)
33  break;
34 
35  if(match < best_match)
36  {
37  pos = i;
38  best_match = match;
39  }
40  }
41  }
42 
43 
44  // joins two vectors of the same type by copying v2 to the given position of
45  // v1 and returns it as new vector
46  //
47  // pos is adjusting the start position of v2 insight v1
48  // a negative values means that v1 is shifted pos positions to the left
49  // a positive values means that v2 starts at position pos of v1
50  // if abs(pos) is too big the hole between the two vectors is filled with
51  // the given default_value
52  template<typename T>
53  std::vector<T> joinVectors(const std::vector<T> &v1,const std::vector<T> &v2, int pos,const T &default_value)
54  {
55  std::vector<T> temp;
56  if(pos > 0)
57  {
58  temp.resize(pos+v2.size());
59  int n = std::min(pos,(int)v1.size());
60  std::copy(v1.begin(),v1.begin()+n,temp.begin());
61 
62  //fill hole with default values
63  if(n < pos)
64  std::fill(temp.begin()+n,temp.begin()+pos,default_value);
65  std::copy(v2.begin(),v2.end(),temp.begin()+pos);
66  }
67  else
68  temp = v2;
69 
70  if((int)v1.size()-pos > (int)v2.size())
71  {
72  int n = (int)v1.size()-pos-(int)v2.size();
73  int hole = n-(int)v1.size();
74  temp.resize(temp.size()+n);
75  if(hole > 0)
76  {
77  //fill hole with default values
78  std::fill(temp.begin()+((int)temp.size()-n),temp.begin()+((int)temp.size()-n+hole),0);
79  n -= hole;
80  }
81  std::copy(v1.begin()+((int)v1.size()-n),v1.end(),temp.begin()+((int)temp.size()-n));
82  }
83  return temp;
84  }
85 };
86 #endif
Definition: Circle.hpp:6
void matchTemplate1D(TIter start, TIter end, TIter template_start, TIter template_end, int &pos, double &best_match, bool remove_offset=true)
Definition: MatchTemplate.hpp:16
std::vector< T > joinVectors(const std::vector< T > &v1, const std::vector< T > &v2, int pos, const T &default_value)
Definition: MatchTemplate.hpp:53