-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssgproc.cpp
339 lines (259 loc) · 9.95 KB
/
ssgproc.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "ssgproc.h"
#include "TSC/imageprocess/imageprocess.h"
#include "TSC/bubble/bubbleprocess.h"
SSGProc::SSGProc()
{
}
// Given a node signature and its weight, it's value is updated
// with new node signature values.
void SSGProc::updateNodeSig(pair<NodeSig, int>& ns, NodeSig new_ns)
{
//Areas
ns.first.area = ns.first.area * ns.second +
new_ns.area;
ns.first.area /= ns.second + 1;
//Color - R
ns.first.colorR = ns.first.colorR * ns.second +
new_ns.colorR;
ns.first.colorR /= ns.second + 1;
//Color - G
ns.first.colorG = ns.first.colorG * ns.second +
new_ns.colorG;
ns.first.colorG /= ns.second + 1;
//Color - B
ns.first.colorB = ns.first.colorB * ns.second +
new_ns.colorB;
ns.first.colorB /= ns.second + 1;
//Center
ns.first.center.x = ns.first.center.x * ns.second +
new_ns.center.x;
ns.first.center.y = ns.first.center.y * ns.second +
new_ns.center.y;
ns.first.center.x /= (float)ns.second + 1;
ns.first.center.y /= (float)ns.second + 1;
#ifdef BOW_APPROACH_USED
//BOW hist
if(ns.second == 0)
ns.first.bow_hist = new_ns.bow_hist;
else
ns.first.bow_hist = ns.first.bow_hist * ns.second +
new_ns.bow_hist;
ns.first.bow_hist /= (float)ns.second + 1;
#endif
//Edges
//Increment
ns.second += 1;
}
void SSGProc::updateSSGInvariants(SSG& ssg, Mat& current_image, Parameters* params)
{
Mat gray_image;
cvtColor(current_image, gray_image, CV_BGR2GRAY);
Mat hue_channel = ImageProcess::generateChannelImage(current_image, 0, TSC_SAT_LOWER, TSC_SAT_UPPER, TSC_SAT_LOWER, TSC_VAL_UPPER);
Mat hue_channel_filtered;
cv::medianBlur(hue_channel, hue_channel_filtered, 3);
vector<bubblePoint> hue_bubble = bubbleProcess::convertGrayImage2Bub(hue_channel_filtered, FOCAL_LENGHT_PIXELS, 180);
vector<bubblePoint> hue_bubble_red = bubbleProcess::reduceBubble(hue_bubble);
Mat val_channel= ImageProcess::generateChannelImage(current_image, 2, TSC_SAT_LOWER, TSC_SAT_UPPER, TSC_SAT_LOWER, TSC_VAL_UPPER);
vector<bubblePoint> val_bubble = bubbleProcess::convertGrayImage2Bub(val_channel, FOCAL_LENGHT_PIXELS, 255);
vector<bubblePoint> val_bubble_red = bubbleProcess::reduceBubble(val_bubble);
bubbleStatistics stats_val = bubbleProcess::calculateBubbleStatistics(val_bubble_red, 255);
//qDebug() << stats_val.mean << stats_val.variance;
//If frame is not informative, return..
if(stats_val.mean <= params->tsc_params.tau_mu || stats_val.variance <= params->tsc_params.tau_sigma)
{
qDebug() << "Uninformative" << stats_val.mean << stats_val.variance;
return;
}
vector<Mat> filter_outputs = ImageProcess::applyFilters(gray_image);
Mat invariants_total, invariants_total_log;
DFCoefficients df_coeff = bubbleProcess::calculateDFCoefficients(hue_bubble_red, NR_HARMONICS, NR_HARMONICS);
Mat hue_invariants = bubbleProcess::calculateInvariantsMat(df_coeff, NR_HARMONICS, NR_HARMONICS);
invariants_total = hue_invariants.clone();
for(uint i = 0; i < filter_outputs.size(); i++)
{
vector<bubblePoint> img_bubble = bubbleProcess::convertGrayImage2Bub(filter_outputs[i], FOCAL_LENGHT_PIXELS, 255);
vector<bubblePoint> img_bubble_red = bubbleProcess::reduceBubble(img_bubble);
DFCoefficients df_coeff = bubbleProcess::calculateDFCoefficients(img_bubble_red, NR_HARMONICS, NR_HARMONICS);
Mat invariants = bubbleProcess::calculateInvariantsMat(df_coeff, NR_HARMONICS, NR_HARMONICS);
// if(i == 0)
// invariants_total = invariants.clone();
// else
// {
// hconcat(invariants_total, invariants, invariants_total);
// }
cv::hconcat(invariants_total, invariants, invariants_total);
}
//qDebug() << invariants_total.size().width << invariants_total.size().height;
cv::log(invariants_total,invariants_total_log);
invariants_total_log = invariants_total_log / 25;
cv::transpose(invariants_total_log,invariants_total_log);
for(int i = 0; i < invariants_total_log.rows; i++)
{
if(invariants_total_log.at<float>(i,0) < 0)
invariants_total_log.at<float>(i,0) = 0.5;
}
if(ssg.member_invariants.empty())
{
ssg.member_invariants = invariants_total_log;
}
else
{
hconcat(ssg.member_invariants, invariants_total_log, ssg.member_invariants);
}
cv::reduce(ssg.member_invariants, ssg.mean_invariant, 1, CV_REDUCE_AVG);
}
void SSGProc::updateSSGInvariantsFromDB(SSG& ssg, Mat& current_image, Parameters* params, FrameDesc& frame_desc)
{
Mat gray_image;
cvtColor(current_image, gray_image, CV_BGR2GRAY);
bubbleStatistics stats_val = frame_desc.bs;
//qDebug() << stats_val.mean << stats_val.variance;
//If frame is not informative, return..
if(stats_val.mean <= params->tsc_params.tau_mu || stats_val.variance <= params->tsc_params.tau_sigma)
{
//qDebug() << "Uninformative" << stats_val.mean << stats_val.variance;
return;
}
Mat invariants_total_log = frame_desc.bd;
if(ssg.member_invariants.empty())
{
ssg.member_invariants = invariants_total_log;
}
else
{
hconcat(ssg.member_invariants, invariants_total_log, ssg.member_invariants);
}
cv::reduce(ssg.member_invariants, ssg.mean_invariant, 1, CV_REDUCE_AVG);
}
//Updates SSG
void SSGProc::updateSSG(SSG& ssg, vector<NodeSig>& ns, Mat& map_col)
{
//If SSG contains less number of elements
//then initialize
for(int i = ssg.nodes.size(); i < map_col.size().height; i++)
{
NodeSig ns;
pair<NodeSig, int> new_node(ns, 0);
ssg.nodes.push_back(new_node);
}
//For each node that appears at the last frame(inspected using map),
//add weighted version of it to SSG
for(int i = 0; i < map_col.size().height; i++)
{
//If ith node appears at the last frame
//then, add weighted
if(map_col.at<int>(i, 0) > 0)
{
updateNodeSig(ssg.nodes[i], ns[map_col.at<int>(i, 0)]);
}
}
#ifdef PROCESS_EDGES
//Update edges - add edges in new ns to ssg
for(int i = 0; i < ns.size(); i++)
{
NodeSig ns_ = ns[i];
int source_node = -1;
//find global id of node
for(int k = 0; k < map.size().height; k++)
{
//If last column of map has local node id -> get global id of node
if(map.at<int>(k, map.size().width-1) == i)
{
source_node = k;
}
}
//if source node is placed in last column of map
if(source_node != -1)
{
for(int j = 0; j < ns_.edges.size(); j++)
{
int target_node = -1;
std::pair<int,float> edge = ns_.edges[j];
//find target node edge points
for(int k = 0; k < map.size().height; k++)
{
//If last column of map has local node id -> get global id of node
if(map.at<int>(k,map.size().width-1) == edge.first)
{
target_node = k;
}
}
if(target_node != -1)
{
//there is an edge from source_node -> target_node
std::pair<int,float> edge_;
edge_.first = target_node;
edge_.second = edge.second;
ssg.nodes[source_node].first.edges.push_back(edge_);
}
}
}
}
#endif
}
//Draws given SSG structure on image
Mat SSGProc::drawSSG(SSG& ssg, Mat &input)
{
Mat bg_img = Mat::zeros(input.size(), CV_8UC3);
bg_img = Scalar(255,255,255);
for(int i = 0; i < ssg.nodes.size(); i++)
{
#ifdef PROCESS_EDGES
//Draw edges
for(int j = 0; j < ssg.nodes[i].first.edges.size(); j++)
{
//If both two connecting nodes appear in SSG
//
int idx = ssg.nodes[i].first.edges[i].first;
if(ssg.nodes.size() > idx && ssg.nodes[idx].second > longestApp*tau_p)
{
Point p1 = ssg.nodes[i].first.center;
Point p2 = ssg.nodes[idx].first.center;
line(bg_img,p1,p2,Scalar(0,0,0),2);
}
}
#endif
float rad = sqrt(ssg.nodes[i].first.area/M_PI)/10.0;
circle(bg_img, ssg.nodes[i].first.center, rad,
Scalar(ssg.nodes[i].first.colorR, ssg.nodes[i].first.colorG, ssg.nodes[i].first.colorB),-1);
circle(bg_img, ssg.nodes[i].first.center, rad,
Scalar(0,0,0),1);
}
for(int i = 0; i < ssg.nodes.size(); i++)
{
float rad = sqrt(ssg.nodes[i].first.area/M_PI)/2.0;
circle(bg_img, ssg.nodes[i].first.center, rad,
Scalar(ssg.nodes[i].first.colorR, ssg.nodes[i].first.colorG, ssg.nodes[i].first.colorB),-1);
circle(bg_img, ssg.nodes[i].first.center, rad,
Scalar(0,0,0),1);
}
//Save SSG
string outName = getOutputFolder()+QString::number(ssg.getId()).toStdString()+".jpg";
imwrite(outName, bg_img);
return bg_img;
}
void SSGProc::filterSummarySegments(SSG& ssg, float tau_p)
{
//Find longest appear
int longestApp = 0;
for(int i = 0; i < ssg.nodes.size(); i++)
{
if(ssg.nodes[i].second > longestApp)
longestApp = ssg.nodes[i].second;
}
//Experimental
//Adaptive tau_p thresholding
// float ratio = (ssg.getEndFrame()-ssg.getStartFrame())/100;
// ratio = ratio > 1 ? 1 : ratio;
// tau_p = tau_p - 0.3*ratio;
// tau_p = tau_p < 0 ? 0 : tau_p;
for(int i = 0; i < ssg.nodes.size();)
{
if(ssg.nodes[i].second < longestApp*tau_p)
{
ssg.nodes.erase(ssg.nodes.begin()+i);
}
else
i++;
}
}