-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
222 lines (218 loc) · 8.26 KB
/
main.cpp
File metadata and controls
222 lines (218 loc) · 8.26 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
//Main Program
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
#include "marker.h"
#include "GraphUtils.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <time.h>
//#include <Gdiplus.h>
//using namespace cv;
using namespace std;
//initial min and max HSV filter values.
//these will be changed using trackbars
int H_MIN = 0;
int H_MAX = 256;
int S_MIN = 0;
int S_MAX = 256;
int V_MIN = 0;
int V_MAX = 256;
//default capture width and height
const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
const int ACTUAL_WIDTH = 780;
const int ACTUAL_HEIGHT = 585;
//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 = 10 * 10;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;
//names that will appear at the top of each window
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";
void on_trackbar(int, void*)
{//This function gets called whenever a
// trackbar position is changed
}
string intToString(int number){
std::stringstream ss;
ss << number;
return ss.str();
}
void createTrackbars(){
//create window for trackbars
cv::namedWindow(trackbarWindowName, 0);
//create memory to store trackbar name on window
char TrackbarName[50];
sprintf(TrackbarName, "H_MIN", H_MIN);
sprintf(TrackbarName, "H_MAX", H_MAX);
sprintf(TrackbarName, "S_MIN", S_MIN);
sprintf(TrackbarName, "S_MAX", S_MAX);
sprintf(TrackbarName, "V_MIN", V_MIN);
sprintf(TrackbarName, "V_MAX", V_MAX);
//create trackbars and insert them into window
//3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
//the max value the trackbar can move (eg. H_HIGH),
//and the function that is called whenever the trackbar is moved(eg. on_trackbar)
cv::createTrackbar("H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar);
cv::createTrackbar("H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar);
cv::createTrackbar("S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar);
cv::createTrackbar("S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar);
cv::createTrackbar("V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar);
cv::createTrackbar("V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar);
}
void drawObject(vector <Marker> theYellow, cv::Mat &frame, int(&x)[16], int(&y)[16]){
for (int i = 0; i < theYellow.size(); i++){
if (i < 16){
x[i] = theYellow.at(i).getXpos() * ACTUAL_WIDTH / FRAME_WIDTH;
y[i] = theYellow.at(i).getYpos() * ACTUAL_HEIGHT / FRAME_HEIGHT;
}
cv::circle(frame, cv::Point(theYellow.at(i).getXpos(), theYellow.at(i).getYpos()), 10, cv::Scalar(255, 0, 0));
cv::putText(frame, intToString(theYellow.at(i).getXpos() * ACTUAL_WIDTH / FRAME_WIDTH) + "," + intToString(theYellow.at(i).getYpos()* ACTUAL_HEIGHT / FRAME_HEIGHT), cv::Point(theYellow.at(i).getXpos(), theYellow.at(i).getYpos() + 20), cv::FONT_HERSHEY_PLAIN, 0.8, cv::Scalar(0, 255, 0));
//cv::putText(frame, theYellow.at(i).getType(), cv::Point(theYellow.at(i).getXpos(), theYellow.at(i).getYpos() - 30), 1, 2, theYellow.at(i).getColour());
}
}
void morphOps(cv::Mat &thresh){
//create structuring element that will be used to "dilate" and "erode" image.
//the element chosen here is a 3px by 3px rectangle
cv::Mat erodeElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
//dilate with larger element so make sure object is nicely visible
cv::Mat dilateElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(8, 8));
erode(thresh, thresh, erodeElement);
erode(thresh, thresh, erodeElement);
dilate(thresh, thresh, dilateElement);
dilate(thresh, thresh, dilateElement);
}
void trackFilteredObject(Marker theYellow, cv::Mat threshold, cv::Mat HSV, cv::Mat &cameraFeed, int(&x)[16], int(&y)[16]){
vector <Marker> yellows;
cv::Mat temp;
threshold.copyTo(temp);
//these two vectors needed for output of findContours
vector< vector<cv::Point> > contours;
vector<cv::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){
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
cv::Moments moment = moments((cv::Mat)contours[index]);
double area = moment.m00;
//if the area is less than 10 px by 10px 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){
Marker yellow;
yellow.setXpos(moment.m10 / area);
yellow.setYpos(moment.m01 / area);
yellow.setType(theYellow.getType());
yellow.setColour(theYellow.getColour());
yellows.push_back(yellow);
objectFound = true;
}
else objectFound = false;
}
//let user know you found an object
if (objectFound == true){
//draw object location on screen
drawObject(yellows, cameraFeed, x, y);
}
else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", cv::Point(0, 50), 1, 2, cv::Scalar(0, 0, 255), 2);
}
}
}
int main(int argc, char* argv[]){
//if we would like to calibrate our filter values, set to true.
bool calibrationMode = false;
//Matrix to store each frame of the webcam feed
cv::Mat cameraFeed;
cv::Mat threshold;
cv::Mat HSV;
if (calibrationMode){
//create slider bars for HSV filtering
createTrackbars();
}
//video capture object to acquire webcam feed
//open capture object at location zero (default location for webcam)
// if (argc<2)
// {
// cv::VideoCapture cap(0);
// }
// else
// {
cv::VideoCapture cap("test_01.avi");
// }
// if not success, exit program
if (!cap.isOpened()){
cout << "Cannot open the video file" << endl;
return -1;
}
//set height and width of capture frame
cap.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
double fps = 20;
int x[16];
int y[16];
int sec = 0;
cout << "Frame per seconds : " << fps << endl;
//create a window called "test_01"
cv::namedWindow("test_08", CV_WINDOW_AUTOSIZE);
//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
bool second = true;
ofstream myfile("test_08.xls");
myfile.is_open();
clock_t startTime = clock();
while (1){
//store image to matrix
cap.read(cameraFeed);
if (cameraFeed.empty())
{
cout<<"End of stream"<<endl;
break;
}
//convert frame from BGR to HSV colorspace
cvtColor(cameraFeed, HSV, cv::COLOR_BGR2HSV);
Marker yellow("yellow");
if (calibrationMode == true){
//if in calibration mode, we track objects based on the HSV slider values. Filter “HSV Noise”
cvtColor(cameraFeed, HSV, cv::COLOR_BGR2HSV);
inRange(HSV, cv::Scalar(H_MIN, S_MIN, V_MIN), cv::Scalar(H_MAX, S_MAX, V_MAX), threshold);
//inRange(HSV, yellow.getHSVmin(), yellow.getHSVmax(), threshold);
morphOps(threshold);
imshow(windowName2, threshold);
trackFilteredObject(yellow, threshold, HSV, cameraFeed, x, y);
}
else{
cvtColor(cameraFeed, HSV, cv::COLOR_BGR2HSV);
inRange(HSV, yellow.getHSVmin(), yellow.getHSVmax(), threshold);
morphOps(threshold);
imshow(windowName2, threshold);
trackFilteredObject(yellow, threshold, HSV, cameraFeed, x, y);
}
imshow(windowName, cameraFeed);
cout << clock() << endl;
if (clock() - startTime > 980){
sec = sec + 1;
for (int i = 0; i < 8; i++) myfile << '\n' << sec << '\t' << x[i] << '\t' << y[i] << '\t' << x[i + 8] << '\t' << y[i + 8];
startTime = clock();
}
//wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
if (cv::waitKey(30) == 27) {
cout << "esc key is pressed by user" << endl;
break;
}
}
//closes the textfile
myfile.close();
return 0; }