-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest03.cpp
282 lines (164 loc) · 5.85 KB
/
test03.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
#include <iostream>
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "test02.cpp"
#include <opencv2/video/video.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <sys/time.h>
extern "C" {
#include "serial_comm.h" //a C header, so lets wrap it in extern "C"
}
struct hsvRange{
int hmax,hmin,smax,smin,vmax,vmin;
} colorRange;
int fps = 60;
//int delay = ((double)(1/(double)(fps)))*1000;
//Prototypes
void readCapParams();
void shutterCB(int pos, void* param);
void onMouse( int event, int x, int y, int, void* );
///Globals
cv::VideoCapture cap;
//Theese two are global so that the callback can modify them
long fcount = 0;
unsigned long t_us_start;
//
int main(int argc, char** argv){
//for timing
struct timeval t;
unsigned long t_us_now,t_us_done,t_diff,t2_us_start,t2_us_stop,t2_diff,t2_temp;
double fps_calc =0;
double fps_avg =0;
// HSV range
colorRange.hmin = 0;
colorRange.hmax = 5;
colorRange.smin = 0;
colorRange.smax = 5;
colorRange.vmin = 230;
colorRange.vmax = 255;
// For color/treshold sliders
//cv::Scalar g_tres_min(128,128,128);
//cv::Scalar g_tres_max(170,170,170);
//unsigned int colH = 128,colS = 128,colV
//TestClass tc;
//tc.doStuff();
initSerial();
cv::Mat frame;
std::cout << "Init test3" << std::endl;
cv::namedWindow("Color", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
cv::namedWindow("HSV", CV_WINDOW_AUTOSIZE); //create a window with the name "HSV"
//int initShutter = 728;
int initShutter = 0;
int shutterVal = initShutter;
// Shutter slider
cv::createTrackbar("tbShutter","Color",&shutterVal,4095,shutterCB,NULL);
cv::Mat colorFrame;
cv::Mat tresholdedFrame;
cv::Mat hsvFrame;
// HSV values are H from 0-180 while S and V are from 0-255
//cv::createTrackbar("tbH","MyWindow",&colH,180,HCB,&g_tres_min);
//cv::createTrackbar("tbS","MyWindow",&colS,255,SCB,NULL);
//cv::createTrackbar("tbV","MyWindow",&colV,255,VCB,NULL);
cv::setMouseCallback("Color",onMouse,&colorFrame);
cap.open(CV_CAP_DC1394); // Open first firewire camera. in 2.3 use CV_CAP, in 2.5 use CV::CAP
//Get al config of camera
//readCapParams();
std::cout << "Initial FPS: " << cap.get(CV_CAP_PROP_FPS) << std::endl;
// Try setting one
cap.set(CV_CAP_PROP_WHITE_BALANCE_BLUE_U,794); // 736
cap.set(CV_CAP_PROP_WHITE_BALANCE_RED_V,437);
cap.set(CV_CAP_PROP_EXPOSURE,initShutter); // "Shutter" in coriander
cap.set(CV_CAP_PROP_FPS,fps);
cap.set(CV_CAP_PROP_GAMMA,1);
cap.set(CV_CAP_PROP_GAIN,30);
std::cout << "Set FPS: " << cap.get(CV_CAP_PROP_FPS) << "And gamma: " << cap.get(CV_CAP_PROP_GAMMA) << std::endl;
//readCapParams();
//cv::displayStatusBar("MyWindow","Test text ..",0); //IAU's openCV is not compiled with extended qt/GUI support.
//initial time
gettimeofday(&t, NULL);
t_us_start = t.tv_sec*(1000000)+t.tv_usec;
t_us_done = t_us_start;
int cnt = -1;
int frameCnt = 0;
bool countingState=false;
while(1){
cap >> frame;
gettimeofday(&t, NULL);
t2_us_stop = t.tv_sec*(1000000)+t.tv_usec;
//Lets get time now
gettimeofday(&t, NULL);
t_us_now = t.tv_sec*(1000000)+t.tv_usec;
if(frameCnt == 120){
// Lets turn on the LED, set the countingstate, and get start time
countingState =true;
//Lets get time
gettimeofday(&t, NULL);
ledON();
t2_us_start = t.tv_sec*(1000000)+t.tv_usec;
}
t_diff = t_us_now-t_us_done;
fps_calc = 1.0/((double)(t_diff)/1000000.0); // calc fps
fps_avg = fcount/((double)(t_us_now-t_us_start)/1000000.0); // calc avg fps
fcount++;
gettimeofday(&t, NULL);
t_us_done = t.tv_sec*(1000000)+t.tv_usec;
// Get color image, decode bayer BGGR.
cv::cvtColor(frame,colorFrame,CV_BayerBG2RGB,0);
cv::cvtColor(colorFrame,hsvFrame,CV_RGB2HSV,0);
/* -------- TRESHOLDING ----------- */
cv::Scalar hsvMax(colorRange.hmax,colorRange.smax,colorRange.vmax);
cv::Scalar hsvMin(colorRange.hmin,colorRange.smin,colorRange.vmin);
// Apply treshold (HSV)
cv::inRange(hsvFrame,hsvMin,hsvMax,tresholdedFrame);
if(!colorFrame.data) break;
// For filtered HSV
//cv::imshow("HSV",tresholdedFrame); // Uncomment this line to see the actual picture. It will give an unsteady FPS
//cv::imshow("Color",colorFrame); // Uncomment this line to see the actual picture. It will give an unsteady FPS
//std::cout << "Exec time (us): "<< t_diff << " Calc FPS: " << fps_calc << ", FPS(avg): " << fps_avg << std::endl;
cnt = cv::countNonZero(tresholdedFrame);
//std::cout << "Count: " << cnt << std::endl;
if(cnt >= 130){
// LED on
t2_diff = (t2_us_stop-t2_us_start);
std::cout << "Lys fundet efter: (us) " << t2_diff << std::endl;
ledOFF();
}
if(cv::waitKey(1) >= 2){break;} // We wait 1ms - so that the frame can be drawn
frameCnt++; // LED frame count
}
cv::destroyWindow("Color"); //destroy the window with the name, "MyWindow"
cv::destroyWindow("HSV");
closeSerial();
return 0;
}
void readCapParams(){
double mode = 99;
for(int i = -4;i<=21;i++){
mode = cap.get(i);
std::cout << "Param nr " << i << " har: " << mode << std::endl;
mode = 99;
}
}
void shutterCB(int pos, void* param){
struct timeval t;
cap.set(CV_CAP_PROP_EXPOSURE,pos);
fcount=0; // Reset frame counter, so we dont have do wait for the avg to "catch" up
gettimeofday(&t, NULL);
t_us_start = t.tv_sec*(1000000)+t.tv_usec;
std::cout << "CALLBACK !!!: pos: " << pos << "Shutter read: " << cap.get(CV_CAP_PROP_EXPOSURE) << std::endl;
}
void onMouse( int event, int x, int y, int, void* userdata ){
cv::Mat hsvMat;
if( event != cv::EVENT_LBUTTONDOWN ) {return;}
unsigned int R=0,G=0,B=0,H=0,S=0,V=0;
cv::Mat* RGB = (cv::Mat*) userdata;
B = (*RGB).at<cv::Vec3b>(y,x)[0];
G = (*RGB).at<cv::Vec3b>(y,x)[1];
R = (*RGB).at<cv::Vec3b>(y,x)[2];
// Convert to HSV
cv::cvtColor((*RGB),hsvMat,CV_RGB2HSV);
H = hsvMat.at<cv::Vec3b>(y,x)[0];
S = hsvMat.at<cv::Vec3b>(y,x)[1];
V = hsvMat.at<cv::Vec3b>(y,x)[2];
std::cout << "(x,y):" << x << "," << y << " (R,G,B): " << R << "," << G << "," << B << " - (H,S,V) " << H << "," << S << "," << V << std::endl;
}