forked from carlren/gSLICr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgb-gen.cpp
498 lines (414 loc) · 17.5 KB
/
rgb-gen.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
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2014-2015 Isis Innovation Limited and the authors of gSLICr
/*
Delivers the rgb values of the 3x3 matrix around the superpixel along with the black(0)/white(1) label.
This data can be used for training machine learning algorithms.
Needs the image number and the optimum Hysteresis values. as input.
*/
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include "gSLICr_Lib/gSLICr.h"
#include "NVTimer.h"
#include <queue>
#include <cmath>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "./gSLICr_Lib/objects/gSLICr_spixel_info.h"
#include "./gSLICr_Lib/engines/gSLICr_seg_engine_shared.h"
using namespace std;
using namespace cv;
typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
// ===============================================================
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
// ===============================================================
static hsv rgb2hsv(rgb in);
static rgb hsv2rgb(hsv in);
// ===============================================================
hsv rgb2hsv(rgb in)
{
hsv out;
double min, max, delta;
min = in.r < in.g ? in.r : in.g;
min = min < in.b ? min : in.b;
max = in.r > in.g ? in.r : in.g;
max = max > in.b ? max : in.b;
out.v = max; // v
delta = max - min;
if (delta < 0.00001)
{
out.s = 0;
out.h = 0; // undefined, maybe nan?
return out;
}
if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
out.s = (delta / max); // s
} else {
// if max is 0, then r = g = b = 0
// s = 0, h is undefined
out.s = 0.0;
out.h = NAN; // its now undefined
return out;
}
if( in.r >= max ) // > is bogus, just keeps compilor happy
out.h = ( in.g - in.b ) / delta; // between yellow & magenta
else
if( in.g >= max )
out.h = 2.0 + ( in.b - in.r ) / delta; // between cyan & yellow
else
out.h = 4.0 + ( in.r - in.g ) / delta; // between magenta & cyan
out.h *= 60.0; // degrees
if( out.h < 0.0 )
out.h += 360.0;
return out;
}
// ===============================================================
struct pub_info
{
float centre_x;
float centre_y;
float avg_colors[3];
float size;
};
// ===============================================================
void load_image(const Mat& inimg, gSLICr::UChar4Image* outimg)
{
gSLICr::Vector4u* outimg_ptr = outimg->GetData(MEMORYDEVICE_CPU);
for (int y = 0; y < outimg->noDims.y;y++)
for (int x = 0; x < outimg->noDims.x; x++)
{
int idx = x + y * outimg->noDims.x;
outimg_ptr[idx].b = inimg.at<Vec3b>(y, x)[0];
outimg_ptr[idx].g = inimg.at<Vec3b>(y, x)[1];
outimg_ptr[idx].r = inimg.at<Vec3b>(y, x)[2];
}
}
// ===============================================================
void load_image(const gSLICr::UChar4Image* inimg, Mat& outimg)
{
const gSLICr::Vector4u* inimg_ptr = inimg->GetData(MEMORYDEVICE_CPU);
for (int y = 0; y < inimg->noDims.y; y++)
for (int x = 0; x < inimg->noDims.x; x++)
{
int idx = x + y * inimg->noDims.x;
outimg.at<Vec3b>(y, x)[0] = inimg_ptr[idx].b;
outimg.at<Vec3b>(y, x)[1] = inimg_ptr[idx].g;
outimg.at<Vec3b>(y, x)[2] = inimg_ptr[idx].r;
}
}
// ===============================================================
// Declaring some constants of the sliders
const int Slider_max = 255;
const int Hue_Slider_max = 360;
int Lvalue_slider, Hvalue_slider,Value_slider;
double Lvalue,Hvalue,Value;
// ==================Trcakbar functions======================
void on_trackbar( int, void* )
{
Lvalue = (double) Lvalue_slider/Slider_max ;
}
void on_trackbar2( int, void* )
{
Hvalue = (double) Hvalue_slider/Slider_max ;
}
void on_trackbar3( int, void* )
{
Value = (double) Value_slider/Hue_Slider_max ;
}
// ==================utility functions=========================
std::string ToString(int val)
{
stringstream ss;
ss<<val;
return ss.str();
}
// ===============================================================
// =============== utility function to print all features for training data
void print(float rs[],float bs[], float gs[] ,int c[],int x,int y,int n)
{
if(c[n*x+y]!=0){
cout<<(int)(rs[n*x+y]/c[n*x+y])<<" ";
cout<<(int)(bs[n*x+y]/c[n*x+y])<<" ";
cout<<(int)(gs[n*x+y]/c[n*x+y])<<" ";
}
else {
cout<<"0 0 255 ";
}
}
void masker(int mask[], float red_sum[], float blue_sum[], float green_sum[],int n){
int k=0;
for(int i=0;i<729;i++)
{
if(i/n==0 || i%n==0 || i/n==n-1 || i%n==n-1){
red_sum[i] =0;
green_sum[i] =0;
blue_sum[i] =0;
}
else {
red_sum[i] =mask[k]*255;
green_sum[i] =mask[k]*255;
blue_sum[i] =mask[k]*255;
k++;
}
}
}
int main()
{
/*VideoCapture cap("../sam.webm");
if (!cap.isOpened())
{
cerr << "unable to open camera!\n";
return -1;
}*/
// ====================================================== gSLICr settings ===================================================
gSLICr::objects::settings my_settings;
my_settings.img_size.x = 500;
my_settings.img_size.y = 500;
my_settings.no_segs = 750;
my_settings.spixel_size = 100;
my_settings.coh_weight = 1.0f;
my_settings.no_iters = 50;
my_settings.color_space = gSLICr::CIELAB; // gSLICr::CIELAB for Lab, or gSLICr::RGB for RGB
my_settings.seg_method = gSLICr::GIVEN_NUM; // or gSLICr::GIVEN_NUM for given number
my_settings.do_enforce_connectivity = true; // whether or not run the enforce connectivity step
int n = int(sqrt(my_settings.no_segs));
// ===========================================================================================================================
// ============================================= instantiate a core_engine ===================================================
gSLICr::engines::core_engine* gSLICr_engine = new gSLICr::engines::core_engine(my_settings);
gSLICr::UChar4Image* in_img = new gSLICr::UChar4Image(my_settings.img_size, true, true);
gSLICr::UChar4Image* out_img = new gSLICr::UChar4Image(my_settings.img_size, true, true);
Size s(my_settings.img_size.x, my_settings.img_size.y);
Size s1(640, 480);
Mat oldFrame, frame;
Mat boundry_draw_frame; boundry_draw_frame.create(s, CV_8UC3);
// ================================================== Input the file number h and the respective thresholds LValue , Hvalue and Value
int key, h=0;
cin>>h;
//cin>>Lvalue>>Hvalue>>Value;
Lvalue=2;
Hvalue=2;
Value=2;
// =================Reading the frame according to the name===========================================================================
std::string first ("../dr/");
std::string sec (".png");
std::string mid = ToString(h);
std::string name;
name=first+mid+sec;
oldFrame = cv::imread(name);
// =================Declaration pf few arrays to be used further=========================================================================
float blue_sum[my_settings.no_segs*(2)] = {0};
float green_sum[my_settings.no_segs*(2)] = {0};
float red_sum[my_settings.no_segs*(2)] = {0};
int blue[my_settings.img_size.x][my_settings.img_size.y] = {0};
int green[my_settings.img_size.x][my_settings.img_size.y] = {0};
int red[my_settings.img_size.x][my_settings.img_size.y] = {0};
int sum_x[my_settings.no_segs] = {0};
int sum_y[my_settings.no_segs] = {0};
int matrix[250000] = {0};
int count[750]={0};
pub_info *obj=(pub_info*)malloc(2*my_settings.no_segs*sizeof(pub_info));
int prev_lable[my_settings.no_segs]={0};
int lable;
// =====================================================================================================================================
resize(oldFrame, frame, s);
load_image(frame, in_img);
gSLICr_engine->Process_Frame(in_img);
gSLICr_engine->Draw_Segmentation_Result(out_img);
load_image(out_img, boundry_draw_frame);
gSLICr_engine->Write_Seg_Res_To_PGM("abc",matrix);
Mat M = frame;
Mat M2,Mimg;
// =====================================================================================================================================
// ============================================== Retrieving colour channels from the image ========================================
for(int i=0;i<my_settings.img_size.x;i++)
{
for(int j=0;j<my_settings.img_size.y;j++)
{
blue[i][j] = M.at<cv::Vec3b>(i,j)[0]; // b
green[i][j] = M.at<cv::Vec3b>(i,j)[1]; // g
red[i][j] = M.at<cv::Vec3b>(i,j)[2]; // r
}
}
// =====================================================================================================================================
// ================================================== Summing over the pixel Lvalues of all segments ===================================
for(int i=0;i<my_settings.img_size.x*my_settings.img_size.y;i++)
{
blue_sum[matrix[i]]+= blue[i/my_settings.img_size.x][i%my_settings.img_size.x];
red_sum[matrix[i]]+= red[i/my_settings.img_size.x][i%my_settings.img_size.x];
green_sum[matrix[i]]+= green[i/my_settings.img_size.x][i%my_settings.img_size.x];
sum_x[matrix[i]]+= ( i/my_settings.img_size.x);
sum_y[matrix[i]]+= (i%my_settings.img_size.x);
count[matrix[i]]++;
}
// ======================================== Placing in a queue for hysterisis threhsolding =========================================================
queue <int> clrbox;
for(int i=0;i<my_settings.no_segs;i++)
{
rgb rgb_obj;
rgb_obj.r = (red_sum[i]/count[i]) ;// r
rgb_obj.g = (green_sum[i]/count[i]) ;// r
rgb_obj.b = (blue_sum[i]/count[i]) ;// r
hsv hsv_obj= rgb2hsv(rgb_obj);
if(hsv_obj.h>Value){
if((hsv_obj.v>Lvalue && hsv_obj.v<Hvalue)){
prev_lable[i]=1;
}
else if( hsv_obj.v>Hvalue)
{
clrbox.push(i);
prev_lable[i]=2;
}
else
prev_lable[i]=0;
}
else prev_lable[i]=0;
}
// ==========================================================================================================================================
n--; // ================================================== because matrix is of size (n-1) X (n-1)
// ================================= Processing the queue for Hysterisis =============================================================
while(!clrbox.empty())
{
int var = clrbox.front();
clrbox.pop();
prev_lable[var]=3;
int x = (int)(var/n);
int y = (int)(var%n);
if(n*(x-1) + y-1>=0|| y!=0 ){
if(prev_lable[n*(x-1) + y-1]==1){
prev_lable[n*(x-1) + y-1]=2;
clrbox.push(n*(x-1) + y-1);
}
}
if(n*(x-1) + y>=0)
{
if(prev_lable[n*(x-1) + y]==1){
prev_lable[n*(x-1) + y]=2;
clrbox.push(n*(x-1) + y);
}
}
if(n*(x-1) + y + 1>=0 && y!=n-1){
if(prev_lable[n*(x-1) + y + 1]==1){
prev_lable[n*(x-1) + y + 1]=2;
clrbox.push(n*(x-1) + y + 1);
}}
if(var-1>=0 && y!=0){
if(prev_lable[var-1]==1){
prev_lable[var-1]=2;
clrbox.push(var-1);
}}
if(y!=n-1){
if(prev_lable[var+1]==1){
prev_lable[var+1]=2;
clrbox.push(var+1);
}}
if(n*(x+1) + y -1<n && y!=0){
if(prev_lable[n*(x+1) + y -1]==1){
prev_lable[n*(x+1) + y -1]=2;
clrbox.push(n*(x+1) + y -1);
} }
if(n*(x+1) + y<n){
if(prev_lable[n*(x+1) + y]==1){
prev_lable[n*(x+1) + y]=2;
clrbox.push(n*(x+1) + y);
}}
if(n*(x+1) + y+1<n && y!=n-1){
if(prev_lable[n*(x+1) + y + 1]==1){
prev_lable[n*(x+1) + y + 1]=2;
clrbox.push(n*(x+1) + y + 1);
}
}
}
// =====================================================================================================================================
// ================================================== printing the features with neighbouring cells and excluding the boundary cells
cout<<n<<endl;
//for(int i=0;i<729;i++)
//{ 676 superpixels
cout<<count[27*25+25]<<"\n";
cout<<blue_sum[27*22+11]<<endl;
int x,y;
for(x=1;x<=24;x++)
for(y=1;y<=24;y++){
//int x = (int)(i/n);
//int y = (int)(i%n);
//if(x==0 || x==n-1 || y==0 || y==n-1)continue;
print(red_sum, green_sum, blue_sum, count, x-1,y-1,n);
print(red_sum, green_sum, blue_sum, count, x-1,y,n);
print(red_sum, green_sum, blue_sum, count, x-1,y+1,n);
print(red_sum, green_sum, blue_sum, count, x,y-1,n);
print(red_sum, green_sum, blue_sum, count, x,y,n);
print(red_sum, green_sum, blue_sum, count, x,y+1,n);
//cout<<(int)(red_sum[i-1]/count[i-1])<<" "<<(int)(green_sum[i-1]/count[i-1])<<" "<<(int)(blue_sum[i-1]/count[i-1]);
//cout<<(int)(red_sum[i]/count[i])<<" "<<(int)(green_sum[i]/count[i])<<" "<<(int)(blue_sum[i]/count[i]);
//cout<<(int)(red_sum[i+1]/count[i+1])<<" "<<(int)(green_sum[i+1]/count[i+1])<<" "<<(int)(blue_sum[i+1]/count[i+1]);
print(red_sum, green_sum, blue_sum, count, x+1,y-1,n);
print(red_sum, green_sum, blue_sum, count, x+1,y,n);
print(red_sum, green_sum, blue_sum, count, x+1,y+1,n);
if(prev_lable[x*27+y]==3)
cout<<"1";
else
cout<<"0";
cout<<endl;
}
//}
// =====================================================Fuction for masking prediction values===============================================
int mask[]={};//Insert mask when needed
int size_mask=sizeof(mask)/sizeof(*mask);
if(size_mask!=0)
masker(mask,red_sum,blue_sum,green_sum,n);
// ================================================== Re-inserting the average Lvalues back into the image =============================
//else
{
for(int i=0;i<my_settings.img_size.y;i++)
{
for(int j=0;j<my_settings.img_size.x;j++)
{
M.at<cv::Vec3b>(i,j)[0] = blue_sum[matrix[i*my_settings.img_size.x + j ]]/count[matrix[i*my_settings.img_size.x + j]];// b
M.at<cv::Vec3b>(i,j)[1] = green_sum[matrix[i*my_settings.img_size.x + j ]]/count[matrix[i*my_settings.img_size.x + j]];// g
M.at<cv::Vec3b>(i,j)[2] = red_sum[matrix[i*my_settings.img_size.x + j ]]/count[matrix[i*my_settings.img_size.x + j]];// r
}
}
}
// ======================================================================================================================================
// ================================================== now compute all properties of pub_info obj[no_segs] if needed
/*
for(int i=0;i<=my_settings.no_segs;i++)
{
if(count[i]!=0)
{
obj[i].size=count[i];
obj[i].avg_colors[0] = (int)(blue_sum[i]/count[i]);
obj[i].avg_colors[1] = (int)(green_sum[i]/count[i]);
obj[i].avg_colors[2] = (int)(red_sum[i]/count[i]);
obj[i].centre_x = (int)(sum_x[i]/count[i]);
obj[i].centre_y = (int)(sum_y[i]/count[i]);
}
}
*/
//==================================Finally displaying and/or showing the image after resizing===========================================================
Canny( M, M2, 100, 180, 3);
Mat M3;
resize(M, M3, s1);
//cv::imshow("FinalImg",M2);
cv::namedWindow("Binary",5);
cv::imshow("Binary",M3);
std::string last (".bmp");
std::string newest;
newest = mid + last;
cv::imwrite(newest,M3);
free(obj);
//key = (char)waitKey(1);
//if (key == 27) break;
destroyAllWindows();
return 0;
}