Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LineMatcher.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003, 2004 (see file CONTACTS for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include "Base/ImageUtils/Scanline.hh"
23 #include <float.h>
24 
25 template <class StorageType>
26 int RegionMatcher::
27 LineMatcherNCC(const unsigned int p1[2], const unsigned int start2[2],
28  const unsigned int end2[2],
29  const StorageType **ida1, const StorageType **ida2,
30  const StorageType **grad1, const StorageType **grad2,
31  const unsigned int halfwinsize,
32  const unsigned int epsilon,
33  const double gradientscale,
34  unsigned int result[2],
35  double& correlation) const
36 {
37  int res=0;
38  Scanline scan(start2, end2, epsilon);
39  unsigned int next[2];
40  StorageType grad_threshold=
41  (StorageType)rint((double)grad1[p1[1]][p1[0]]*gradientscale);
42  double tmpcorr;
43 
44  correlation=-1.1;
45  while (scan.GetNext(next)){
46  BIASDOUT(D_REGION_MATCHER_LINE, "point ("<<next[0]<<", "<<next[1]<<")");
47  if (grad2[next[1]][next[0]]>=grad_threshold){
48  NCC(p1, next, ida1, ida2, halfwinsize, tmpcorr);
49 // cerr << "p1 ("<<p1[0]<<", "<<p1[1]<<")\tnext ("<<next[0]<<", "
50 // <<next[1]<<")\tcorr: "<<tmpcorr<<endl;
51  BIASDOUT(D_REGION_MATCHER_LINE, "gradient is big enough "
52  <<(int)(grad2[next[1]][next[0]])<<" > "<<gradientscale<<" * "
53  <<(int)(grad1[p1[1]][p1[0]])<<"\tcorrelation: "<<tmpcorr);
54  if (tmpcorr>correlation){
55  result[0]=next[0];
56  result[1]=next[1];
57  correlation=tmpcorr;
58  }
59  } else { // if (grad2[next[1]][next[0]]>grad_threshold){
60  BIASDOUT(D_REGION_MATCHER_LINE, "gradient too small "
61  <<(int)(grad2[next[1]][next[0]])<<" <= "<<gradientscale<<"*"
62  <<(int)(grad1[p1[1]][p1[0]])<<" = "<<(int)grad_threshold);
63  }
64  } // while
65  if (correlation<-1.0) res=-1;
66 
67  return res;
68 }
69 
70 
71 template <class StorageType> int
72 RegionMatcher::
73 LineMatcherSAD(const unsigned int p1[2], const unsigned int start2[2],
74  const unsigned int end2[2],
75  const StorageType **ida1, const StorageType **ida2,
76  const StorageType **grad1, const StorageType **grad2,
77  const unsigned int halfwinsize, const unsigned int epsilon,
78  const double gradientscale, unsigned int result[2],
79  double& sad)const
80 {
81  int res=0;
82  Scanline scan(start2, end2, epsilon);
83  unsigned int next[2];
84  StorageType grad_threshold=
85  (StorageType)rint((double)grad1[p1[1]][p1[0]]*gradientscale);
86  double tmpcorr;
87 
88  sad=DBL_MAX;
89  while (scan.GetNext(next)){
90  BIASDOUT(D_REGION_MATCHER_LINE, "point ("<<next[0]<<", "<<next[1]<<")");
91  if (grad2[next[1]][next[0]]>=grad_threshold){
92  SAD(p1, next, ida1, ida2, halfwinsize, tmpcorr);
93 // cerr << "p1 ("<<p1[0]<<", "<<p1[1]<<")\tnext ("<<next[0]<<", "
94 // <<next[1]<<")\tcorr: "<<tmpcorr<<endl;
95  BIASDOUT(D_REGION_MATCHER_LINE, "gradient is big enough "
96  <<(int)(grad2[next[1]][next[0]])<<" > "<<gradientscale<<" * "
97  <<(int)(grad1[p1[1]][p1[0]])<<"\tsad: "<<tmpcorr);
98  if (tmpcorr<sad){
99  result[0]=next[0];
100  result[1]=next[1];
101  sad=tmpcorr;
102  }
103  } else { // if (grad2[next[1]][next[0]]>grad_threshold){
104  BIASDOUT(D_REGION_MATCHER_LINE, "gradient too small "
105  <<(int)(grad2[next[1]][next[0]])<<" <= "<<gradientscale<<"*"
106  <<(int)(grad1[p1[1]][p1[0]])<<" = "<<(int)grad_threshold);
107  }
108  } // while
109  if (sad==DBL_MAX) res=-1;
110 
111  return res;
112 }
113 
114 
115 template <class StorageType> int
116 RegionMatcher::
117 LineMatcherSSD(const unsigned int p1[2], const unsigned int start2[2],
118  const unsigned int end2[2],
119  const StorageType **ida1, const StorageType **ida2,
120  const StorageType **grad1, const StorageType **grad2,
121  const unsigned int halfwinsize, const unsigned int epsilon,
122  const double gradientscale, unsigned int result[2],
123  double& ssd)const
124 {
125  int res=0;
126  Scanline scan(start2, end2, epsilon);
127  unsigned int next[2];
128  StorageType grad_threshold=
129  (StorageType)rint((double)grad1[p1[1]][p1[0]]*gradientscale);
130  double tmpcorr;
131 
132  ssd=DBL_MAX;
133  while (scan.GetNext(next)){
134  BIASDOUT(D_REGION_MATCHER_LINE, "point ("<<next[0]<<", "<<next[1]<<")");
135  if (grad2[next[1]][next[0]]>=grad_threshold){
136  SSD(p1, next, ida1, ida2, halfwinsize, tmpcorr);
137 // cerr << "p1 ("<<p1[0]<<", "<<p1[1]<<")\tnext ("<<next[0]<<", "
138 // <<next[1]<<")\tcorr: "<<tmpcorr<<endl;
139  BIASDOUT(D_REGION_MATCHER_LINE, "gradient is big enough "
140  <<(int)(grad2[next[1]][next[0]])<<" > "<<gradientscale<<" * "
141  <<(int)(grad1[p1[1]][p1[0]])<<"\tssd: "<<tmpcorr);
142  if (tmpcorr<ssd){
143  result[0]=next[0];
144  result[1]=next[1];
145  ssd=tmpcorr;
146  }
147  } else { // if (grad2[next[1]][next[0]]>grad_threshold){
148  BIASDOUT(D_REGION_MATCHER_LINE, "gradient too small "
149  <<(int)(grad2[next[1]][next[0]])<<" <= "<<gradientscale<<"*"
150  <<(int)(grad1[p1[1]][p1[0]])<<" = "<<(int)grad_threshold);
151  }
152  } // while
153  if (ssd==DBL_MAX) res=-1;
154 
155  return res;
156 }
Class for scanning a region given by a line and a distance ca.
Definition: Scanline.hh:44
bool GetNext(int next[2])
Definition: Scanline.hh:135