-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
149 lines (112 loc) · 3.01 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
145
146
147
148
149
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <deque>
#include <thread>
#include "UKC_GPIO.h"
using namespace cv;
using namespace std;
RNG rng(12345);
char mode = 0; //debug mode 0 = disable 1 = enable
int tail = 10; //tail on object in debug
int flag_bird = 0; //exist bird
int flag_stat = 0;
Rect min_birdsize = Rect(0,0,20,20); //0 ,0, min width pixel , min height pixel
void bird();
Rect ContoursNBoxing(Mat src);
Mat MovingMat(Mat src, Mat pre_src);
void GPIO_F();
void OPENCV_F();
UKC_GPIO GPIO;
//////////////////////////////// ** main ** //////////////////////////////////////////////
int main(){
GPIO.GPIO_init();
thread GPIO_th(&GPIO_F);
thread OpenCV_th(&OPENCV_F);
GPIO_th.join();
OpenCV_th.join();
return 0;
}
////////////////////////////// ** Thread ** /////////////////////////////////////
void GPIO_F(){
while(1){
if(flag_bird == 0){
GPIO.Motor1_off();
}
else if (flag_bird == 1){
GPIO.Motor1_on();
delay(300);
}
delay(30);
}
}
void OPENCV_F(){
mode = 1;
bird();
}
///////////////////////////////** OpenCV **//////////////////////////////////////////////
void bird() {
//init
Mat frame;
Mat pre_frame;
Mat res;
Mat cam;
Rect TRect;
Point2f center;
Point cent = Point(0, 0);
//frame init
VideoCapture video(0);
video >> cam;
cvtColor(cam, frame, COLOR_BGR2GRAY);
while (true){
//get frame
frame.copyTo(pre_frame);
video >> cam;
cvtColor(cam, frame, COLOR_BGR2GRAY);
res = MovingMat(frame, pre_frame); //detect moving
TRect = ContoursNBoxing(res); //Contours & Boxing
cent = ((TRect.br() + TRect.tl()) * 0.5 ); //get result center
if (TRect.height < min_birdsize.height && TRect.width < min_birdsize.width)
flag_bird = 0;
else
flag_bird = 1;
if(mode){
cout << "bird = " << cent << endl;
cout << flag_bird <<endl;
rectangle(res,TRect,Scalar(255,255,255));
rectangle(cam,TRect,Scalar(0,255,0),1);
imshow("bird",cam);
imshow("move",res);
}
if (waitKey(1) == 27) {
break;
}
}
return;
}
Mat MovingMat(Mat src , Mat pre_src) {
Mat res;
Mat T1;
Mat T2;
blur(src, src, Size(5, 5));
T1 = pre_src - src;
T2 = src - pre_src;
res = T1 + T2;
threshold(res, res, 20, 255, THRESH_BINARY);
return res;
}
Rect ContoursNBoxing(Mat src) {
//Contours & Boxing
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<vector<Point> > contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
Rect TRect = Rect(Point(0, 0), Point(0, 0));
for (int i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
if ((TRect.height * TRect.width) < (boundingRect(Mat(contours_poly[i])).height * boundingRect(Mat(contours_poly[i])).width))
TRect = boundingRect(Mat(contours_poly[i]));
}
return TRect;
}