-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource1.cpp
More file actions
249 lines (173 loc) · 5.79 KB
/
Source1.cpp
File metadata and controls
249 lines (173 loc) · 5.79 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
#include <sstream>
#include <string>
#include <iostream>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include "Colour.h"
#include <vector>
#include <fstream>
using namespace cv;
//default capture width and height
const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS = 50;
//minimum and maximum object area
const int MIN_OBJECT_AREA = 40 * 40;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;
//our hue, saturation, and value variables, we can change theese via slider
int lowH = 52;
int highH = 88;
int lowS = 91;
int highS = 255;
int lowV = 0;
int highV = 255;
void on_trackbar(int, void*)
{
}
//integer to string
string intToString(int number){
std::stringstream ss;
ss << number;
return ss.str();
}
//writes the data to a file
void writeToFile(int x, int y, int numDetected)
{
ofstream file;
file.open("data.txt");
file << x << "\n" << y << "\n" << numDetected << endl;
file.close();
}
//creates our trackbars or sliders for changing the variables
void createTrackbars(){
namedWindow("Control", CV_WINDOW_AUTOSIZE);
//Create trackbars in "Control" window
cvCreateTrackbar("LowH", "Control", &lowH, 255);
cvCreateTrackbar("HighH", "Control", &highH, 255);
cvCreateTrackbar("LowS", "Control", &lowS, 255);
cvCreateTrackbar("HighS", "Control", &highS, 255);
cvCreateTrackbar("LowV", "Control", &lowV, 255);
cvCreateTrackbar("HighV", "Control", &highV, 255);
}
//Draw the circle in the center of the detected object and print out the pixel value, additionally check if it is centered
void drawObject(int x, int y, Mat &frame, bool highest){
if (highest == true)
{
cv::circle(frame, cv::Point(x, y), 10, cv::Scalar(0, 255, 0));
if (x > 340) putText(frame, "move right", Point(0, 25), 1, 2, Scalar(0, 0, 255));
else if (x < 300) putText(frame, "move left", Point(0, 24), 1, 2, Scalar(0, 0, 255));
else putText(frame, "perfect", Point(0, 25), 1, 2, Scalar(0, 255, 0));
}
else
{
cv::circle(frame, cv::Point(x, y), 10, cv::Scalar(0, 0, 255));
}
cv::putText(frame, intToString(x) + " , " + intToString(y), cv::Point(x, y + 20), 1, 1, Scalar(0, 255, 0));
}
//erodes and dialates the image so we can get rid of noise
void morphOps(Mat &thresh){
Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat dilateElement = getStructuringElement(MORPH_RECT, Size(9, 9));
erode(thresh, thresh, erodeElement);
erode(thresh, thresh, erodeElement);
dilate(thresh, thresh, dilateElement);
dilate(thresh, thresh, dilateElement);
}
void trackFilteredObject(Mat threshold, Mat HSV, Mat &cameraFeed){
vector <Colour> greenP;
bool highest = false;
Mat temp;
threshold.copyTo(temp);
//these two vectors needed for output of findContours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//use moments method to find our filtered object
double refArea = 0;
bool objectFound = false;
if (hierarchy.size() > 0) {
int numObjects = hierarchy.size();
//if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
if (numObjects < MAX_NUM_OBJECTS){
//go through each element in our frame
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
Colour green;
Moments moment = moments((cv::Mat)contours[index]);
double area = moment.m00;
//if the area is less than 20 px by 20px then it is probably just noise
//if the area is the same as the 3/2 of the image size, probably just a bad filter
//we only want the object with the largest area so we safe a reference area each
//iteration and compare it to the area in the next iteration.
if (area > MIN_OBJECT_AREA){
green.setX(moment.m10 / area);
green.setY(moment.m01 / area);
greenP.push_back(green);
objectFound = true;
}
else objectFound = false;
}
//let user know you found an object
if (objectFound == true)
{
int leastY = greenP[0].getY();
//check if it is the highest object in our frame
for (int i = 0; i < greenP.size(); i++)
{
int currentY = greenP[i].getY();
if (currentY < leastY || greenP.size() == 1)
{
leastY = currentY;
}
}
//draw on our circle detecting the target as well as write it's properties to a file
for (int i = 0; i < greenP.size(); i++)
{
if (leastY == greenP[i].getY())
{
drawObject(greenP[i].getX(), greenP[i].getY(), cameraFeed, true);
writeToFile(greenP[i].getX(), greenP[i].getY(), greenP.size());
}
//draw object location on screen
else drawObject(greenP[i].getX(), greenP[i].getY(), cameraFeed, false);
}
}
}
else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", Point(0, 50), 1, 2, Scalar(0, 0, 255), 1);
}
}
int main(int argc, char* argv[])
{
//basic setup
bool calibrationMode = true;
Mat cameraFeed;
Mat threshold;
Mat HSV;
if (calibrationMode){
createTrackbars();
}
VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
while (1){
//grabs our frame from the webcam
capture.read(cameraFeed);
//the rest just creates windows for filtering and calls the functions above
cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
if (calibrationMode == true){
cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
inRange(HSV, Scalar(lowH, lowS, lowV), Scalar(highH, highS, highV), threshold);
morphOps(threshold);
imshow("Thresholded Image", threshold);
trackFilteredObject(threshold, HSV, cameraFeed);
}
imshow("Original Image", cameraFeed);
int c = cvWaitKey(10);
if ((char)c == 27) break;
waitKey(30);
}
cvDestroyAllWindows();
return 0;
}