-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
144 lines (115 loc) · 3.36 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include "GPIOlib.h"
#define PI 3.1415926
#define PREFER_DELTA 0.09
#define PREFER_ANGLE_DELTA 0.09
#define ANGLE 10
#define BIGANGLE 15
//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 = "/devideo0";
const string MAIN_WINDOW_NAME = "Processed Image";
const string CANNY_WINDOW_NAME = "Canny";
const int CANNY_LOWER_BOUND = 50;
const int CANNY_UPPER_BOUND = 250;
const int HOUGH_THRESHOLD = 80;
const int REC_WIDTH = 500;
const int REC_HEIGHT = 500;
void adjust(float theta1, float theta2){
float sum = theta1 + theta2;
clog << theta1 << " " << theta2 << " " << sum << "\n";
if (theta1 == 0){
turnTo(-BIGANGLE);
delay(75);
}
else if (theta2 == 0){
turnTo(BIGANGLE);
delay(60);
turnTo(0);
}
}
int main()
{
init();
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
//采集摄像头
cout << "摄像头 ";
cout << dWidth + ' ' + dHeight;
Mat image;
while (true)
{
capture >> image;
if (image.empty())
break;
//Set the ROI for the image
//调整到合适的位置
Rect roi(0, image.rows / 3, image.cols, image.rows / 3);
Mat imgROI = image(roi);
//Canny algorithm
Mat contours;
Canny(imgROI, contours, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND);
#ifdef _DEBUG
cv::imshow(CANNY_WINDOW_NAME, contours);
#endif
vector<Vec2f> lines;
HoughLines(contours, lines, 1, PI / 180, HOUGH_THRESHOLD);
Mat result(imgROI.size(), CV_8U, Scalar(255));
imgROI.copyTo(result);
clog << lines.size() << endl;
float maxRad = -2 * PI;
float minRad = 2 * PI;
//Draw the lines and judge the slope
float theta1 = 0;
float theta2 = 0;
for (vector<Vec2f>::const_iterator it = lines.begin(); it != lines.end(); ++it)
{
float rho = (*it)[0]; //First element is distance rho
float theta = (*it)[1]; //Second element is angle theta
//Filter to remove vertical and horizontal lines,
//and atan(0.09) equals about 5 degrees.
//水平的线
if (PI / 2 - PREFER_DELTA < theta&&theta<PI / 2 + PREFER_DELTA){
continue;
}
else {
if (rho > 0)
theta1 = theta;
else if (rho < 0)
theta2 = theta;
//画图
Point pt1(rho / cos(theta), 0);
Point pt2((rho - result.rows * sin(theta)) / cos(theta), result.rows);
line(result, pt1, pt2, Scalar(0, 255, 255), 3, CV_AA);
}
#ifdef _DEBUG
clog << "Line: (" << rho << "," << theta << ")\n";
#endif
}
adjust(theta1, theta2);
#ifdef _DEBUG
stringstream overlayedText;
overlayedText << "Lines: " << lines.size();
cv::putText(result, overlayedText.str(), Point(10, result.rows - 10), 2, 0.8, Scalar(0, 0, 255), 0);
cv::imshow(MAIN_WINDOW_NAME, result);
#endif
lines.clear();
cv::waitKey(1);
}
return 0;
}