-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesteOCV.cpp
More file actions
289 lines (204 loc) · 6.72 KB
/
Copy pathtesteOCV.cpp
File metadata and controls
289 lines (204 loc) · 6.72 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
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
281
282
283
284
285
286
287
288
289
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
#include "opencv2/video/tracking.hpp"
using namespace cv;
using namespace std;
string intToString(int number) {
std::stringstream ss;
ss << number;
return ss.str();
}
class Symbol {
public:
Mat img;
string name;
};
void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center) {
std::vector<cv::Point2f> top, bot;
for (unsigned int i = 0; i < corners.size(); i++) {
if (corners[i].y < center.y)
top.push_back(corners[i]);
else
bot.push_back(corners[i]);
}
cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
corners.clear();
corners.push_back(tl);
corners.push_back(tr);
corners.push_back(br);
corners.push_back(bl);
}
int readRefImages(Symbol *symbols) {
symbols[0].img = imread("arrowL.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[0].img.data)
return -1;
threshold(symbols[0].img, symbols[0].img, 100, 255, 0);
symbols[0].name = "Left 90";
symbols[1].img = imread("arrowR.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[1].img.data)
return -1;
threshold(symbols[1].img, symbols[1].img, 100, 255, 0);
symbols[1].name = "Right 90";
symbols[2].img = imread("arrowT.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[2].img.data)
return -1;
threshold(symbols[2].img, symbols[2].img, 100, 255, 0);
symbols[2].name = "Turn Around";
symbols[3].img = imread("arrowB.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[3].img.data)
return -1;
threshold(symbols[3].img, symbols[3].img, 100, 255, 0);
symbols[3].name = "Ball";
symbols[4].img = imread("arrowL45.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[4].img.data)
return -1;
threshold(symbols[4].img, symbols[4].img, 100, 255, 0);
symbols[4].name = "Left 45";
symbols[5].img = imread("arrowR45.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[5].img.data)
return -1;
threshold(symbols[5].img, symbols[5].img, 100, 255, 0);
symbols[5].name = "Right 45";
symbols[6].img = imread("arrowStop.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[6].img.data)
return -1;
threshold(symbols[6].img, symbols[6].img, 100, 255, 0);
symbols[6].name = "Stop";
symbols[7].img = imread("arrowGo.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!symbols[7].img.data)
return -1;
threshold(symbols[7].img, symbols[7].img, 100, 255, 0);
symbols[7].name = "Go";
return 0;
}
int lowThreshold;
void CannyThreshold(int, void*) {
}
int main(int argc, char** argv) {
// check http://docs.opencv.org/doc/tutorials/features2d/table_of_content_features2d/table_of_content_features2d.html
// for OpenCV general detection/matching framework details
VideoCapture cap;
cap.open(0);
namedWindow("A", CV_WINDOW_AUTOSIZE);
namedWindow("B", CV_WINDOW_AUTOSIZE);
namedWindow("C", CV_WINDOW_AUTOSIZE);
Mat camera;
Mat greyImg;
Symbol symbols[10];
if (readRefImages(symbols) == -1) {
printf("Error reading reference symbols\n");
return -1;
}
createTrackbar("Min Threshold:", "A", &lowThreshold, 100, CannyThreshold);
double t = (double) getTickCount();
// do something ...
//t = ((double)getTickCount() - t)/getTickFrequency();
//cout << "Times passed in seconds: " << t << endl;
cap >> camera;
Mat new_image;
while (1) {
t = (double) getTickCount() - t;
cap >> camera;
cvtColor(camera, greyImg, CV_RGB2GRAY);
//threshold(greyImg, greyImg, 100, 255, 0);
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
GaussianBlur(greyImg, greyImg, Size(9, 9), 2, 2);
/// Detect edges using canny
Canny(greyImg, canny_output, lowThreshold, lowThreshold * 3, 3);
// imshow("B",canny_output);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE,
CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Point> approxRect;
for (size_t i = 0; i < contours.size(); i++) {
approxPolyDP(contours[i], approxRect,
arcLength(Mat(contours[i]), true) * 0.05, true);
if (approxRect.size() == 4) {
float area = contourArea(contours[i]);
if (area > 10000) {
std::vector<cv::Point2f> corners;
vector<Point>::iterator vertex;
vertex = approxRect.begin();
//vertex++;
circle(camera, *vertex, 2, Scalar(0, 0, 255), -1, 8, 0);
corners.push_back(*vertex);
vertex++;
circle(camera, *vertex, 2, Scalar(0, 0, 255), -1, 8, 0);
corners.push_back(*vertex);
vertex++;
circle(camera, *vertex, 2, Scalar(0, 0, 255), -1, 8, 0);
corners.push_back(*vertex);
vertex++;
circle(camera, *vertex, 2, Scalar(0, 0, 255), -1, 8, 0);
corners.push_back(*vertex);
Moments mu;
mu = moments(contours[i], false);
Point2f center(mu.m10 / mu.m00, mu.m01 / mu.m00);
sortCorners(corners, center);
// Define the destination image
Mat correctedImg = ::Mat::zeros(195, 271, CV_8UC3);
// Corners of the destination image
std::vector<cv::Point2f> quad_pts;
quad_pts.push_back(Point2f(0, 0));
quad_pts.push_back(Point2f(correctedImg.cols, 0));
quad_pts.push_back(
Point2f(correctedImg.cols, correctedImg.rows));
quad_pts.push_back(Point2f(0, correctedImg.rows));
// Get transformation matrix
Mat transmtx = getPerspectiveTransform(corners, quad_pts);
// Apply perspective transformation
warpPerspective(camera, correctedImg, transmtx,
correctedImg.size());
Mat correctedImgBin;
cvtColor(correctedImg, correctedImgBin, CV_RGB2GRAY);
//equalizeHist(correctedImgBin, correctedImgBin);
correctedImgBin.copyTo(new_image);
threshold(correctedImgBin, correctedImgBin, 140, 255, 0);
imshow("B", correctedImgBin);
double minVal,maxVal,medVal;
minMaxLoc(new_image, &minVal, &maxVal);
medVal=(maxVal-minVal)/2;
threshold(new_image, new_image, medVal, 255, 0);
imshow("C", new_image);
Mat diffImg;
int match, minDiff, diff;
minDiff = 12000;
match = -1;
for (int i = 0; i < 8; i++) {
//diffImg = symbols[i].img-correctedImgBin;
bitwise_xor(new_image, symbols[i].img, diffImg,
noArray());
diff = countNonZero(diffImg);
if (diff < minDiff) {
minDiff = diff;
match = i;
}
if (i == 0) {
// imshow("B",diffImg);
}
}
//imshow("B", correctedImg);
if (match != -1) {
putText(camera, symbols[match].name, Point(320, 30), 1,
2, Scalar(0, 255, 0), 2);
}
//break;
}
}
}
imshow("A", camera);
waitKey(20);
}
}