-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
175 lines (135 loc) · 4.64 KB
/
Source.cpp
File metadata and controls
175 lines (135 loc) · 4.64 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
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <string>
#include <time.h>
#include "opencv2/gpu/gpu.hpp"
using namespace cv;
using namespace std;
using namespace gpu;
int main(int argc, char** argv)
{
//setup all my variables
double neededAngle;
double offset;
int width;
double angle;
int height;
cout << "Resolution Width: ";
cin >> width;
cout << "Resolution Height: ";
cin >> height;
cout << "Camera FOV: ";
cin >> angle;
//Half is always a decimal becaues the pixels start at 0; This functions determines the midway point of the camera view
double half = width - 1;
half = half / 2;
float minTargetRadius;
cout << "Minimum Target Radius: ";
cin >> minTargetRadius;
int i = 0;
angle = angle / width;
vector<double> vec;
VideoCapture cap(0); //capture the video from webcam
vector<cv::Point2i> center;
vector<int> radius;
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
int iLowH = 44;
int iHighH = 99;
int iLowS = 114;
int iHighS = 255;
int iLowV = 93;
int iHighV = 255;
//Create trackbars in "Control" window
createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Control", &iHighH, 179);
createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "Control", &iHighS, 255);
createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
createTrackbar("HighV", "Control", &iHighV, 255);
int iLastX = -1;
int iLastY = -1;
Mat imgTmp;
namedWindow("Stream", CV_WINDOW_NORMAL);
//Capture a temporary image from the camera
cap.read(imgTmp);
//Create a black image with the size as the camera output
Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);
while (true)
{
Mat imgOriginal;
cap.read(imgOriginal); // read a new frame from video
imshow("Stream", imgOriginal);
GpuMat g_imgHSV;
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
GpuMat g_imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
g_imgThresholded.upload(imgThresholded);
//morphological opening (removes small objects from the foreground)
gpu::erode(g_imgThresholded, g_imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
gpu::dilate(g_imgThresholded, g_imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
//morphological closing (removes small holes from the foreground)
gpu::dilate(g_imgThresholded, g_imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
gpu::erode(g_imgThresholded, g_imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
int thresh = 100;
GpuMat g_canny_output;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
RNG rng(12345);
gpu::Canny(g_imgThresholded, g_canny_output, thresh, thresh * 2, 3);
/// Find contours
Mat canny_output;
g_canny_output.download(canny_output);
findContours(canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
}
/*This function finds if there are any contours, and if there are
Then we will first find the center, then add that to a vector(array); This is because there can be more
than one contours */
if (contours.size() > 0) {
for (int i = 0; i < contours.size(); i++) {
if (cv::contourArea(contours[i]) > 1) {
cv::Point2f c;
float r;
cv::minEnclosingCircle(contours[i], c, r);
if (r >= minTargetRadius)
{
center.push_back(c);
radius.push_back(r);
}
}
}
size_t count = center.size();
}
imshow("contours", drawing);
int ssize = center.size();
int nsize = center.size() - 1;
//This is an equation I got from Cheesy Poofs to find out what angle we need to be at
if (ssize > 0) {
offset = center[nsize].x - half;
neededAngle = offset * angle;
cout << neededAngle << endl;
}
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}