-
Notifications
You must be signed in to change notification settings - Fork 1
/
driverFuncCLSuvam.cpp
486 lines (363 loc) · 13.4 KB
/
driverFuncCLSuvam.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <stdio.h>
#include "graph.h"
#include <opencv2/opencv.hpp>
#include <math.h>
#include <iostream>
#include <algorithm>
// #include <opencv2/core/core.hpp>
// #include <opencv2/highgui/highgui.hpp>
#define GAMMA 1
// #define GAMMAT 23
#define BINS 257
#define BINSIZE 1
// #define FACTOR 2.5
// #define FACTORINIT 2
#define FACTORINIT 20
#define M tan(45*3.14159265/180.0)
#define VEHICLE_FACTOR 1
// typedef Graph<int,int,int> GraphType;
typedef Graph<float,float,float> GraphType;
using namespace cv;
using namespace std;
double stddev = 0;
// Vec3i globalMean = Vec3i(0,0,0);
inline int twoD_to_oneD(const int &y , const int &x , const int &width )
{
return x + y*width;
}
inline Point oneD_to_twoD(const int &p , const int &width )
{
return Point(p%width , p/width);
}
float getWeight(const Vec3b &Ib1 , const Vec3b &Ib2 )
{
static const float inv_sqrt_2pi = 0.3989422804014327;
Vec3f I1 = Vec3f(Ib1);
Vec3f I2 = Vec3f(Ib2);
float t1,t2;
t1 = I1[2];
t2 = I2[2];
I1[2] = 0;
I2[2] = 0;
I1 /= sqrt((I1).dot(I1));
I2 /= sqrt((I2).dot(I2));
// I1[2] = 0;
// I2[2] = 0;
// float a = sqrt((I1-I2).dot(I1-I2))/GAMMA;
// float weight = inv_sqrt_2pi * exp(-0.5*a*a )/GAMMA ;
// weight = (0.00001 + weight)/1.00001;
// // cout<<weight<<endl;
// return weight;
// // return 7e5*weight/(1+abs(t1-t2));
float a = sqrt((I1-I2).dot(I1-I2));
// float weight = inv_sqrt_2pi * exp(-0.5*a*a )/GAMMA ;
float weight = std::max(1e-5, 1.0 - a*M);
// weight = (1e-5 + weight)/(1+1e-5);
// cout<<weight<<endl;
return 10*weight;
}
float getWeightCL(Vec3f meanCL[] , Vec3f varCL[] , Vec3b &Ib)
{
static const float inv_sqrt_2pi = 0.3989422804014327;
Vec3f pixel = Vec3f(Ib);
// int normVal = int( sqrt(pixel[0]*pixel[0] + pixel[1]*pixel[1] + pixel[2]*pixel[2]) );
int index = ((int)Ib[2])/BINSIZE;
float x;
// cout<<"meanCL : : "<<meanCL[index]<<endl;
// float weight = exp(-1*norm(pixel-meanCL[index])/GAMMA);
if ( meanCL[index][0] < 0 )
{
return 1e-15;
}
else
{
Vec3f diff = (pixel - meanCL[index]);
diff[0] /= sqrt(varCL[index][0]);
diff[1] /= sqrt(varCL[index][1]);
diff[2] = 0;
x = diff.dot(diff);
}
// cout<<x<<endl;
float weight = inv_sqrt_2pi * exp(-0.5*x) / sqrt( varCL[index][0]*varCL[index][1] );
// cout<<"norm : : "<<norm(pixel - meanCL[index])<<endl;
// cout<<"weight : : "<<weight<<endl;
weight = (0.00001 + weight)/1.00001;
// cout<<weight<<endl;
return weight;
}
int main(int argc, char** argv)
{
if( argc != 3)
{
cout <<" Usage: driverFunc image map" << endl;
return -1;
}
Mat image , gray_image;
// VideoCapture cap(argv[1]); // open the default camera
// if(!cap.isOpened()) // check if we succeeded
// return -1;
gray_image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
cvtColor(gray_image,gray_image,CV_BGR2HSV);
// gray_image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); // Read the file
Mat roimap = imread(argv[2], CV_LOAD_IMAGE_COLOR);
// Mat roimap_hsv;
// cvtColor(roimap,roimap_hsv,CV_BGR2HSV);
// cout<<roimap_hsv.cols<<endl;
cv::resize(gray_image,gray_image,roimap.size());
if(! gray_image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
const int width = gray_image.cols;
const int height = gray_image.rows;
cout<<gray_image.cols<<endl;
cout<<gray_image.rows<<endl;
int NumNodes = width*height;
//deepL
Vec3f meanCL[BINS] , meanCLBkgrnd[BINS] , varCL[BINS] , varCLBkgrnd[BINS];
vector<Vec2i> roadPixels[BINS] , bkgrndPixels[BINS];
// cout<<roimap_hsv.rows<<endl;
// cout<<roimap.at<Vec3b>(194,97)<<endl;
Vec3b road = Vec3b(128,64,128);
Vec3b vehicle = Vec3b(128,0,64);
int total_road = 0;
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
Vec3i pixel = (Vec3i)gray_image.at<Vec3b>(j,i);
// cout<<i<<" "<<j<< " "<<pixel<<endl;
if ( roimap.at<Vec3b>(j,i) == road )
{
roadPixels[ pixel[2]/BINSIZE ].push_back( Vec2i(i,j) );
total_road ++;
}
else
bkgrndPixels[ pixel[2]/BINSIZE ].push_back( Vec2i(i,j) );
}
}
// cout<<"Hello"<<endl;
float FACTOR = FACTORINIT;
float roadPercent = (float)total_road/(width*height);
// if(roadPercent < 0.15)
// FACTOR = 1+2*roadPercent;
cout<<FACTOR<<endl;
for (int i = 0; i < BINS; ++i)
{
if(roadPixels[i].size() > 0)
{
Vec3f meanVal = Vec3f(0,0,0) ;
for (int j = 0; j < roadPixels[i].size(); ++j)
{
meanVal += (Vec3f)gray_image.at<Vec3b>( roadPixels[i][j][1] , roadPixels[i][j][0] );
}
meanCL[i][0] = meanVal[0] / roadPixels[i].size();
meanCL[i][1] = meanVal[1] / roadPixels[i].size();
meanCL[i][2] = meanVal[2] / roadPixels[i].size();
}
else
meanCL[i] = Vec3f(-1,-1,-1);
if(bkgrndPixels[i].size() > 0)
{
Vec3f meanVal = Vec3f(0,0,0) ;
for (int j = 0; j < bkgrndPixels[i].size(); ++j)
{
meanVal += (Vec3f)gray_image.at<Vec3b>( bkgrndPixels[i][j][1] , bkgrndPixels[i][j][0] );
}
meanCLBkgrnd[i][0] = meanVal[0] / bkgrndPixels[i].size();
meanCLBkgrnd[i][1] = meanVal[1] / bkgrndPixels[i].size();
meanCLBkgrnd[i][2] = meanVal[2] / bkgrndPixels[i].size();
}
else
meanCLBkgrnd[i] = Vec3f(-1,-1,-1);
// cout<<meanCL[i]<<endl;
}
for (int i = 0; i < BINS; ++i)
{
if(roadPixels[i].size() > 0)
{
Vec3f variance = Vec3f(1,1,1) ;
for (int j = 0; j < roadPixels[i].size(); ++j)
{
Vec3f diff = ( meanCL[i] - (Vec3f)gray_image.at<Vec3b>( roadPixels[i][j][1] , roadPixels[i][j][0] ) );
variance += Vec3f(diff[0]*diff[0], diff[1]*diff[1] , diff[2]*diff[2]);
}
varCL[i][0] = (variance[0] / roadPixels[i].size())+1;
varCL[i][1] = (variance[1] / roadPixels[i].size())+1;
varCL[i][2] = (variance[2] / roadPixels[i].size())+1;
}
else
varCL[i] = Vec3f(-1,-1,-1);
if(bkgrndPixels[i].size() > 0)
{
Vec3f variance = Vec3f(1,1,1) ;
for (int j = 0; j < bkgrndPixels[i].size(); ++j)
{
Vec3f diff = ( meanCLBkgrnd[i] - (Vec3f)gray_image.at<Vec3b>( bkgrndPixels[i][j][1] , bkgrndPixels[i][j][0] ) );
variance += Vec3f(diff[0]*diff[0] , diff[1]*diff[1] , diff[2]*diff[2]);
}
varCLBkgrnd[i][0] = (variance[0] / bkgrndPixels[i].size())+1;
varCLBkgrnd[i][1] = (variance[1] / bkgrndPixels[i].size())+1;
varCLBkgrnd[i][2] = (variance[2] / bkgrndPixels[i].size())+1;
}
else
varCLBkgrnd[i] = Vec3f(-1,-1,-1);
cout<<varCL[i]<<endl;
}
Mat canvas = Mat::zeros(gray_image.rows, gray_image.cols*2+10, gray_image.type());
// VideoWriter outputVideo("../segment1.avi", CV_FOURCC('X','V','I','D'), cap.get(CV_CAP_PROP_FPS), S, true);
// VideoWriter outputVideoCombined("../segment1Combined.avi", CV_FOURCC('X','V','I','D'), cap.get(CV_CAP_PROP_FPS), Size(2*gray_image.cols + 10 , gray_image.rows), true);
// // outputVideo.open("../segment.mp4", CV_FOURCC('X','2','6','4'), cap.get(CV_CAP_PROP_FPS), S, true);
// while(1)
{
// cap >> gray_image;
gray_image.copyTo(canvas(Range::all(), Range(0, gray_image.cols)));
GraphType *g = new GraphType(/*estimated # of nodes*/ NumNodes, /*estimated # of edges*/ 4*NumNodes);
for (int i = 0; i < NumNodes; ++i)
{
g->add_node();
}
for (int i = 1; i < width; ++i)
{
int n1, n2;
float weight;
n1 = twoD_to_oneD(0,i,width);
n2 = twoD_to_oneD(0,i-1,width);
weight = getWeight( gray_image.at<Vec3b>(0,i) , gray_image.at<Vec3b>(0,i-1));
g->add_edge( n1 , n2 , weight , weight );
// weight = getWeightTerminals(gray_image.at<Vec3b>(0,i),mean);
// weight = getWeightCL( meanCL , varCL , gray_image.at<Vec3b>(0,i) );
float w1 = getWeightCL( meanCL , varCL , gray_image.at<Vec3b>(0,i) );
float w2 = getWeightCL( meanCLBkgrnd , varCLBkgrnd , gray_image.at<Vec3b>(0,i) );
if (roimap.at<Vec3b>(0,i) == road)
{
w1 = 1;
w2 = 0;
}
else
{
w1 /= FACTORINIT;
}
// if (roimap.at<Vec3b>(0,i) == vehicle)
// {
// w1 /= VEHICLE_FACTOR;
// w2 *= VEHICLE_FACTOR;
// }
float normaLize = w1+w2;
g->add_tweights(n1 , w1/normaLize , w2/normaLize);
}
for (int j = 1; j < height; ++j)
{
int n1, n2 ;
float weight;
n1 = twoD_to_oneD(j,0,width);
n2 = twoD_to_oneD(j-1,0,width);
// cout<<j<<endl;
weight = getWeight( gray_image.at<Vec3b>(j,0) , gray_image.at<Vec3b>(j-1,0));
g->add_edge( n1 , n2 , weight , weight );
// weight = getWeightTerminals(gray_image.at<Vec3b>(j,0),mean);
float w1 = getWeightCL( meanCL , varCL , gray_image.at<Vec3b>(j,0));
float w2 = getWeightCL( meanCLBkgrnd , varCLBkgrnd , gray_image.at<Vec3b>(j,0));
if (roimap.at<Vec3b>(j,0) == road)
{
w1 = 1;
w2 = 0;
}
else
{
w1 /= FACTORINIT;
}
// if (roimap.at<Vec3b>(j,0) == vehicle)
// {
// w1 /= VEHICLE_FACTOR;
// w2 *= VEHICLE_FACTOR;
// }
float normaLize = w1+w2;
g->add_tweights(n1 , w1/normaLize , w2/normaLize);
}
// cout<<" Error "<<endl;
for (int i = 1; i < width; ++i)
{
for (int j = 1; j < height; ++j)
{
// cout << (int)gray_image.at<uchar>(j,i)<< " ";
int n1,n2 ;
float weight;
n1 = twoD_to_oneD(j,i,width);
// cout<<j<< " " << i<< " "<<n1<<endl;
n2 = twoD_to_oneD(j-1,i,width);
weight = getWeight( gray_image.at<Vec3b>(j,i) , gray_image.at<Vec3b>(j-1,i));
g->add_edge( n1 , n2 , weight , weight );
n2 = twoD_to_oneD(j,i-1,width);
weight = getWeight( gray_image.at<Vec3b>(j,i) , gray_image.at<Vec3b>(j,i-1));
g->add_edge( n1 , n2 , weight , weight );
// weight = getWeightTerminals(gray_image.at<Vec3b>(j,i) , mean);
float w1 = getWeightCL( meanCL , varCL , gray_image.at<Vec3b>(j,i) );
float w2 = getWeightCL( meanCLBkgrnd , varCLBkgrnd , gray_image.at<Vec3b>(j,i) );
if (roimap.at<Vec3b>(j,i) == road)
{
w1 = 1;
w2 = 0;
// cout<<"Non Road"<<w1<<" "<<w2<<endl;
}
else
{
w1 /= FACTORINIT;
}
// else
// cout<<"Non Road"<<w1<<" "<<w2<<endl;
// if (roimap.at<Vec3b>(j,i) == vehicle)
// {
// w1 /= VEHICLE_FACTOR;
// w2 *= VEHICLE_FACTOR;
// // cout<<"asfsaf\n";
// }
// if (i==312 && j==289)
// if (i==224 && j==222)
// {
// cout<<i<<" "<<j<<" "<<weight<<endl;
// }
float normaLize = w1+w2;
// cout<<weight<<endl;
g->add_tweights(n1 , w1/normaLize , w2/normaLize);
}
}
float flow = g -> maxflow();
printf("Flow = %f\n", flow);
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
int n1;
n1 = twoD_to_oneD(j,i,width);
if (g->what_segment(n1) == GraphType::SOURCE)
{
gray_image.at<Vec3b>(j,i) = Vec3b(255,105,180);
}
else
{
gray_image.at<Vec3b>(j,i) = Vec3b(0,0,0);
}// cout<<"SINK"<<endl;
}
}
imwrite( "data/segmentation_wi.png", gray_image );
gray_image.copyTo(canvas(Range::all(), Range(gray_image.cols+10, gray_image.cols*2+10)));
// outputVideoCombined << canvas;
// imshow("seg", gray_image);
// imshow("seg", canvas);
// waitKey(20);
// if(waitKey(30) >= 0) break;
delete g;
}
// printf("Minimum cut:\n");
// if (g->what_segment(0) == GraphType::SOURCE)
// printf("node0 is in the SOURCE set\n");
// else
// printf("node0 is in the SINK set\n");
// if (g->what_segment(1) == GraphType::SOURCE)
// printf("node1 is in the SOURCE set\n");
// else
// printf("node1 is in the SINK set\n");
return 0;
}