-
Notifications
You must be signed in to change notification settings - Fork 31
/
FireSight.hpp
309 lines (264 loc) · 11.5 KB
/
FireSight.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#ifndef FIRESIGHT_HPP
#define FIRESIGHT_HPP
#include "opencv2/features2d/features2d.hpp"
#include <vector>
#include <map>
#ifdef _MSC_VER
#include "winjunk.hpp"
#else
#define CLASS_DECLSPEC
#endif
#include "jansson.h"
using namespace cv;
using namespace std;
#if __amd64__ || __x86_64__ || _WIN64 || _M_X64
#define FIRESIGHT_64_BIT
#define FIRESIGHT_PLATFORM_BITS 64
#else
#define FIRESIGHT_32_BIT
#define FIRESIGHT_PLATFORM_BITS 32
#endif
namespace firesight {
CLASS_DECLSPEC typedef map<string,const char *> ArgMap;
CLASS_DECLSPEC extern ArgMap emptyMap;
typedef struct MatchedRegion {
Range xRange;
Range yRange;
Point2f average;
int pointCount;
float ellipse; // area of ellipse that covers {xRange, yRange}
float covar;
MatchedRegion(Range xRange, Range yRange, Point2f average, int pointCount, float covar);
string asJson();
json_t *as_json_t();
} MatchedRegion;
typedef class HoleRecognizer {
#define HOLE_SHOW_NONE 0 /* do not show matches */
#define HOLE_SHOW_MSER 1 /* show all MSER matches */
#define HOLE_SHOW_MATCHES 2 /* only show MSER matches that meet hole criteria */
public:
HoleRecognizer(float minDiameter, float maxDiameter);
/**
* Update the working image to show MSER matches.
* Image must have at least three channels representing RGB values.
* @param show matched regions. Default is HOLE_SHOW_NONE
*/
void showMatches(int show);
void scan(Mat &matRGB, vector<MatchedRegion> &matches, float maxEllipse = 1.05, float maxCovar = 2.0);
private:
int _showMatches;
MSER mser;
float minDiam;
float maxDiam;
int delta;
int minArea;
int maxArea;
float maxVariation;
float minDiversity;
int max_evolution;
float area_threshold;
float min_margin;
int edge_blur_size;
void scanRegion(vector<Point> &pts, int i,
Mat &matRGB, vector<MatchedRegion> &matches, float maxEllipse, float maxCovar);
} MSER_holes;
typedef struct Circle {
float x;
float y;
float radius;
Circle(float x, float y, float radius);
string asJson();
json_t *as_json_t();
} Circle;
typedef class HoughCircle {
#define CIRCLE_SHOW_NONE 0 /* do not show circles */
#define CIRCLE_SHOW_ALL 1 /* show all circles */
public:
HoughCircle(int minDiameter, int maxDiameter);
/**
* Update the working image to show detected circles.
* Image must have at least three channels representing RGB values.
* @param show matched regions. Default is CIRCLE_SHOW_NONE
*/
void setShowCircles(int show);
void scan(Mat &matRGB, vector<Circle> &circles);
void setFilterParams( int d, double sigmaColor, double sigmaSpace);
void setHoughParams(double dp, double minDist, double param1, double param2);
private:
int _showCircles;
int minDiam;
int maxDiam;
vector<Circle> circles;
void show(Mat & image, vector<Circle> circles);
// bilateral filter parameters
int bf_d;
double bf_sigmaColor;
double bf_sigmaSpace;
// HoughCircle parameters
double hc_dp;
double hc_minDist;
double hc_param1;
double hc_param2;
} HoughCircle;
typedef struct XY {
double x, y;
XY(): x(0), y(0) {}
XY(double x_, double y_): x(x_), y(y_) {}
} XY;
typedef class Pt2Res {
public:
Pt2Res() {}
double getResolution(double thr1, double thr2, double confidence, double separation, vector<XY> coords);
private:
static bool compare_XY_by_x(XY a, XY b);
static bool compare_XY_by_y(XY a, XY b);
int nsamples_RANSAC(size_t ninl, size_t xlen, unsigned int NSAMPL, double confidence);
static double _RANSAC_line(XY * x, size_t nx, XY C);
static double _RANSAC_pattern(XY * x, size_t nx, XY C);
vector<XY> RANSAC_2D(unsigned int NSAMPL, vector<XY> coords, double thr, double confidence, double(*err_fun)(XY *, size_t, XY));
void least_squares(vector<XY> xy, double * a, double * b);
} Pt2Res;
#ifdef LGPL2_1
typedef struct QRPayload {
double x, y;
string text;
json_t * as_json_t() {
json_t *pObj = json_object();
json_object_set(pObj, "x", json_real(x));
json_object_set(pObj, "y", json_real(y));
json_object_set(pObj, "text", json_string(text.c_str()));
return pObj;
}
string asJson() {
json_t *pObj = as_json_t();
char *pObjStr = json_dumps(pObj, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_INDENT(2));
string result(pObjStr);
return result;
}
} QRPayload;
typedef class ZbarQrDecode {
public:
ZbarQrDecode() {}
vector<QRPayload> scan(Mat &img, int show);
} ZbarQrDecode;
#endif // LGPL2_1
typedef class StageData {
public:
StageData(string stageName);
~StageData();
} StageData, *StageDataPtr;
typedef class Model {
private:
json_t *pJson;
public: // methods
Model(ArgMap &argMap=emptyMap);
~Model();
/**
* Return JSON root node of image recognition model.
* Caller must json_decref() returned value.
*/
inline json_t *getJson(bool incRef = true) {
if (incRef) {
return json_incref(pJson);
} else {
return pJson;
}
};
public: // fields
Mat image;
map<string, Mat> imageMap;
map<string, StageDataPtr> stageDataMap;
ArgMap argMap;
} Model;
typedef class CLASS_DECLSPEC Pipeline {
protected:
bool processModel(Model &model);
bool stageOK(const char *fmt, const char *errMsg, json_t *pStage, json_t *pStageModel);
KeyPoint _regionKeypoint(const vector<Point> ®ion);
void _eigenXY(const vector<Point> &pts, Mat &eigenvectorsOut, Mat &meanOut, Mat &covOut);
void _covarianceXY(const vector<Point> &pts, Mat &covOut, Mat &meanOut);
bool morph(json_t *pStage, json_t *pStageModel, Model &model, String mop, const char * fmt) ;
bool apply_absdiff(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_backgroundSubtractor(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_blur(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_matchTemplate(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_calcHist(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_calcOffset(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_Canny(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_circle(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_convertTo(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_cout(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_crop(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_cvtColor(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_dft(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_dftSpectrum(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_dilate(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_drawKeypoints(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_drawRects(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_equalizeHist(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_erode(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_FireSight(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_HoleRecognizer(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_HoughCircles(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_points2resolution_RANSAC(json_t *pStage, json_t *pStageModel, Model &model);
#ifdef LGPL2_1
bool apply_qrdecode(json_t *pStage, json_t *pStageModel, Model &model);
#endif // LGPL2_1
bool apply_imread(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_imwrite(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_log(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_Mat(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_matchGrid(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_meanStdDev(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_minAreaRect(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_model(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_morph(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_MSER(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_normalize(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_proto(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_PSNR(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_putText(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_rectangle(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_resize(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_threshold(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_warpRing(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_SimpleBlobDetector(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_sharpness(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_split(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_stageImage(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_transparent(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_undistort(const char *pName, json_t *pStage, json_t *pStageModel, Model &model);
bool apply_warpAffine(json_t *pStage, json_t *pStageModel, Model &model);
bool apply_warpPerspective(const char *pName, json_t *pStage, json_t *pStageModel, Model &model);
const char * dispatch(const char *pName, const char *pOp, json_t *pStage, json_t *pStageModel, Model &model);
void detectKeypoints(json_t *pStageModel, vector<vector<Point> > ®ions);
void detectRects(json_t *pStageModel, vector<vector<Point> > ®ions);
int parseCvType(const char *typeName, const char *&errMsg);
void validateImage(Mat &image);
json_t *pPipeline;
public:
enum DefinitionType { PATH, JSON };
/**
* Construct an image processing pipeline described by the given JSON array
* that specifies a sequence of named processing stages.
* @param pDefinition null terminated JSON string or file path
* @param indicates whether definition is JSON string or file path
*/
Pipeline(const char * pDefinition=NULL, DefinitionType defType=JSON );
/**
* Construct an image processing pipeline described by the given JSON array
* that specifies a sequence of named processing stages.
* @param pJson jansson array node
*/
Pipeline(json_t *pJson);
~Pipeline();
/**
* Process the given working image and return a JSON object that represents
* the recognized model comprised of the individual stage models.
* @param mat initial and transformed working image
* @return pointer to jansson root node of JSON object that has a field for each recognized stage model. E.g., {s1:{...}, s2:{...}, ... , sN:{...}}
*/
json_t *process(Mat &mat, ArgMap &argMap);
} Pipeline;
} // namespace firesight
#endif