-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
316 lines (265 loc) · 8.41 KB
/
functions.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
#include "cvHeader.hpp"
/* ------ Blur an image ----- */
void blur_image(string imgName)
{
cout << "Blurring the image " << imgName << endl;
Mat src, dst;
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";
src = imread(imgName, 1);
namedWindow(window_name1, WINDOW_AUTOSIZE);
imshow("Unprocessed Image", src);
dst = src.clone();
GaussianBlur(src,dst,Size(15,15),0,0);
namedWindow(window_name2, WINDOW_AUTOSIZE);
imshow("Processed Image", dst);
waitKey();
}
/* ----- Canny edge detection on camera ----- */
void canny_video()
{
cout << "Canny Edge Detector from Camera" << endl;
VideoCapture cap(0);
if (!cap.isOpened())
return;
Mat edges;
namedWindow("edges", 1);
while (true) {
Mat frame;
cap >> frame;
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if (waitKey(30) >= 0) break;
}
}
/* ----- Display an image ----- */
void display_image(string imgName)
{
cout << "Displaying image " << imgName << endl;
IplImage* img = cvLoadImage(imgName.c_str());
cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
cvShowImage("Image", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Image");
}
/* ----- Lucas-Kanade ----- */
void lucas_kanade(string iname1, string iname2)
{
Mat imgA = imread(iname1, CV_LOAD_IMAGE_GRAYSCALE);
Mat imgB = imread(iname2, CV_LOAD_IMAGE_GRAYSCALE);
Size img_sz = imgA.size();
Mat imgC(img_sz,1);
int win_size = 15;
int maxCorners = 20;
double qualityLevel = 0.05;
double minDistance = 5.0;
int blockSize = 3;
double d = 0.04;
std::vector<cv::Point2f> cornersA;
cornersA.reserve(maxCorners);
std::vector<cv::Point2f> cornersB;
cornersB.reserve(maxCorners);
goodFeaturesToTrack(imgA, cornersA, maxCorners, qualityLevel, minDistance, cv::Mat());
goodFeaturesToTrack(imgB, cornersB, maxCorners, qualityLevel, minDistance, cv::Mat());
cornerSubPix(imgA, cornersA, Size(win_size, win_size), Size(-1, -1),
TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));
cornerSubPix(imgB, cornersB, Size(win_size, win_size), Size(-1, -1),
TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));
CvSize pyr_sz = Size(img_sz.width+8, img_sz.height/3);
std::vector<uchar> features_found;
features_found.reserve(maxCorners);
std::vector<float> feature_errors;
feature_errors.reserve(maxCorners);
calcOpticalFlowPyrLK(imgA, imgB, cornersA, cornersB, features_found, feature_errors,
Size(win_size, win_size), 5,
cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03), 0);
for(int i = 0 ; i < features_found.size() ; i++)
{
cout << "Error is " << feature_errors[i] << endl;
cout << "Got it" << endl;
Point p0(ceil(cornersA[i].x), ceil(cornersA[i].y));
Point p1(ceil(cornersB[i].x), ceil(cornersB[i].y));
line(imgC, p0, p1, CV_RGB(255,255,255), 2);
}
namedWindow("Image1", 0);
namedWindow("Image2", 0);
namedWindow("LK", 0);
imshow("Image1", imgA);
imshow("Image2", imgB);
imshow("LK", imgC);
cvWaitKey(0);
return;
}
/* ----- Convert an integer to a string ----- */
string intToString(int number)
{
std::stringstream ss;
ss << number;
return ss.str();
}
/* ----- Search for movement in a video ----- */
void searchForMovement(Mat threshImage, Mat &cameraFeed)
{
bool ObjectDetected = false;
Mat temp;
int theObject[2] = {0,0};
Rect objectBoundingRectangle = Rect(0,0,0,0);
threshImage.copyTo(temp);
vector< vector< Point> > contours;
vector< Vec4i> hierarchy;
findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if (contours.size() > 0) ObjectDetected = true;
else ObjectDetected = false;
if (ObjectDetected) {
vector< vector< Point> > largestContourVec;
largestContourVec.push_back(contours.at(contours.size()-1));
objectBoundingRectangle = boundingRect(largestContourVec.at(0));
int xpos = objectBoundingRectangle.x + objectBoundingRectangle.width/2;
int ypos = objectBoundingRectangle.y + objectBoundingRectangle.height/2;
theObject[0] = xpos , theObject[1] = ypos;
}
int x = theObject[0];
int y = theObject[1];
circle(cameraFeed, Point(x,y), 20, Scalar(0,255,0), 2);
line(cameraFeed, Point(x,y), Point(x,y-25), Scalar(0,255,0), 2);
line(cameraFeed, Point(x,y), Point(x,y+25), Scalar(0,255,0), 2);
line(cameraFeed, Point(x,y), Point(x-25,y), Scalar(0,255,0), 2);
line(cameraFeed, Point(x,y), Point(x+25,y), Scalar(0,255,0), 2);
putText(cameraFeed,"Tracking object at (" +
intToString(x)+","+intToString(y)+")", Point(x,y), 1, 1, Scalar(255,0,0), 2);
}
/* ----- Track an object in a video ----- */
void object_tracking(string vidName, int sens)
{
cout << "Detecting a moving object in " << vidName << " with " << sens << " sensitivity value" << endl;
bool ObjectDetected = false, debugMode = false, trackingEnabled = true, pause = false;
Mat frame1, frame2;
Mat grayImage1, grayImage2;
Mat differenceImage, threshImage;
VideoCapture capture;
const static int BLUR_SIZE = 10;
while (true) {
capture.open(vidName);
if (!capture.isOpened()) {
cout << "Error acquiring video feed\n";
getchar(); return;
}
while (capture.get(CV_CAP_PROP_POS_FRAMES) < capture.get(CV_CAP_PROP_FRAME_COUNT)-1) {
capture.read(frame1); capture.read(frame2);
cvtColor(frame1, grayImage1, COLOR_BGR2GRAY);
cvtColor(frame2, grayImage2, COLOR_BGR2GRAY);
absdiff(grayImage1, grayImage2, differenceImage);
threshold(differenceImage, threshImage, sens, 255, THRESH_BINARY);
if (debugMode == true) {
imshow("Difference Image", differenceImage);
imshow("Threshold Image", threshImage);
} else {
destroyWindow("Difference Image");
destroyWindow("Threshold Image");
}
blur(threshImage, threshImage, Size(BLUR_SIZE,BLUR_SIZE));
threshold(threshImage, threshImage, sens, 255, THRESH_BINARY);
if (debugMode == true)
imshow("Final Threshold Image", threshImage);
else
destroyWindow("Final Threshold Image");
if (trackingEnabled)
searchForMovement(threshImage, frame1);
imshow("Frame1", frame1);
switch (waitKey(10))
{
case 27:
return;
case 116:
trackingEnabled = !trackingEnabled;
if (trackingEnabled == false) cout << "Tracking disabled." << endl;
else cout << "Tracking enabled." << endl;
break;
case 100:
debugMode = !debugMode;
if (debugMode == false) cout << "Debug mode disabled." << endl;
else cout << "Debug mode enabled." << endl;
break;
case 112:
pause = !pause;
if (pause == true) { cout << "Code paused, press 'p' again to resume." << endl;
while (pause == true)
{
switch (waitKey())
{
case 112:
pause = false;
cout << "Code resumed." << endl;
break;
}
}
}
}
}
capture.release();
}
}
/* ----- Print all the options available ----- */
void print_options()
{
cout << "Options (command line arguments):" << endl;
cout << "\tblur - Blur image (image)" << endl;
cout << "\tcannyVideo - Canny edge detector from camera" << endl;
cout << "\tdisplayImage - Dispaly image (image)" << endl;
cout << "\tobjectDetection - Detect moving object in video (video, threshold)" << endl;
cout << "\tvideoCapture - Capture video from camera" << endl;
cout << "\tvideoFromFile - Display video from file (video)" << endl;
cout << endl;
}
/* ----- Capture video from camera ----- */
void video_capture()
{
cout << "Capturing video" << endl;
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open the video cam" << endl;
return;
}
double dWidth = cap.get (CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get (CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame size: " << dWidth << " x " << dHeight << endl;
namedWindow ("MyVideo", CV_WINDOW_AUTOSIZE);
while (true)
{
Mat frame;
bool bSuccess = cap.read (frame);
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow ("MyVideo", frame);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
}
/* ----- Capture video from a file ----- */
void video_from_file(string vidName)
{
cout << "Displaying the video from file " << vidName << endl;
cvNamedWindow ("Video", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture(vidName.c_str());
IplImage* frame;
while (true)
{
frame = cvQueryFrame (capture);
if (!frame) break;
cvShowImage ("Video", frame);
char c = cvWaitKey(33);
if (c == 27) break;
}
cvReleaseCapture (&capture);
cvDestroyWindow ("Video");
}