-
Notifications
You must be signed in to change notification settings - Fork 1
/
最终版.cpp
239 lines (238 loc) · 6.65 KB
/
最终版.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
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat img;
void one() {
VideoCapture cap(1);
while (true) {
cap.read(img);
Mat imgGray, imgBlur, imgHSV, imgCanny, imgTwo, imgDil, imgErode;
//resize(img, img, Size(500, 500));
cvtColor(img, imgHSV, COLOR_BGR2HSV);
inRange(imgHSV, Scalar(100, 50, 50), Scalar(130, 255, 255), imgTwo);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(imgTwo, imgErode, kernel);
dilate(imgErode, imgDil, kernel);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imgDil, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
double maxarea = 0;
int m, t;
for (int i = 0; i < contours.size(); i++) {
double area = contourArea(contours[i]);
//cout << area << endl;
if (area > maxarea) {
maxarea = area;
m = i;
}
}
maxarea = contourArea(contours[0], true);
for (int i = 0; i < contours.size() && i != m; i++) {
double area = contourArea(contours[i]);
if (area > maxarea) {
maxarea = area;
t = i;
}
}
RotatedRect rect = minAreaRect(contours[t]);
Point2f rectPoints[4];
rect.points(rectPoints);
for (int j = 0; j < 4; j++) {
line(img, rectPoints[j], rectPoints[(j + 1) % 4], Scalar(0, 255, 0), 4, 8, 0);
}
//cout << "X坐标:" << rect.center.x << " Y坐标:" << rect.center.y << " 偏转角度:" << rect.angle << endl;
if (rect.angle <= 50 && rect.angle >= 30) {
cout << "右转" << endl;
//return"右转";
}
else if ((rect.angle > 0 && rect.angle < 20) || (rect.angle >= 80 && rect.angle < 90)) {
cout << "直行" << endl;
//return"直行";
}
else {
cout << "左转" << endl;
//return"左转";
}
//imshow("img Two", imgTwo);
//imshow("img Erode", imgErode);
//imshow("img Dil", imgDil);
imshow("img", img);
waitKey(1);
}
}
void two() {
VideoCapture cap(1);
while (true) {
cap.read(img);
//string path = "C:\\Users\\PC\\Desktop\\5.jpg";
//img = imread(path);
Mat imgGray, imgBlur, imgHSV, imgCanny, imgTwo, imgDil, imgErode;
resize(img, img, Size(500, 500));
cvtColor(img, imgHSV, COLOR_BGR2HSV);
inRange(imgHSV, Scalar(18, 50, 103), Scalar(60, 255, 255), imgTwo);
Mat kernel = getStructuringElement(MORPH_RECT, Size(9, 9));
erode(imgTwo, imgErode, kernel);
dilate(imgErode, imgDil, kernel);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imgDil, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
double maxarea = 0;
int m, t = 0; {};
for (int i = 0; i < contours.size(); i++) {
double area = contourArea(contours[i]);
cout << area << endl;
if (area > maxarea) {
maxarea = area;
m = i;
}
}
maxarea = contourArea(contours[0], true);
for (int i = 0; i < contours.size() && i != m; i++) {
double area = contourArea(contours[i]);
if (area > maxarea) {
maxarea = area;
t = i;
}
}
Rect rect = boundingRect(contours[t]);
//Point2f rectPoints[4];
//rect.points(rectPoints);
//for (int j = 0; j < 4; j++) {
//line(img, rectPoints[j], rectPoints[(j + 1) % 4], Scalar(0, 255, 0), 4, 8, 0);
//}
rectangle(img, rect.tl(), rect.br(), Scalar(0, 255, 0), 5);
float a = rect.width / rect.height;
cout << a << endl;
float area1 = rect.height * rect.width;
cout << area1 << endl;
if (area1 < 5500) {
cout << "空白" << endl;
//return"空白";
}
else if (a < 1) {
cout << "R面" << endl;
//return"R面";
}
else if (a >= 1) {
cout << "条形码面" << endl;
//return"条形码面";
}
imshow("img Two", imgTwo);
imshow("img Erode", imgErode);
imshow("img Dil", imgDil);
imshow("img", img);
waitKey(1);
}
}
void three() {
VideoCapture cap(1);
while (true) {
cap.read(img);
//string path = "C:\\Users\\PC\\Desktop\\7.jpg";
//img = imread(path, IMREAD_COLOR);
//if (img.empty())
//{
//cout << "Could not open or find the image" << std::endl;
//return -1;
//}
resize(img, img, Size(500, 500));
Mat imgGray, imgBlur, imgHSV, imgCanny, imgTwo, imgDil, imgErode, thresHold, a, b, c, d;
vector<Mat>hsvSplit;
vector<Mat>channels;
cvtColor(img, imgHSV, COLOR_BGR2HSV);
split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, imgHSV);
split(img, channels);
meanStdDev(channels[0], a, b);
meanStdDev(channels[2], c, d);
double m = a.at<double>(0, 0);
double n = c.at<double>(0, 0);
if (m > n) {
cout << "蓝色" << endl;
//return"蓝色";
}
else {
cout << "红色" << endl;
//return"红色";
}
threshold(hsvSplit[2], thresHold, 240, 245, THRESH_BINARY);
blur(thresHold, thresHold, Size(3, 3));
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
dilate(thresHold, element, element);
vector<RotatedRect> vc;
vector<RotatedRect> vRec;
vector<vector<Point>>contours;
findContours(element.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
double area = contourArea(contours[i]);
//cout << area << endl;
if (area < 15 || contours[i].size() <= 10)
continue;
RotatedRect Light = fitEllipse(contours[i]);
if (Light.size.width / Light.size.height > 1.5
|| area / Light.size.area() < 0.5)
continue;
Light.size.height *= 1.1;
Light.size.width *= 1.1;
vc.push_back(Light);
//vector<Rect>rect(contours.size());
//rectangle(img, rect[i].tl(), rect[i].br(), Scalar(0, 255, 0), 5);
}
for (size_t i = 0; i < vc.size(); i++)
{
for (size_t j = i + 1; (j < vc.size()); j++)
{
Point2f point1;
Point2f point2;
point1.x = vc[i].center.x;
point1.y = vc[i].center.y + 20;
point2.x = vc[j].center.x;
point2.y = vc[j].center.y - 20;
rectangle(img, point1, point2, Scalar(0, 255, 0), 2);
double height, width;
height = (vc[i].size.height + vc[j].size.height) / 2;
width = sqrt((vc[i].center.x - vc[j].center.x) * (vc[i].center.x - vc[j].center.x) + (vc[i].center.y - vc[j].center.y) * (vc[i].center.y - vc[j].center.y));
double ratio = height / width;
if (ratio <= 1) {
cout << "小装甲板" << endl;
//return"小装甲板";
}
else
{
cout << "大装甲板" << endl;
//return"大装甲板";
}
}
}
//imshow("img", img);
imshow("img thresHold", thresHold);
imshow("img hsv", imgHSV);
waitKey(1);
}
}
int main()
{
int i = 0;
while (true) {
if (i == 0) {
cin >> i;
}
if (i == 1) {
one();
i--;
}
if (i == 2) {
two();
i = i - 2;
}
if (i == 3) {
three();
i = i - 3;
}
}
return 0;
}