Debian Codegraph @

cvd_src/tensor_voting.cc

raw symbols SCIP
1 #include <TooN/TooN.h>
2 #include <TooN/helpers.h>
3 #include <cvd/image.h>
4 #include <cvd/tensor_voting.h>
5 #include <utility>
6 #include <vector>
7
8 using namespace TooN;
9 using namespace std;
10
11 namespace CVD
12 {
13 namespace TensorVoting
14 {
15
16 Matrix<2> rot(double angle)
17 {
18 Matrix<2> v;
19 v[0] = makeVector(cos(angle), sin(angle));
20 v[1] = makeVector(-sin(angle), cos(angle));
21 return v;
22 }
23
24 //See the tensor voting documentation for a description of the maths
25 pair<Matrix<2>, double> tensor_kernel_element(Vector<2>& g, int x1, int y1, double sigma, double ratio)
26 {
27 double x = x1 * g[0] + y1 * g[1];
28 double y = x1 * g[1] - y1 * g[0];
29 x /= sigma;
30 y /= sigma;
31
32 if(y == 0)
33 {
34 Matrix<2> t = g.as_col() * g.as_row();
35
36 if(x == 0)
37 return make_pair(t, 1);
38 else
39 return make_pair(t, exp(-(x * x)));
40 }
41 else
42 {
43 double k = 2 * y / (x * x + y * y);
44 double r = 1 / k;
45 double theta = atan(y / x);
46 double arclen = 2 * theta * r;
47
48 double scale = exp(-(arclen * arclen + ratio * k * k));
49 Vector<2> d = rot(2 * theta) * g;
50
51 return make_pair(d.as_col() * d.as_row(), scale);
52 }
53 }
54
55 //Borrowed from the tag library.
56 template <class A, class B>
57 struct refpair
58 {
59 A& a;
60 B& b;
61 refpair(A& aa, B& bb)
62 : a(aa)
63 , b(bb)
64 {
65 }
66
67 void operator=(const pair<A, B>& p)
68 {
69 a = p.first;
70 b = p.second;
71 }
72 };
73
74 template <class A, class B>
75 refpair<A, B> rpair(A& aa, B& bb)
76 {
77 return refpair<A, B>(aa, bb);
78 }
79
80 TV_coord make(int x, int y, int s)
81 {
82 TV_coord t;
83 t.x = x;
84 t.y = y;
85 t.o = (ptrdiff_t)s * y + x;
86 return t;
87 }
88
89 //Compute a kernel, with small values set to zero, with pointer offsets
90 //for the nonzero elements.
91 vector<pair<TV_coord, Matrix<2>>> compute_a_tensor_kernel(int radius, double cutoff, double angle, double sigma, double ratio, int row_stride)
92 {
93
94 vector<pair<TV_coord, Matrix<2>>> ret;
95
96 Vector<2> g;
97 g[0] = cos(angle);
98 g[1] = sin(angle);
99
100 for(int y = -radius; y <= radius; y++)
101 for(int x = -radius; x <= radius; x++)
102 {
103 Matrix<2> tensor;
104 double scale;
105 rpair(tensor, scale) = tensor_kernel_element(g, x, y, sigma, ratio);
106
107 if(scale >= cutoff)
108 ret.push_back(make_pair(make(x, y, row_stride), scale * tensor));
109 }
110
111 return ret;
112 }
113 }
114 }