-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathFASTex.hpp
288 lines (241 loc) · 6.72 KB
/
FASTex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* FASTex.hpp
*
* Created on: Dec 15, 2015
* Author: Michal.Busta at gmail.com
*
* Copyright (c) 2015, Michal Busta, Lukas Neumann, Jiri Matas.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Based on:
*
* FASText: Efficient Unconstrained Scene Text Detector,Busta M., Neumann L., Matas J.: ICCV 2015.
* Machine learning for high-speed corner detection, E. Rosten and T. Drummond, ECCV 2006
*/
#ifndef CMP_FAST_HPP_
#define CMP_FAST_HPP_
#include "KeyPoints.h"
#include <unordered_map>
#include <vector>
namespace cmp{
inline long ColourDistance(const uchar* e1, const uchar* e2)
{
int ur1 = e1[2];
int ur2 = e2[2];
long rmean = ( ur1 + ur2 ) / 2;
long r = ur1 - ur2;
int ug1 = e1[1];
int ug2 = e2[1];
long g = ug1 - ug2;
int ub1 = e1[0];
int ub2 = e2[0];
long b = ub1 - ub2;
return (((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8);
}
inline long ColourDistanceMAX(const uchar* e1, const uchar* e2, uchar& sign)
{
int d1 = e1[0] - (int) e2[0];
int ad1 = abs(d1);
int d2 = e1[0] - (int) e2[0];
int ad2 = abs(d2);
int d3 = e1[0] - (int) e2[0];
int ad3 = abs(d3);
if(ad1 > ad2)
{
if(ad1 > ad3)
{
sign = d1 > 0;
return ad1;
}
sign = d3 > 0;
return ad3;
}else
{
if(ad2 > ad3)
{
sign = d2 > 0;
return ad2;
}
sign = d3 > 0;
return ad3;
}
}
inline long ColourDistanceVec(const cv::Vec3b& e1, const cv::Vec3b& e2)
{
int ur1 = e1[2];
int ur2 = e2[2];
long rmean = ( ur1 + ur2 ) / 2;
long r = ur1 - ur2;
int ug1 = e1[1];
int ug2 = e2[1];
long g = ug1 - ug2;
int ub1 = e1[0];
int ub2 = e2[0];
long b = ub1 - ub2;
return (((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8);
}
inline long ColourDistanceGray(const uchar& e1, const uchar& e2)
{
return e2 - e1;
}
template<int channel>
inline long ColourDistanceRGB(const cv::Vec3b& e1, const cv::Vec3b& e2)
{
return e2[channel] - e1[channel];
}
template<int channel>
inline long ColourDistanceRGBP(const uchar& e1, const uchar& e2)
{
return (&e2)[channel] - (&e1)[channel];
}
template<int channel>
inline long ColourDistanceRGBIP(const uchar& e1, const uchar& e2)
{
return (&e1)[channel] - (&e2)[channel];
}
inline long ColourDistanceGrayABS(const uchar* e1, const uchar* e2)
{
return abs(((int) *e2) - *e1);
}
inline long ColourDistanceGrayP(const uchar* e1, const uchar* e2)
{
return (*e2 - *e1);
}
inline long ColourDistanceGrayI(const uchar& e1, const uchar& e2)
{
return e1 - e2;
}
template<int channel>
inline long ColourDistanceRGBI(const cv::Vec3b& e1, const cv::Vec3b& e2)
{
return (e1)[channel] - (e2)[channel];
}
inline long ColourDistanceGrayIP(const uchar* e1, const uchar* e2)
{
return ( *e1 - *e2);
}
inline long ColourDistanceGrayNorm(const uchar* e1, const uchar* e2)
{
int ur1 = e1[0];
int ur2 = e2[0];
long rmean = ( ur1 + ur2 ) / 2;
long r = ur1 - ur2;
return (((512+rmean)*r*r)>>8);
}
inline int getValueCorner12(const uchar * ptr, int* pixel, int* corners, const int& k, const int& ks, const uchar& (*dist)(const uchar&, const uchar&) )
{
int x = ptr[pixel[k]];
if( k == 3 && ks != 2 && ks != 3 ){
x = dist(x, ptr[corners[0]]);
}else if(k == 5 && ks != 4 && ks != 5){
x = dist(x, ptr[corners[1]]);
}else if(k == 8 && ks != 7 && ks != 8){
x = dist(x, ptr[corners[2]]);
}else if(k == 11 && ks != 11){
x = dist(x, ptr[corners[3]]);
}
return x;
}
inline void getCrossCorner12(const uchar * ptr, int* corners, int* cornersOut, const int& k, int& k1, int& k2, const uchar& (*dist)(const uchar&, const uchar&) )
{
switch(k){
case 0:
case 1:
case 11:
k1 = dist(ptr[cornersOut[1]], ptr[corners[1]]);
k2 = dist(ptr[cornersOut[2]], ptr[corners[2]]);
break;
case 2:
case 3:
case 4:
k1 = dist(ptr[cornersOut[2]], ptr[corners[2]]);
k2 = dist(ptr[cornersOut[3]], ptr[corners[3]]);
break;
case 5:
case 6:
case 7:
k1 = dist(ptr[cornersOut[0]], ptr[corners[0]]);
k2 = dist(ptr[cornersOut[3]], ptr[corners[3]]);
break;
case 8:
case 9:
case 10:
k1 = dist(ptr[cornersOut[0]], ptr[corners[0]]);
k2 = dist(ptr[cornersOut[1]], ptr[corners[1]]);
break;
}
}
/**
* The interface method
*/
class CV_EXPORTS_W FASTextI
{
public:
enum
{
KEY_POINTS_BLACK = 0, KEY_POINTS_WHITE = 1, KEY_POINTS_ALL = 3
};
CV_WRAP FASTextI( long threshold = 10, bool nonmaxSuppression=true, int keypointsTypes = KEY_POINTS_ALL, int Kmin = 9, int Kmax = 11);
virtual ~FASTextI(){
};
void detect( const cv::Mat& image, std::vector<FastKeyPoint>& keypoints, const cv::Mat& mask ) const
{
keypoints.clear();
if( image.empty() )
return;
CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size()) );
detectImpl( image, keypoints, mask );
}
void segment( const cv::Mat& image, std::vector<FastKeyPoint>& keypoints, std::unordered_multimap<int, std::pair<int, int> >& keypointsPixels, const cv::Mat& mask ) const
{
keypoints.clear();
if( image.empty() )
return;
CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size()) );
segmentImpl( image, keypoints, keypointsPixels, mask );
}
virtual bool isColorDetector(){
return false;
}
void setThreshold(long threshold){
this->threshold = threshold;
}
void setKeypointsTypes(int keypointsTypes){
this->keypointsTypes = keypointsTypes;
}
protected:
virtual void detectImpl( const cv::Mat& image, std::vector<FastKeyPoint>& keypoints, const cv::Mat& mask=cv::Mat() ) const = 0;
virtual void segmentImpl( const cv::Mat& image, std::vector<FastKeyPoint>& keypoints, std::unordered_multimap<int, std::pair<int, int> >& keypointsPixels, const cv::Mat& mask=cv::Mat() ) const
{
detectImpl( image, keypoints, mask);
}
long threshold;
bool nonmaxSuppression;
int Kmin;
int Kmax;
int keypointsTypes;
std::vector<std::vector<float> > fastAngles;
};
/**
* Gray level FASText Feature detector
*/
class CV_EXPORTS_W FASTextGray : public FASTextI
{
public:
CV_WRAP FASTextGray( long threshold=10, bool nonmaxSuppression=true, int keypointsTypes = KEY_POINTS_ALL, int Kmin = 9, int Kmax = 11);
virtual ~FASTextGray(){
};
protected:
virtual void detectImpl( const cv::Mat& image, std::vector<FastKeyPoint>& keypoints, const cv::Mat& mask=cv::Mat() ) const;
};
}//namespace cmp;
#endif /* FAST_HPP_ */