-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
113 lines (94 loc) · 2.48 KB
/
window.cpp
File metadata and controls
113 lines (94 loc) · 2.48 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
#include <iostream>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "coloredobject.hpp"
using namespace std;
using namespace cv;
vector<ColoredObject> objects;
int pendingX = -1;
int pendingY = -1;
int pendingID = 0;
boost::posix_time::ptime t;
void mouseCallback(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
pendingX = x;
pendingY = y;
}
}
int main(int argc, const char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open the camera" << endl;
return -1;
}
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;
ofstream file;
file.open("cam_size");
file << dWidth << " " << dHeight << endl;
file.close();
namedWindow("Camera", CV_WINDOW_AUTOSIZE);
setMouseCallback("Camera", mouseCallback, NULL);
Mat frameBGR, frameHSV;
t = boost::posix_time::microsec_clock::local_time();
while (true)
{
bool bSuccess = cap.read(frameBGR);
if (!bSuccess)
{
cout << "Cannot read from the video stream" << endl;
break;
}
GaussianBlur(frameBGR, frameBGR, Size(3, 3), 0, 0);
cvtColor(frameBGR, frameHSV, CV_BGR2HSV);
for (int i=0; i<objects.size(); i++)
{
if (objects.at(i).tick())
{
rectangle(frameBGR, objects.at(i).getBoundingRect(), Scalar(0, 0, 255));
//objects.at(i).print();
}
}
boost::posix_time::ptime ct = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration diff = ct - t;
long delta = diff.total_milliseconds();
double fps = 1000.0 / delta;
string message = to_string(fps) + " fps";
putText(frameBGR, message, Point(35, 35), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255, 0, 0));
t = ct;
imshow("Camera", frameBGR);
if (pendingX != -1)
{
Vec3b colorBGR = (frameBGR).at<Vec3b>(pendingX, pendingY);
Vec3b colorHSV = (frameHSV).at<Vec3b>(pendingX, pendingY);
objects.push_back(ColoredObject(pendingID, pendingX, pendingY, &frameHSV));
pendingID++;
pendingX = -1;
pendingY = -1;
}
int x = waitKey(1);
if (x == 27)
{
cout << "esc key is pressed" << endl;
break;
}
else if (char(x) == 'p')
{
printf("----Printing all objects----\n");
for (int i=0; i<objects.size(); i++)
objects.at(i).print();
}
}
for (int i=0; i<objects.size(); i++)
{
objects.at(i).terminate();
}
system("rm cam_size");
return 0;
}