Debian Codegraph @

cvd/interpolate.h

raw symbols SCIP
1
2 #ifndef CVD_INC_INTERPOLATE_H
3 #define CVD_INC_INTERPOLATE_H
4
5 #include <cvd/config.h>
6 #include <cvd/image.h>
7 #include <cvd/vector_image_ref.h>
8 #include <cvd/vision.h>
9
10 namespace CVD
11 {
12 ///Interploate a 1D local extremem by fitting a quadratic tho the three data points and interpolating.
13 ///The middle argument must be the most extreme, and the extremum
14 ///position is returned relative to 0.
15 ///
16 ///Arguments are checked for extremeness by means of assert.
17 ///
18 ///@param d1 Data point value for $x=-1$
19 ///@param d2 Data point value for $x=0$
20 ///@param d3 Data point value for $x=1$
21 ///@return The $x$ coordinate of the extremum.
22 ///@ingroup gVision
23 inline double interpolate_extremum(double d1, double d2, double d3)
24 {
25 assert(d2 >= d1 && d2 > d3 || d2 > d1 && d2 >= d3 || d2 <= d1 && d2 < d3 || d2 < d1 && d2 <= d3);
26 //Use Quadratic interpolation to find the peak, position
27 //and hence the "real" edge position.
28 return -0.5 * (d3 - d1) / (d3 + d1 - 2 * d2);
29 }
30
31 #if defined CVD_HAVE_TOON || defined DOXYGEN_IGNORE_INTERNAL
32
33 ///Interpolate a 2D local maximum, by fitting a quadratic. This is done by using
34 ///using the 9 datapoints to compute the local Hessian using finite differences and
35 ///finding the location where the gradient is zero.
36 ///
37 /// Given the grid of pixels:
38 /// <pre>
39 /// a b c
40 /// d e f
41 /// g h i
42 /// </pre>
43 /// The centre pixel (e) must be the most extreme of all the pixels.
44 ///
45 ///@param I__1__1 Pixel $(-1, -1)$ relative to the centre (a)
46 ///@param I__1_0 Pixel $(-1, 0)$ relative to the centre (b)
47 ///@param I__1_1 Pixel $(-1, 1)$ relative to the centre (c)
48 ///@param I_0__1 Pixel $( 0, -1)$ relative to the centre (d)
49 ///@param I_0_0 Pixel $( 0, 0)$ relative to the centre (e)
50 ///@param I_0_1 Pixel $( 0, 1)$ relative to the centre (f)
51 ///@param I_1__1 Pixel $( 1, -1)$ relative to the centre (g)
52 ///@param I_1_0 Pixel $( 1, 0)$ relative to the centre (h)
53 ///@param I_1_1 Pixel $( 1, 1)$ relative to the centre (i)
54 ///@return Location of the local extrema.
55 ///@ingroup gVision
56 inline TooN::Vector<2> interpolate_extremum(double I__1__1,
57 double I__1_0,
58 double I__1_1,
59 double I_0__1,
60 double I_0_0,
61 double I_0_1,
62 double I_1__1,
63 double I_1_0,
64 double I_1_1)
65 {
66 //Compute the gradient values
67 double gx = 0.5 * (I_1_0 - I__1_0);
68 double gy = 0.5 * (I_0_1 - I_0__1);
69
70 //Compute the Hessian values
71 double gxx = I_1_0 - 2 * I_0_0 + I__1_0;
72 double gyy = I_0_1 - 2 * I_0_0 + I_0__1;
73 double gxy = 0.25 * (I_1_1 + I__1__1 - I_1__1 - I__1_1);
74
75 //Compute -inv(H) * grad
76
77 double Dinv = 1. / (gxx * gyy - gxy * gxy);
78
79 TooN::Vector<2> v;
80 v[0] = -Dinv * (gyy * gx - gxy * gy);
81 v[1] = -Dinv * (-gxy * gx + gxx * gy);
82
83 return v;
84 }
85
86 ///Interpolate a 2D local maximum, by fitting a quadratic.
87 ///@param i Image in which to interpolate extremum
88 ///@param p Point at which to interpolate extremum
89 ///@return Location of local extremum in image coordinates
90 ///@ingroup gVision
91 template <class I>
92 inline TooN::Vector<2> interpolate_extremum(const BasicImage<I>& i, ImageRef p)
93 {
94 CVD_IMAGE_ASSERT(p.x > 0 && p.y > 0 && p.x < i.size().x - 1 && p.y < i.size().y - 1, ImageError::AccessOutsideImage);
95
96 //Extract and label 9 particular points
97 double I__1__1 = i[p + ImageRef(-1, -1)];
98 double I__1_0 = i[p + ImageRef(-1, 0)];
99 double I__1_1 = i[p + ImageRef(-1, 1)];
100 double I_0__1 = i[p + ImageRef(0, -1)];
101 double I_0_0 = i[p + ImageRef(0, 0)];
102 double I_0_1 = i[p + ImageRef(0, 1)];
103 double I_1__1 = i[p + ImageRef(1, -1)];
104 double I_1_0 = i[p + ImageRef(1, 0)];
105 double I_1_1 = i[p + ImageRef(1, 1)];
106 return interpolate_extremum(I__1__1, I__1_0, I__1_1, I_0__1, I_0_0, I_0_1, I_1__1, I_1_0, I_1_1) + vec(p);
107 }
108
109 #endif
110
111 ///Interpolate a 2D local maximum, by fitting a quadratic. This is done by using
112 ///using the 9 datapoints to compute the local Hessian using finite differences and
113 ///finding the location where the gradient is zero. This version returns also the
114 /// value of the extremum.
115 ///
116 /// Given the grid of pixels:
117 /// <pre>
118 /// a b c
119 /// d e f
120 /// g h i
121 /// </pre>
122 /// The centre pixel (e) must be the most extreme of all the pixels.
123 ///
124 ///@param I__1__1 Pixel $(-1, -1)$ relative to the centre (a)
125 ///@param I__1_0 Pixel $(-1, 0)$ relative to the centre (b)
126 ///@param I__1_1 Pixel $(-1, 1)$ relative to the centre (c)
127 ///@param I_0__1 Pixel $( 0, -1)$ relative to the centre (d)
128 ///@param I_0_0 Pixel $( 0, 0)$ relative to the centre (e)
129 ///@param I_0_1 Pixel $( 0, 1)$ relative to the centre (f)
130 ///@param I_1__1 Pixel $( 1, -1)$ relative to the centre (g)
131 ///@param I_1_0 Pixel $( 1, 0)$ relative to the centre (h)
132 ///@param I_1_1 Pixel $( 1, 1)$ relative to the centre (i)
133 ///@return pair containing Location of the local extrema and the value
134 ///@ingroup gVision
135 inline std::pair<TooN::Vector<2>, double> interpolate_extremum_value(double I__1__1,
136 double I__1_0,
137 double I__1_1,
138 double I_0__1,
139 double I_0_0,
140 double I_0_1,
141 double I_1__1,
142 double I_1_0,
143 double I_1_1)
144 {
145 //Compute the gradient values
146 double gx = 0.5 * (I_1_0 - I__1_0);
147 double gy = 0.5 * (I_0_1 - I_0__1);
148
149 //Compute the Hessian values
150 double gxx = I_1_0 - 2 * I_0_0 + I__1_0;
151 double gyy = I_0_1 - 2 * I_0_0 + I_0__1;
152 double gxy = 0.25 * (I_1_1 + I__1__1 - I_1__1 - I__1_1);
153
154 //Compute -inv(H) * grad
155
156 double Dinv = 1. / (gxx * gyy - gxy * gxy);
157
158 TooN::Vector<2> v;
159 v[0] = -Dinv * (gyy * gx - gxy * gy);
160 v[1] = -Dinv * (-gxy * gx + gxx * gy);
161
162 double value = I_0_0 + (gx + 0.5 * gxx * v[0] + gxy * v[1]) * v[0] + (gy + 0.5 * gyy * v[1]) * v[1];
163
164 return std::make_pair(v, value);
165 }
166
167 ///Interpolate a 2D local maximum, by fitting a quadratic.
168 ///@param i Image in which to interpolate extremum
169 ///@param p Point at which to interpolate extremum
170 ///@return pair containing Location of local extremum in image coordinates and the value of the extemum
171 ///@ingroup gVision
172 template <class I>
173 inline std::pair<TooN::Vector<2>, double> interpolate_extremum_value(const BasicImage<I>& i, ImageRef p)
174 {
175 CVD_IMAGE_ASSERT(p.x > 0 && p.y > 0 && p.x < i.size().x - 1 && p.y < i.size().y - 1, ImageError::AccessOutsideImage);
176
177 //Extract and label 9 particular points
178 double I__1__1 = i[p + ImageRef(-1, -1)];
179 double I__1_0 = i[p + ImageRef(-1, 0)];
180 double I__1_1 = i[p + ImageRef(-1, 1)];
181 double I_0__1 = i[p + ImageRef(0, -1)];
182 double I_0_0 = i[p + ImageRef(0, 0)];
183 double I_0_1 = i[p + ImageRef(0, 1)];
184 double I_1__1 = i[p + ImageRef(1, -1)];
185 double I_1_0 = i[p + ImageRef(1, 0)];
186 double I_1_1 = i[p + ImageRef(1, 1)];
187 std::pair<TooN::Vector<2>, double> result = interpolate_extremum_value(I__1__1, I__1_0, I__1_1, I_0__1, I_0_0, I_0_1, I_1__1, I_1_0, I_1_1);
188 result.first += vec(p);
189 return result;
190 }
191
192 }
193
194 #endif