-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDetect.cpp
More file actions
293 lines (228 loc) · 8.88 KB
/
Detect.cpp
File metadata and controls
293 lines (228 loc) · 8.88 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
290
291
292
293
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opencv2/text.hpp"
using namespace cv;
using namespace cv::text;
using namespace std;
Mat src; Mat src_gray;
Mat src_split[3];
int thresh = 80;
int max_thresh = 255;
RNG rng(12345);
int lowThreshold = 110;
int const max_lowThreshold = 255;
int ratio = 3;
int kernel_size = 3;
Mat dst, detected_edges;
Ptr<OCRTesseract> ocr = OCRTesseract::create(NULL, NULL, "0123456789", 3, 6);
/// Function header
void thresh_callback(int, void* );
void blob_detector();
void recognize_chars(Mat img);
/** @function main */
int main( int argc, char** argv ){
/// Load source image and convert it to gray
//cout << "opening video" << endl;
VideoCapture capture("/media/pi/VID/vids.mp4");
//VideoCapture capture(0);
//cout << "video open" << endl;
//src = imread( argv[1], CV_LOAD_IMAGE_COLOR);
/// Convert image to gray and blur it
//blur( src_gray, src_gray, Size(3,3) );
/// Create Window
//namedWindow( "RGB", CV_WINDOW_AUTOSIZE );
//imshow( "RGB", src );
//cout << "reading frame" << endl;
if(!capture.read(src)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
//cout << "before callback" << endl;
thresh_callback( 0, 0 );
//blob_detector();
//cout << "tresh, callback" << endl;
namedWindow( "src", WINDOW_NORMAL );
//resizeWindow("src", 400, 280);
//cout << "window" << endl;
createTrackbar( "Min Threshold:", "src", &lowThreshold, max_lowThreshold);
//cout << "trackbar created" << endl;
int time, complete;
dst.create( src.size(), src.type() );
while(true){
//cout << "reading frame" << endl;
time = clock();
complete = clock();
if(!capture.read(src)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
//cout << "elapsed for reading: " << ((clock() - time) / (float) (float) 800) << endl;
imshow( "src", src);
thresh_callback(0, 0);
//cout << "elapsed ALL TOGETHER: " << ((clock() - complete) / (float) (float) 800) << endl << endl << endl;
waitKey(1);
//cout << "frame read" << endl;
}
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void* ){
/// Using Canny's output as a mask, we display our result
//dst = Scalar::all(0);
//src.copyTo( dst, detected_edges);
//imshow( "canny", dst );
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//cout << "in callback" << endl;
int time = clock();
split(src, src_split);
//cvtColor( src_split[0], src_gray, CV_BGR2GRAY );
src_gray= src_split[0];
//cout << "elapsed for grayscale: " << (clock() - time) / (float) 800 << endl;
//time = clock();
equalizeHist(src_gray, src_gray);
//cout << "elapsed for equalizing: " << (clock() - time) / (float) 800 << endl;
//time = clock();
int blur_size = 2;
blur( src_gray, detected_edges, Size(blur_size,blur_size) );
/// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
/// Detect edges using Threshold
//threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
//cout << "elapsed for canny: " << (clock() - time) / (float) 800 << endl;
//time = clock();
/// Find contours
/*Mat element = getStructuringElement( MORPH_ELLIPSE,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
erode( detected_edges, detected_edges, element );
/*
float factor = 1.5;
element = getStructuringElement( MORPH_ELLIPSE,
Size( 2*erosion_size*factor + 1, 2*erosion_size*factor+1 ),
Point( erosion_size*factor, erosion_size*factor) );
erode( detected_edges, detected_edges, element );
*/
findContours( detected_edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
//cout << "elapsed for contours: " << (clock() - time) / (float) 800 << endl;
//time = clock();
/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minEllipse;
Mat img = Mat::zeros( src.size(), CV_8UC3 );
for( int i = 0; i < contours.size(); i++ ){
if( contours[i].size() > 100 ){
if (contourArea(contours[i]) > 1000){
RotatedRect temp = fitEllipse(Mat(contours[i]));
if( 0.80 < abs(temp.size.width/temp.size.height) && abs(temp.size.width/temp.size.height) < 1.2){
if (temp.size.width*temp.size.height/4*3.14 <= 1.1 * contourArea(contours[i])) {
//cout << area(temp) << " < 1.1* " << contourArea(contours[i]) << endl;
minEllipse.push_back(temp);
//drawContours(img, contours, i, Scalar(0,255,0), -1, 8);
//ellipse(img, temp, Scalar(0,255,0), 2, 8);
} else {
//cout << "Reject ellipse " << i << endl;
//drawContours(img, contours, i, Scalar(0,0,100), -1, 8);
//ellipse(img, temp, Scalar(0,0,100), 2, 8);
}
}
}
}
}
//namedWindow("Ellipses", WINDOW_NORMAL);
//imshow("Ellipses", detected_edges);
//waitKey(1);
//cout << "elapsed for fitting elipses: " << (clock() - time) / (float) 800 << endl;
//time = clock();
/// Draw contours + rotated rects + ellipses
/*
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ ){
if(contours[i].size()> 100){
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// contour
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
}
cout << "elapsed for drawing contours :" << (clock() - time) / (float) 800 << endl;
time = clock();
*/
/*for (int i = 0; i < minEllipse.size(); ++i)
{
// ellipse
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
ellipse( drawing, minEllipse[i], color, 2, 8 );
}*/
//printf("%d\n", matches.size());
/*
Scalar color;
for(int i = 0; i < minEllipse.size(); i++){
ellipse( drawing, minEllipse[i], color, 2, 8 );
ellipse( threshold_output, minEllipse[i], 170, 2, 8 );
}
cout << "elapsed for drawing ellipses:" << (clock() - time) / (float) 800 << endl;
time = clock();
*/
Mat cropped;
float crop = 0.6;
for(int i = 0; i < minEllipse.size(); i++){
int cx = minEllipse[i].center.x;
int cy = minEllipse[i].center.y;
int x = minEllipse[i].size.width * crop;
int y = minEllipse[i].size.height * crop;
if(0 < cx - x && 0 < cy - y && cx+x < src.cols && cy+y < src.rows){
float crop = 0.65;
Rect myROI(cx-x*crop, cy-y*crop, 2*x*crop, 2*y*crop);
cropped = src(myROI);
recognize_chars(cropped);
}
}
// Show in a window
//namedWindow( "tresholded", WINDOW_NORMAL );
//resizeWindow("tresholded", 400, 300);
//namedWindow( "Contours", WINDOW_NORMAL );
//resizeWindow("Contours", 400, 300);
//imshow( "Contours", drawing );
//namedWindow( "src", WINDOW_NORMAL );
//resizeWindow("src", 400, 280);
//imshow("src", src);
//imshow( "tresholded", threshold_output );
//cout << "elapsed for displaying:" << (clock() - time) / (float) 800 << endl;
//time = clock();
}
void recognize_chars(Mat img){
cvtColor( img, img, CV_BGR2GRAY );
equalizeHist(img, img);
threshold( img, img, 100, 255, THRESH_BINARY );
int erosion_size = img.cols/30;
//cout << "es " << erosion_size << endl;
Mat element = getStructuringElement( MORPH_ELLIPSE,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
dilate( img, img, element );
namedWindow( "sign", CV_WINDOW_AUTOSIZE );
imshow( "sign", img );
waitKey(1);
//printf("%s\n", "ocr started");
std::string speed;
speed = ocr -> run(img, 60);
if(speed.length() > 0){
int spd = stoi(speed);
switch(spd){
case 30:
case 40:
case 50:
case 60:
case 70:
case 90:
case 100:
cout << "SPEED: " << speed << endl;
}
}
}