-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
229 lines (176 loc) · 5.75 KB
/
main.cpp
File metadata and controls
229 lines (176 loc) · 5.75 KB
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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include "GPIOlib.h"
#define PI 3.1415926
//Uncomment this line at run-time to skip GUI rendering
//#define _DEBUG
using namespace cv;
using namespace std;
using namespace GPIO;
const string CAM_PATH = "/dev/video0";
const string MAIN_WINDOW_NAME = "Processed Image";
const string CANNY_WINDOW_NAME = "Canny";
const int CANNY_LOWER_BOUND = 50;
const int CANNY_UPPER_BOUND = 150;
const int HOUGH_THRESHOLD = 40;
int lineCount = 0;
int isVisible = 0;
int watch = 41;
int shouldIdle = 0;
int currentStage = 1;
int speed = 5;
int turnWatch = 0;
int turnWatchFlag = 0;
int main() {
GPIO::init();
turnTo(0);
VideoCapture capture(CAM_PATH);
//If this fails, try to open as a video camera, through the use of an integer param
if (!capture.isOpened()) {
capture.open(atoi(CAM_PATH.c_str()));
}
double dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //the width of frames of the video
double dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //the height of frames of the video
clog << "Frame Size: " << dWidth << "x" << dHeight << endl;
Mat image;
int i = 1;
//投票决定应该turn哪边
int should_turn_left = 0;
while (true) {
if (turnWatchFlag == 1) {
turnWatch++;
if (turnWatch == 4) {
turnWatch = 0;
turnWatchFlag = 0;
clog << "Recover TurnTo" << endl;
if (shouldIdle == 0) {
turnTo(0);
}
}
}
if (lineCount == 4 && currentStage == 1) {
clog << "Stage 1 Finished" << endl;
shouldIdle++;
currentStage++;
lineCount = 0;
} else if (lineCount == 9 && currentStage == 2) {
clog << "Stage 2 Finished" << endl;
shouldIdle++;
currentStage++;
lineCount = 0;
} else if (lineCount == 5 && currentStage == 3) {
clog << "Stage 3 Finished" << endl;
shouldIdle++;
currentStage++;
}
watch++;
controlLeft(1, speed);
controlRight(1, speed);
if (shouldIdle == 0) {
if (should_turn_left > 0) {
clog << "turn left" << endl;
turnWatchFlag = 1;
turnWatch = 0;
turnTo(-35);
}
if (should_turn_left < 0) {
clog << "turn right" << endl;
turnWatchFlag = 1;
turnWatch = 0;
turnTo(10);
}
i = 0;
} else if (shouldIdle > 0) {
shouldIdle++;
int interval1 = 35;
if (currentStage == 2) {
interval1 += 20;
}
// delay to turn left first corner
if (shouldIdle == interval1) {
if (currentStage == 4) {
stopLeft();
stopRight();
return 0;
} else {
turnTo(-32);
}
}
int interval2 = interval1 + 60;
// how long to turn left
if (shouldIdle == interval2) {
turnTo(0);
clog << "Recover!!!!!!!!!!!" << endl;
}
// delay to turn left second corner
int interval3 = interval2;
if (shouldIdle == interval3) {
shouldIdle = 0;
lineCount = 0;
i = 0;
}
}
should_turn_left = 0;
i++;
capture >> image;
if (image.empty())
break;
Rect roi(0, image.rows / 3, image.cols, image.rows / 2);
Mat imgROI = image(roi);
//Canny algorithm
Mat contours;
//blur(imgROI,imgROI,Size(30,30));
Canny(imgROI, contours, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND);
#ifdef _DEBUG
imshow(CANNY_WINDOW_NAME, contours);
#endif
vector<Vec4i> lines;
HoughLinesP(contours, lines, 1, PI / 180, HOUGH_THRESHOLD, 10, 5);
Mat result(imgROI.size(), CV_8U, Scalar(255));
imgROI.copyTo(result);
clog << lines.size() << endl;
//Draw the lines and judge the slope
int color = 50;
int flag = 0;
for (vector<Vec4i>::const_iterator it = lines.begin(); it != lines.end(); ++it) {
line(result, Point((*it)[0], (*it)[1]), Point((*it)[2], (*it)[3]), Scalar(color, color, 255), 20, CV_AA);
float slope = ((float) (*it)[3] - (*it)[1]) / ((*it)[2] - (*it)[0]);
//clog << "slope" << slope << endl;
if (slope >= -0.4 && slope <= 0.4) {
if (isVisible == 0) {
isVisible = 1;
}
flag = 1;
}
if (slope <= 2 && slope >= 0.4) {
should_turn_left++;
}
if (slope >= -2 && slope <= -0.4) {
should_turn_left--;
}
color += 50;
color %= 256;
//Filter to remove vertical and horizontal lines,
//and atan(0.09) equals about 5 degrees.
}
if (flag == 0 && isVisible == 1 && watch > 40) {
clog << "count++ " << lineCount << endl;
lineCount++;
isVisible = 0;
watch = 0;
}
#ifdef _DEBUG
stringstream overlayedText;
overlayedText<<"Lines: "<<lines.size();
putText(result,overlayedText.str(),Point(10,result.rows-10),2,0.8,Scalar(0,0,255),0);
imshow(MAIN_WINDOW_NAME,result);
#endif
lines.clear();
waitKey(1);
}
return 0;
}