Debian Codegraph @

cvd/tensor_voting.h

raw symbols SCIP
1 #ifndef CVD_INC_TENSOR_VOTE_H
2 #define CVD_INC_TENSOR_VOTE_H
3
4 #include <TooN/TooN.h>
5 #include <TooN/helpers.h>
6 #include <cvd/image.h>
7 #include <utility>
8 #include <vector>
9
10 namespace CVD
11 {
12
13 #ifndef DOXYGEN_IGNORE_INTERNAL
14 namespace TensorVoting
15 {
16 struct TV_coord
17 {
18 std::ptrdiff_t o;
19 int x;
20 int y;
21 };
22
23 std::vector<std::pair<TV_coord, TooN::Matrix<2>>> compute_a_tensor_kernel(int radius, double cutoff, double angle, double sigma, double ratio, int row_stride);
24 inline unsigned int quantize_half_angle(double r, int num_divs)
25 {
26 return ((int)floor((r / M_PI + 100) * num_divs + 0.5)) % num_divs;
27 }
28 }
29
30 #endif
31
32 /**
33 This function performs tensor voting on the gradients of an image. The
34 voting is performed densely at each pixel, and the contribution of each
35 pixel is scaled by its gradient magnitude. The kernel for voting is
36 computed as follows. Consider that there is a point at \f$(0,0)\f$, with
37 gradient normal \f$(0,1)\f$. This will make a contribution to the point
38 \f$(x,y)\f$.
39
40 The arc-length, \f$l\f$, of the arc passing through \f$(0,0)\f$, tangent to
41 the gradient at this point and also passing through \f$(x, y)\f$ is:
42 \f[
43 l = 2 r \theta
44 \f]
45 Where
46 \f[
47 \theta = \tan^{-1}\frac{y}{x}
48 \f]
49 and the radius of the arc, \f$r\f$ is:
50 \f[
51 r = \frac{x^2 + y^2}{2y}.
52 \f]
53
54 The scale of the contribution is:
55 \f[
56 s = e^{-\frac{l^2}{\sigma^2} - \kappa\frac{\sigma^2}{r^2}}.
57 \f]
58 Note that this is achieved by scaling \f$x\f$ and \f$y\f$ by \f$\sigma\f$, so
59 \f$\kappa\f$ controls the kernel shape independent of the size.
60 The complete tensor contribution is therefore:
61 \f[
62 e^{-\frac{l^2}{\sigma^2} - \kappa\frac{\sigma^2}{r^2}}
63 \left[
64 \begin{array}{c}
65 \cos 2\theta\\
66 \sin 2\theta
67 \end{array}
68 \right]
69 [ \cos 2\theta\ \ \sin 2\theta]
70 \f]
71
72
73 @param image The image on which to perform tensor voting
74 @param sigma \f$ \sigma \f$
75 @param ratio \f$ \kappa \f$
76 @param cutoff When \f$s\f$ points drop below the cutoff, it is set to zero.
77 @param num_divs The voting kernels are quantized by angle in to this many dicisions in the half-circle.
78 @ingroup gVision
79 **/
80 template <class C>
81 Image<TooN::Matrix<2>> dense_tensor_vote_gradients(const BasicImage<C>& image, double sigma, double ratio, double cutoff = 0.001, unsigned int num_divs = 4096)
82 {
83 using std::pair;
84 using std::vector;
85 using TensorVoting::TV_coord;
86 using TooN::Matrix;
87
88 Matrix<2> zero(TooN::Zeros);
89 Image<Matrix<2>> field(image.size(), zero);
90
91 //Kernel values go as exp(-x*x / sigma * sigma)
92 //So, for cutoff = exp(-x*x / sigma * sigma)
93 //ln cutoff = -x*x / sigma*sigma
94 //x = sigma * sqrt(-ln cutoff)
95 int kernel_radius = (int)ceil(sigma * sqrt(-log(cutoff)));
96
97 //First, build up the kernels
98 vector<vector<pair<TV_coord, Matrix<2>>>> kernels;
99 for(unsigned int i = 0; i < num_divs; i++)
100 {
101 double angle = M_PI * i / num_divs;
102 kernels.push_back(TensorVoting::compute_a_tensor_kernel(kernel_radius, cutoff, angle, sigma, ratio, field.row_stride()));
103 }
104
105 for(int y = kernel_radius; y < field.size().y - kernel_radius; y++)
106 for(int x = kernel_radius; x < field.size().x - kernel_radius; x++)
107 {
108 double gx = ((double)image[y][x + 1] - image[y][x - 1]) / 2.;
109 double gy = ((double)image[y + 1][x] - image[y - 1][x]) / 2.;
110
111 double scale = sqrt(gx * gx + gy * gy);
112 unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan2(gy, gx), num_divs);
113
114 const vector<pair<TV_coord, Matrix<2>>>& kernel = kernels[direction];
115
116 Matrix<2>* p = &field[y][x];
117
118 //The matrices are all symmetric, so only use the upper right triangle.
119 for(unsigned int i = 0; i < kernel.size(); i++)
120 {
121 p[kernel[i].first.o][0][0] += kernel[i].second[0][0] * scale;
122 p[kernel[i].first.o][0][1] += kernel[i].second[0][1] * scale;
123 p[kernel[i].first.o][1][1] += kernel[i].second[1][1] * scale;
124 }
125 }
126
127 //Now do the edges
128 for(int y = 1; y < field.size().y - 1; y++)
129 {
130 for(int x = 1; x < field.size().x - 1; x++)
131 {
132 //Skip the middle bit
133 if(y >= kernel_radius && y < field.size().y - kernel_radius && x == kernel_radius)
134 x = field.size().x - kernel_radius;
135
136 double gx = ((double)image[y][x + 1] - image[y][x - 1]) / 2.;
137 double gy = ((double)image[y + 1][x] - image[y - 1][x]) / 2.;
138
139 double scale = sqrt(gx * gx + gy * gy);
140 unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan(gy / gx), num_divs);
141
142 const vector<pair<TV_coord, Matrix<2>>>& kernel = kernels[direction];
143
144 Matrix<2>* p = &field[y][x];
145
146 //The matrices are all symmetric, so only use the upper right triangle.
147 for(unsigned int i = 0; i < kernel.size(); i++)
148 {
149 if(kernel[i].first.y + y >= 0 && kernel[i].first.y + y < field.size().y && kernel[i].first.x + x >= 0 && kernel[i].first.x + x < field.size().x)
150 {
151 p[kernel[i].first.o][0][0] += kernel[i].second[0][0] * scale;
152 p[kernel[i].first.o][0][1] += kernel[i].second[0][1] * scale;
153 p[kernel[i].first.o][1][1] += kernel[i].second[1][1] * scale;
154 }
155 }
156 }
157 }
158
159 //Copy over bits to make the matrices symmetric
160 for(Image<Matrix<2>>::iterator i = field.begin(); i != field.end(); i++)
161 (*i)[1][0] = (*i)[0][1];
162
163 return field;
164 }
165
166 #ifdef CVD_EXPERIMENTAL
167
168 template <class C>
169 Image<TooN::Matrix<2>> dense_tensor_vote_gradients_fast(const BasicImage<C>& image, double sigma, double ratio, double cutoff = 0.001, int num_divs = 4096)
170 {
171 using std::make_pair;
172 using std::pair;
173 using std::vector;
174 using TensorVoting::TV_coord;
175 using TooN::Matrix;
176
177 Matrix<2> zero(TooN::Zeros);
178 Image<Matrix<2>> ffield(image.size(), zero);
179 Image<__m128> field(image.size());
180 field.zero();
181
182 //In much the same way as dense_tensor_vote_gradients, build up the kernel.
183 int kernel_radius = (int)ceil(sigma * sqrt(-log(cutoff)));
184 vector<vector<pair<TV_coord, Matrix<2>>>> matrix_kernels;
185 for(int i = 0; i < num_divs; i++)
186 {
187 double angle = M_PI * i / num_divs;
188 matrix_kernels.push_back(TensorVoting::compute_a_tensor_kernel(kernel_radius, cutoff, angle, sigma, ratio, field.row_stride()));
189 }
190
191 //Put the kernel in aligned SSE registers.
192 //Image<__m128> is used since it guarantees SSE aligned memory.
193 vector<vector<int>> kernel_offsets;
194 vector<Image<__m128>> kernel_values;
195 for(unsigned int i = 0; i < matrix_kernels.size(); i++)
196 {
197 vector<int> off(matrix_kernels[i].size());
198 Image<__m128> val(ImageRef(matrix_kernels[i].size(), 1));
199
200 for(unsigned int j = 0; j < matrix_kernels[i].size(); j++)
201 {
202 off[j] = matrix_kernels[i][j].first.o;
203 Matrix<2>& m = matrix_kernels[i][j].second;
204 val.data()[j] = _mm_setr_ps(m[0][0], m[0][1], m[1][0], m[1][1]);
205 }
206
207 kernel_offsets.push_back(off);
208 kernel_values.push_back(val);
209 }
210
211 #pragma omp parallel for
212 for(int y = kernel_radius; y < field.size().y - kernel_radius; y++)
213 for(int x = kernel_radius; x < field.size().x - kernel_radius; x++)
214 {
215 float gx = ((float)image[y][x + 1] - image[y][x - 1]) / 2.;
216 float gy = ((float)image[y + 1][x] - image[y - 1][x]) / 2.;
217
218 float scale = sqrt(gx * gx + gy * gy);
219 unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan(gy / gx), num_divs);
220
221 const vector<int>& off = kernel_offsets[direction];
222 __m128* val = kernel_values[direction].data();
223 __m128* p = &field[y][x];
224 __m128 s = _mm_set1_ps(scale);
225
226 for(unsigned int i = 0; i < off.size(); i++)
227 p[off[i]] = _mm_add_ps(p[off[i]], _mm_mul_ps(val[i], s));
228 }
229
230 for(int y = 0; y < field.size().y; y++)
231 for(int x = 0; x < field.size().x; x++)
232 {
233 float f[4];
234 _mm_storeu_ps(f, field[y][x]);
235 ffield[y][x][0][0] = f[0];
236 ffield[y][x][0][1] = f[1];
237 ffield[y][x][1][0] = f[2];
238 ffield[y][x][1][1] = f[3];
239 }
240
241 return ffield;
242 }
243 #endif
244
245 }
246
247 #endif