This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
162 lines (129 loc) · 2.56 KB
/
main.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
#include "CameraFactory.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <memory>
#include <ctime>
#include <thread>
#include <mutex>
#include <string>
#include <list>
using namespace std;
using namespace cv;
using namespace scs;
struct MatPair
{
Mat * img[2];
};
list<MatPair> qs;
mutex mutexQS;
mutex mutexFinish;
mutex mutexIsFinish;
bool finish = false;
bool isFinish = false;
void writeMatPair(MatPair imgs, int i)
{
static char buffer[255];
for(int k = 0; k < 2; k++)
{
sprintf(buffer, "image/%k/%06d.png", k, i);
imwrite(buffer, *imgs.img[k]);
}
}
void writeImages()
{
MatPair imgs;
int index = 0;
bool isSleep = false;
while(1)
{
{
unique_lock<mutex> lock(mutexQS);
unique_lock<mutex> lock2(mutexFinish);
if(finish && qs.empty()) break;
else if(!finish && qs.empty()) isSleep = true;;
else
{
imgs = qs.front();
qs.pop_front();
}
}
if(isSleep)
{
this_thread::sleep_for(chrono::microseconds(1000)); // 1ms
isSleep = false;
continue;
}
writeMatPair(imgs, index++);
delete[] imgs.img;
imgs.img[0] = imgs.img[1] = nullptr;
}
unique_lock<mutex> lock(mutexIsFinish);
isFinish = true;
}
int main()
{
unique_ptr<Camera> cam[2];
for(int i = 0; i < 2; i++)
{
cam[i] = unique_ptr<Camera>(CameraFactory::make(CameraFactory::BalserCamera));
if(!cam[i]->open(i))
{
cerr << "Camera " << i << " Failed." << endl;
return i+1;
}
namedWindow(string("Image ")+char(i+'0'), 0);
}
MatPair img;
char c = 0, buffer[256];
int i = 0;
system("mkdir image");
system("mkdir image/0");
system("mkdir image/1");
mutex mutexCapture;
int counter = 0;
thread(writeImages).detach();
thread th[2];
while (c != 27)
{
double t1 = clock();
counter = 2;
for(int k = 0; k < 2; k++)
th[k] = thread([&, k]()
{
img.img[k] = new Mat;
cam[k]->image(*img.img[k]);
unique_lock<mutex> lock(mutexCapture);
counter--;
});
for(int k = 0; k < 2; k++)
th[k].detach();
while(1)
{
this_thread::sleep_for(chrono::microseconds(1000)); // 1ms
unique_lock<mutex> lock(mutexCapture);
if (counter == 0) break;
}
{
unique_lock<mutex> mutexQS;
qs.push_back(img);
}
c = waitKey(2);
double t2 = clock();
#ifdef WIN32
cout << "Frame Cost = " << t2 - t1 << endl;
#else
cout << "Frame Cost = " << (t2 - t1) / CLOCKS_PER_SEC << endl;
#endif
}
{
unique_lock<mutex> lock(mutexFinish);
finish = true;
}
while(1)
{
this_thread::sleep_for(chrono::microseconds(1000)); // 1ms
unique_lock<mutex> lock(mutexIsFinish);
if (isFinish) break;
}
return 0;
}