-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (68 loc) · 2.42 KB
/
main.cpp
File metadata and controls
73 lines (68 loc) · 2.42 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
#include "circular_buffer.h"
#include <cstdio>
#include <algorithm>
#include <thread>
#include "opencv2/opencv.hpp"
void readbuff(circular_buffer<cv::Mat> /*std::queue<cv::Mat>*/& ioBuff, std::mutex& iLock)
{
int count = 200;
double time_execute = 0;
while (count > 0)
{
std::lock_guard<std::mutex> lock_read(iLock);
if (!ioBuff.empty())
{
auto start_time = std::chrono::high_resolution_clock::now();
cv::Mat frameRead = ioBuff.pop();
//cv::Mat frameRead = ioBuff.front();
//ioBuff.pop();
auto end_time = std::chrono::high_resolution_clock::now();
count--;
time_execute += std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
if (frameRead.empty())
{
printf("Buffer is empty %d \n", count);
continue;
}
cv::imshow("test2", frameRead);
cv::waitKey(100);
}
}
printf("Time of execution read buff %.4f count = %d\n", time_execute / 200.0, count);
}
void writebuff(circular_buffer<cv::Mat>/*std::queue<cv::Mat>*/& ioBuff, std::mutex& iLock)
{
int count = 200;
double time_execute = 0;
cv::VideoCapture cap(0);
while (count > 0 && cap.isOpened())
{
cv::Mat frameRead;
bool result = cap.read(frameRead);
if (result)
{
std::lock_guard<std::mutex> lock_read(iLock);
auto start_time = std::chrono::high_resolution_clock::now();
// if(ioBuff.size() > 10)
// ioBuff.pop();
ioBuff.push(frameRead);
auto end_time = std::chrono::high_resolution_clock::now();
count--;
time_execute += std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
}
// cv::imshow("test", frameRead);
// cv::waitKey(10);
}
printf("time of write %.4f count = %d \n", time_execute / 200.0, count);
}
int main(int argc, char* argv[])
{
circular_buffer<cv::Mat> buffer_custom(100);
std::queue<cv::Mat> buffer;
std::mutex lockMutex;
std::thread write_buf = std::thread(writebuff, std::ref(buffer_custom), std::ref(lockMutex));
std::thread read_buf = std::thread(readbuff, std::ref(buffer_custom), std::ref(lockMutex));
write_buf.detach();
read_buf.join();
return 0;
}