-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspberrypi.cpp
273 lines (232 loc) · 8.17 KB
/
raspberrypi.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
#include <opencv2/opencv.hpp>
#include <raspicam_cv.h>
#include <iostream>
#include <chrono>
#include <ctime>
#include <wiringPi.h>
using namespace std;
using namespace cv;
using namespace raspicam;
// Setting the required global opencv Mats
// NOTE: These must be global variables for the version of openCV, depending on the version the functions could return Mat
Mat frame, Matrix, framePers, frameGray, frameThresh, frameEdge, frameFinal, frameFinalDuplicate, frameFinalDuplicate1;
Mat ROILane, ROILaneEnd;
int LeftLanePos, RightLanePos, frameCenter, laneCenter, Result, laneEnd;
// Starting the camera and stringstream.
RaspiCam_Cv Camera;
stringstream ss;
// Setting the region of interest for edge detection.
vector<int> histrogramLane;
vector<int> histrogramLaneEnd;
Point2f Source[] = {Point2f(45, 135), Point2f(350, 135), Point2f(20, 185), Point2f(380, 185)};
Point2f Destination[] = {Point2f(100, 0), Point2f(280, 0), Point2f(100, 240), Point2f(280, 240)};
void Setup(int argc, char **argv, RaspiCam_Cv &Camera)
{
// Setting the camera attributes and settings.
Camera.set(CAP_PROP_FRAME_WIDTH, ("-w", argc, argv, 400));
Camera.set(CAP_PROP_FRAME_HEIGHT, ("-h", argc, argv, 240));
Camera.set(CAP_PROP_BRIGHTNESS, ("-br", argc, argv, 50));
Camera.set(CAP_PROP_CONTRAST, ("-co", argc, argv, 50));
Camera.set(CAP_PROP_SATURATION, ("-sa", argc, argv, 50));
Camera.set(CAP_PROP_GAIN, ("-g", argc, argv, 50));
Camera.set(CAP_PROP_FPS, ("-fps", argc, argv, 0));
}
void Capture()
{
Camera.grab();
Camera.retrieve(frame);
cvtColor(frame, frame, COLOR_BGR2RGB);
}
void Perspective()
{
line(frame, Source[0], Source[1], Scalar(0, 0, 255), 2);
line(frame, Source[1], Source[3], Scalar(0, 0, 255), 2);
line(frame, Source[3], Source[2], Scalar(0, 0, 255), 2);
line(frame, Source[2], Source[0], Scalar(0, 0, 255), 2);
Matrix = getPerspectiveTransform(Source, Destination);
warpPerspective(frame, framePers, Matrix, Size(400, 240));
}
// Canny edge detection.
void DetectEdges()
{
cvtColor(framePers, frameGray, COLOR_RGB2GRAY);
inRange(frameGray, 230, 255, frameThresh);
Canny(frameGray, frameEdge, 900, 900, 3, false);
add(frameThresh, frameEdge, frameFinal);
cvtColor(frameFinal, frameFinal, COLOR_GRAY2RGB);
cvtColor(frameFinal, frameFinalDuplicate, COLOR_RGB2BGR); // used in histrogram function only
cvtColor(frameFinal, frameFinalDuplicate1, COLOR_RGB2BGR); // used in histrogram function only
}
// Create the histogram.
void Histrogram()
{
histrogramLane.resize(400);
histrogramLane.clear();
for (int i = 0; i < 400; i++) // frame.size().width = 400
{
ROILane = frameFinalDuplicate(Rect(i, 140, 1, 100));
divide(255, ROILane, ROILane);
histrogramLane.push_back((int)(sum(ROILane)[0]));
}
histrogramLaneEnd.resize(400);
histrogramLaneEnd.clear();
for (int i = 0; i < 400; i++)
{
ROILaneEnd = frameFinalDuplicate1(Rect(i, 0, 1, 240));
divide(255, ROILaneEnd, ROILaneEnd);
histrogramLaneEnd.push_back((int)(sum(ROILaneEnd)[0]));
}
laneEnd = sum(histrogramLaneEnd)[0];
cout << "Lane END = " << laneEnd << endl;
}
void LaneFinder()
{
vector<int>::iterator LeftPtr;
LeftPtr = max_element(histrogramLane.begin(), histrogramLane.begin() + 150);
LeftLanePos = distance(histrogramLane.begin(), LeftPtr);
vector<int>::iterator RightPtr;
RightPtr = max_element(histrogramLane.begin() + 250, histrogramLane.end());
RightLanePos = distance(histrogramLane.begin(), RightPtr);
line(frameFinal, Point2f(LeftLanePos, 0), Point2f(LeftLanePos, 240), Scalar(0, 255, 0), 2);
line(frameFinal, Point2f(RightLanePos, 0), Point2f(RightLanePos, 240), Scalar(0, 255, 0), 2);
}
void LaneCenter()
{
laneCenter = (RightLanePos - LeftLanePos) / 2 + LeftLanePos;
frameCenter = 188;
line(frameFinal, Point2f(laneCenter, 0), Point2f(laneCenter, 240), Scalar(0, 255, 0), 3);
line(frameFinal, Point2f(frameCenter, 0), Point2f(frameCenter, 240), Scalar(255, 0, 0), 3);
Result = laneCenter - frameCenter;
}
int main(int argc, char **argv)
{
// Setting pinModes for sending data to Arduino.
wiringPiSetup();
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
// Connecting to camera and outputting if there is an error or fail.
Setup(argc, argv, Camera);
cout << "Connecting to camera" << endl;
if (!Camera.open())
{
cout << "Failed to Connect" << endl;
}
cout << "Camera Id = " << Camera.getId() << endl;
// Main loop that does the lane detection.
while (1)
{
auto start = std::chrono::system_clock::now();
Capture();
Perspective();
DetectEdges();
Histrogram();
LaneFinder();
LaneCenter();
// Threshold detection and send the data to arduino
if (laneEnd > 3000)
{
digitalWrite(23, 1);
digitalWrite(22, 1); // decimal = 7
digitalWrite(21, 1);
cout << "Lane End" << endl;
}
if (Result == 0)
{
digitalWrite(23, 0);
digitalWrite(22, 0); // decimal = 0
digitalWrite(21, 0);
cout << "Forward" << endl;
}
else if (Result > 0 && Result < 10)
{
digitalWrite(23, 1);
digitalWrite(22, 0); // decimal = 1
digitalWrite(21, 0);
cout << "Right1" << endl;
}
else if (Result >= 10 && Result < 20)
{
digitalWrite(23, 0);
digitalWrite(22, 1); // decimal = 2
digitalWrite(21, 0);
cout << "Right2" << endl;
}
else if (Result > 20)
{
digitalWrite(23, 1);
digitalWrite(22, 1); // decimal = 3
digitalWrite(21, 0);
cout << "Right3" << endl;
}
else if (Result < 0 && Result > -10)
{
digitalWrite(23, 0);
digitalWrite(22, 0); // decimal = 4
digitalWrite(21, 1);
cout << "Left1" << endl;
}
else if (Result <= -10 && Result > -20)
{
digitalWrite(23, 1);
digitalWrite(22, 0); // decimal = 5
digitalWrite(21, 1);
cout << "Left2" << endl;
}
else if (Result < -20)
{
digitalWrite(23, 0);
digitalWrite(22, 1); // decimal = 6
digitalWrite(21, 1);
cout << "Left3" << endl;
}
if (laneEnd > 3000)
{
ss.str(" ");
ss.clear();
ss << " Lane End";
putText(frame, ss.str(), Point2f(1, 50), 0, 1, Scalar(255, 0, 0), 2);
}
else if (Result == 0)
{
ss.str(" ");
ss.clear();
ss << "Result = " << Result << " Move Forward";
putText(frame, ss.str(), Point2f(1, 50), 0, 1, Scalar(0, 0, 255), 2);
}
else if (Result > 0)
{
ss.str(" ");
ss.clear();
ss << "Result = " << Result << "bMove Right";
putText(frame, ss.str(), Point2f(1, 50), 0, 1, Scalar(0, 0, 255), 2);
}
else if (Result < 0)
{
ss.str(" ");
ss.clear();
ss << "Result = " << Result << " Move Left";
putText(frame, ss.str(), Point2f(1, 50), 0, 1, Scalar(0, 0, 255), 2);
}
// output the camera output to screen
namedWindow("orignal", WINDOW_KEEPRATIO);
moveWindow("orignal", 0, 100);
resizeWindow("orignal", 640, 480);
imshow("orignal", frame);
namedWindow("Perspective", WINDOW_KEEPRATIO);
moveWindow("Perspective", 640, 100);
resizeWindow("Perspective", 640, 480);
imshow("Perspective", framePers);
namedWindow("Final", WINDOW_KEEPRATIO);
moveWindow("Final", 1280, 100);
resizeWindow("Final", 640, 480);
imshow("Final", frameFinal);
waitKey(1);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
float t = elapsed_seconds.count();
int FPS = 1 / t;
cout << "FPS = " << FPS << endl;
}
return 0;
}