forked from carlren/gSLICr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.cpp
185 lines (149 loc) · 5.5 KB
/
demo.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
// Copyright 2014-2015 Isis Innovation Limited and the authors of gSLICr
#include <time.h>
#include <stdio.h>
#include "gSLICr_Lib/gSLICr.h"
#include "NVTimer.h"
#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;
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;
}
}
int main()
{
cout << "hell";
VideoCapture cap(0);
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 = 250;
my_settings.spixel_size = 200;
my_settings.coh_weight = 1.0f;
my_settings.no_iters = 5;
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
// 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);
pub_info obj[my_settings.no_segs]; // this object has to be published
//StopWatchInterface *my_timer; sdkCreateTimer(&my_timer);
int key; int save_count = 0;
while (cap.read(oldFrame))
{
//int cen = spixel_list[0].id;
float blue_sum[my_settings.no_segs*(2)]={0},green_sum[my_settings.no_segs*(2)]={0},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},sum_y[my_settings.no_segs]={0};
int matrix[250000]={0};
pub_info *obj=(pub_info*)malloc(2*my_settings.no_segs*sizeof(pub_info));
resize(oldFrame, frame, s);
load_image(frame, in_img);
int count[200]={0};
//sdkResetTimer(&my_timer); sdkStartTimer(&my_timer);
gSLICr_engine->Process_Frame(in_img);
//sdkStopTimer(&my_timer);
//cout<<"\rsegmentation in:["<<sdkGetTimerValue(&my_timer)<<"]ms"<<flush;
gSLICr_engine->Draw_Segmentation_Result(out_img);
cv::namedWindow("tt",0);
load_image(out_img, boundry_draw_frame);
cv::imshow("tt", boundry_draw_frame);
gSLICr_engine->Write_Seg_Res_To_PGM("abc",matrix);
Mat M = frame;
Mat M2;
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
}
}
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]]++;
}
//---------------------------------------------------
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]
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]);
}
}
cout<<obj[0].centre_x<<endl;
resize(M, M2, s1);
// M stores the averages
imshow("test", M2);
key = waitKey(1);
if (key == 27) break;
free(obj);
}
destroyAllWindows();
return 0;
}