/
raspberryPi.cpp
446 lines (314 loc) · 7.77 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include "raspi.h"
#include <time.h>
#include <fstream>
float tempo(clock_t t){
float temp;
t = clock() - t;
temp = ((float)t) / CLOCKS_PER_SEC;
cout << temp << endl;
return temp;
}
float Angle(Mat img, Point point, int d){
int ang;
int x, y, z;
float l1,l2;
x = point.x - d;
for (y = img.size().height; y > 0; y--){
if (img.at<uchar>(y, x) == 0) {
z = y;
break;
}
}
l1 = sqrt(point.x*point.x + point.y*point.y);
l2 = sqrt(x*x + z*z);
float dot = point.x * x + point.y * z;
float a = dot / (l1 * l2);
if (a >= 1.0)
return 0.0;
else if (a <= -1.0)
return acos(a);
else
return acos(a); // 0..PI
}
void MyLine(Mat img, Point start, Point end, string image_name, float angle)
{
int lineEnd;
int thickness = 2;
int lineType = 8;
// create 8bit color image. IMPORTANT: initialize image otherwise it will result in 32F
cv::Mat img_rgb(img.size(), CV_8UC3);
// convert grayscale to color image
cv::cvtColor(img, img_rgb, CV_GRAY2RGB);
lineEnd = end.x;
end.x = end.x + lineEnd * angle;
line(img_rgb,
start,
end,
Scalar(0, 0, 255),
thickness,
lineType);
end.x = lineEnd - lineEnd*angle;
line(img_rgb,
start,
end,
Scalar(0, 0, 255),
thickness,
lineType);
String name = "Line" + image_name;
imwrite(name, img_rgb);
ShowImage(img_rgb, img);
}
/*void save(Mat img, string name){
string filename;
time_t t = time(0); // get time now
struct tm * now = localtime(&t);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d_%H-%M-%s.jpg", now);
string date = (buffer);
filename = name + date;
imwrite(filename, img);
}*/
void savePictures(Mat img, string name, string filter){
string filename = filter + "_" + name ;
imwrite(filename, img);
}
Point maxPoint(Mat aux){
int hight = 0;
int width = 0;
int x, y;
for (x = aux.size().width * 0.5; x < aux.size().width - (aux.size().width*0); x++){
for ( y = 0; y < aux.size().height; y++){
if (aux.at<uchar>(y, x)==0) {
if (y > hight){
hight = y;
width = x;
printf(" x Y : %d %d ", width, hight);
}
break;
}
}
}
printf(" CIMA P BAIXO : x Y : %d %d", width, hight);
Point max = Point(width, hight);
return max;
}
Point minPoint(Mat aux){
int hight = aux.size().height;
int width = 0;
int x, y;
for (x = 50; x < aux.size().width - 50; x++){
for (y = aux.size().height; y > 0; y--){
if (aux.at<uchar>(y, x) == 0) {
if (hight > y){
hight = y;
width = x;
printf(" x Y : %d %d ", width, hight);
}
break;
}
}
}
printf(" Baixo P CIMA : x Y : %d %d", width, hight);
Point min = Point(width, hight);
return min;
}
int intermadiatePoint(Point point, Mat img){
int minPointY;
for (int y = point.y; y < img.size().height; y++){
if (img.at<uchar>(y, point.x) == 255) {
minPointY = y;
break;
}
}
return minPointY;
}
int main(int argc, char ** argv)
{
string gauss = "Gaussino";
string canny = "Canny";
string hough = "Hough";
string binarizar = "Binarizar";
string Otsu = "Otsu";
string image_name = "";
int number;
Point min, max, start;
ofstream myfile;
myfile.open("data.txt");
myfile << "ESCREVE QUALQUER COISA\n";
clock_t t1, t2, t3, t4;
double threshold1, threshold2, thres, minLength, maxGap;
bool f1, f2, f3, f4, f5, f6, f7, f8, f9;
string Result;
ostringstream convert;
//int i;
float temp;
//for (i = 1; i <= 6; i++){
//number = i;
//convert << number;
//Result = convert.str();
//image_name = "a" + Result + ".JPG";
image_name = "a2.JPG";
//number++;
//cout << number << endl;
cout << image_name;
myfile << image_name;
myfile << "\n";
t1 = clock();
f1 = false;
f2 = true;
f3 = false;
f4 = false;
f5 = false;
f6 = true;
f7 = true;
if (f7 == true){
threshold1 = 10;
threshold2 = 19;
}
f8 = false;
f9 = true;
if (f9 == true){
thres = 10;// 40
minLength = 20; //50
maxGap = 30; //80
/*
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
string original = "original.jpg";
string foto ="img";
IplImage* frame = cvQueryFrame( capture );
Mat img(frame);
Mat I, I1, imge;
cvtColor(img,imge,CV_RGB2GRAY);
imge.convertTo(I, CV_8U);
equalizeHist(I,I1);
Mat aux = I1;
savePictures(I1, original, foto);
*/
//realiza a leitura e carrega a imagem para a matriz I1
// a imagem tem apenas 1 canal de cor e por isso foi usado o parametro CV_LOAD_IMAGE_GRAYSCALE
Mat lara = imread("lara.JPG", CV_LOAD_IMAGE_GRAYSCALE);
Mat I = imread(image_name, CV_LOAD_IMAGE_GRAYSCALE);
if (I.empty())
return -1;
resize(I, I, lara.size(), 1.0, 1.0, INTER_LINEAR);
Mat I1;
//Mat aux = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
equalizeHist(I, I1);
Mat aux, original;
aux = I1;
//ShowImage(I, I1);
// verifica se carregou e alocou a imagem com sucesso
if (I1.empty())
return -1;
// tipo Size contem largura e altura da imagem, recebe o retorno do metodo .size()
//imSize = I1.size();
// Cria uma matriz do tamanho imSize, de 8 bits e 1 canal
Mat I2 = Mat::zeros(I1.size(), CV_8UC1);
if (f2 == true) {
t2 = clock();
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2)
GaussianBlur(I1, I1, Size(i, i), 0, 0, BORDER_DEFAULT);
//ShowImage(aux, I1);
cout << "Guassiano tempo : ";
temp = tempo(t2);
savePictures(I1, image_name, gauss);
myfile << "Gauss: ";
myfile << temp;
myfile << "\n";
}
if (f1 == true){
t2 = clock();
binarizacao(I1, 125);
//ShowImage(aux, I1);
cout << "binarizacao : ";
temp = tempo(t2);
savePictures(I1, image_name, binarizar);
myfile << "Binarizacao: ";
myfile << temp;
myfile << "\n";
}
if (f3 == true){
t2 = clock();
inversao(I1);
cout << "inversao : ";
tempo(t2);
}
if (f4 == true){
adaptiveThreshold(I1, I1, 255, ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 7, 0);
}
if (f5 == true)
Laplacian(I1, I1, 125, 1, 1, 0, BORDER_DEFAULT);
if (f7 == true){
t2 = clock();
Canny(I1, I2, threshold1, threshold2, 3, false);
cout << "canny : ";
temp = tempo(t2);
savePictures(I2, image_name, canny);
myfile << "Canny: " + (int)(temp * 1000);
myfile << "\n";
}
if (f9 == true){
t2 = clock();
Hough(I2, aux, thres, minLength, maxGap);
cout << "hough : ";
temp = tempo(t2);
savePictures(aux, image_name, hough);
myfile << "Hough: ";
myfile << temp;
myfile << "\n";
}
if (f6 == true){
t2 = clock();
threshold_type = THRESH_BINARY;
threshold(aux, I1, 9, max_BINARY_value, threshold_type);
cout << "Threshold : ";
//savePictures(aux, image_name, Otsu);
temp = tempo(t2);
myfile << "Threshold/OTSU: ";
myfile << temp;
myfile << "\n";
}
string name = Otsu + image_name;
imwrite(name, aux);
ShowImage(I1, aux);
t2 = clock();
max = maxPoint(aux);
min = minPoint(aux);
/*start.y = (max.y + min.y) / 2;
start.x = (max.x + min.x) /2;*/
start.x = max.x;
start.y = max.y;
Point end;
end.x = start.x;
end.y = aux.size().height;
MyLine(I, start, end, image_name, 0.3);
temp = tempo(t2);
ShowImage(I, aux);
myfile << "Rota: ";
myfile << temp;
myfile << "\n";
temp = tempo(t1);
cout << "Final time : ";
myfile << "Final Time: ";
myfile << temp;
myfile << "\n";
//float angle = Angle(aux, min, 5);
//cout << angle;
}
//}
myfile.close();
//ShowImage(aux, I1);
//imwrite(argv[2], I2); // salva imagem I2 no arquivo definido pelo usuario em argv[2]
//}
return 0;
}