Debian Codegraph @

tests/fast_corner_test.cc

raw symbols SCIP
1 #include <algorithm>
2 #include <cstdlib>
3 #include <cvd/fast_corner.h>
4 #include <cvd/image_io.h>
5 #include <iostream>
6 #include <iterator>
7 #include <random>
8 #include <utility>
9
10 using std::cout;
11 using std::endl;
12 using std::string;
13 using std::vector;
14
15 namespace CVD
16 {
17 void fast_corner_detect_plain_9(const BasicImage<byte>& i, vector<ImageRef>& corners, int b);
18 void fast_corner_detect_plain_10(const BasicImage<byte>& i, vector<ImageRef>& corners, int b);
19 void fast_corner_detect_plain_12(const BasicImage<byte>& i, vector<ImageRef>& corners, int b);
20 }
21
22 using namespace CVD;
23
24 inline bool positive(int val, int centre, int barrier)
25 {
26 return val > centre + barrier;
27 }
28
29 inline bool negative(int val, int centre, int barrier)
30 {
31 return val < centre - barrier;
32 }
33
34 template <int num_for_corner, class Test>
35 inline int is_corner(const BasicImage<byte>& im, const ImageRef off, int barrier, const Test& test)
36 {
37 int num_consecutive = 0;
38 int first_cons = 0;
39 const int centre = im[off];
40
41 for(int i = 0; i < 16; i++)
42 {
43 int val = im[fast_pixel_ring[i] + off];
44 if(test(val, centre, barrier))
45 {
46 num_consecutive++;
47
48 if(num_consecutive == num_for_corner)
49 return 1;
50 }
51 else
52 {
53 if(num_consecutive == i)
54 first_cons = i;
55
56 num_consecutive = 0;
57 }
58 }
59
60 return first_cons + num_consecutive >= num_for_corner;
61 }
62
63 template <int Num>
64 void segment_test(const BasicImage<byte>& im, vector<ImageRef>& v, int threshold)
65 {
66 for(int y = 3; y < im.size().y - 3; y++)
67 for(int x = 3; x < im.size().x - 3; x++)
68 if(is_corner<Num>(im, ImageRef(x, y), threshold, positive) || is_corner<Num>(im, ImageRef(x, y), threshold, negative))
69 v.push_back(ImageRef(x, y));
70 }
71
72 template <class A, class B, class C>
73 void test(const SubImage<byte>& i, A funcf, B funcp, C funcs, int threshold, string type)
74 {
75
76 vector<ImageRef> faster, normal, simple;
77
78 funcp(i, normal, threshold);
79 funcf(i, faster, threshold);
80 funcs(i, simple, threshold);
81
82 sort(normal.begin(), normal.end());
83 sort(faster.begin(), faster.end());
84 sort(simple.begin(), simple.end());
85
86 vector<ImageRef> all;
87 copy(normal.begin(), normal.end(), back_inserter(all));
88 copy(faster.begin(), faster.end(), back_inserter(all));
89 copy(simple.begin(), simple.end(), back_inserter(all));
90
91 sort(all.begin(), all.end());
92 vector<ImageRef>::iterator new_end = unique(all.begin(), all.end());
93 all.resize(new_end - all.begin());
94
95 vector<ImageRef> bad;
96 set_symmetric_difference(faster.begin(), faster.end(), simple.begin(), simple.end(), back_inserter(bad));
97 set_symmetric_difference(normal.begin(), normal.end(), simple.begin(), simple.end(), back_inserter(bad));
98
99 if(bad.empty())
100 return;
101
102 cout << "*********************************************" << type << endl;
103 cout << "Size: " << i.size() << " threshold: " << threshold << " ";
104 cout << normal.size() << " " << faster.size() << " " << simple.size() << " " << all.size() << " ";
105 cout << "fail." << endl;
106
107 exit(1);
108 }
109
110 template <class A, class B, class C>
111 void test_images(const SubImage<byte>& im, A funcf, B funcp, C funcs, int threshold, string type)
112 {
113 ImageRef one(1, 1);
114 ImageRef zero(0, 0);
115
116 ImageRef d[] = { { 1, 1 }, { 1, 0 }, { 0, 1 } };
117
118 for(int i = 0; i < 16; i++)
119 {
120 for(const auto& one : d)
121 {
122 ImageRef size = im.size() - i * one;
123
124 if(size.x <= 0 || size.y <= 0)
125 continue;
126
127 Image<byte> part(size);
128 BasicImage<byte> s = im.sub_image(zero, size);
129 //copy(s.begin(), s.end(),part.begin());
130
131 test(s, funcf, funcp, funcs, threshold, type);
132 }
133 }
134 }
135
136 int main(int, char**)
137 {
138 std::random_device device;
139 std::ranlux48 engine(device());
140 std::uniform_real_distribution<> distribution(0.0, 1.0);
141
142 for(int n = 16; n < 100; n += 1)
143 {
144 Image<byte> im(ImageRef(n, n));
145
146 for(Image<byte>::iterator i = im.begin(); i != im.end(); i++)
147 *i = static_cast<byte>((distribution(engine) * 256));
148
149 for(int k = 0; k < 2; k++)
150 {
151 ImageRef start;
152 start.x = static_cast<byte>(distribution(engine) * (n / 2));
153 start.y = static_cast<byte>(distribution(engine) * (n / 2));
154
155 ImageRef size = im.size() - start;
156 size.x = static_cast<byte>(distribution(engine) * (size.x - 1) + 1);
157 size.y = static_cast<byte>(distribution(engine) * (size.y - 1) + 1);
158
159 SubImage<byte> ims = im.sub_image(start, size);
160
161 int threshold = static_cast<byte>(distribution(engine) * 256);
162 test_images(ims, fast_corner_detect_9, fast_corner_detect_plain_9, segment_test<9>, threshold, "FAST9");
163 test_images(ims, fast_corner_detect_10, fast_corner_detect_plain_10, segment_test<10>, threshold, "FAST10");
164 test_images(ims, fast_corner_detect_12, fast_corner_detect_plain_12, segment_test<12>, threshold, "FAST12");
165 }
166 }
167 }