<Mat> 的循环缓冲区比应有的少
Circular buffer of <Mat> holds less than it should
我正在处理视频流,在 C++ 代码中使用 Mat
的增强循环缓冲区。一个线程推送 Mat
个对象:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
pthread_mutex_lock(&mtx);
cb.push_back(frame);
pthread_mutex_unlock(&mtx);
}
}
其他线程处理并弹出:
void* process(void* arg){
while (1){
Mat frame;
pthread_mutex_lock(&mtx);
frame =cb.front();
cb.pop_front();
pthread_mutex_unlock(&mtx);
scan(frame);
}
}
但是,即使缓冲区已满,与视频流相比也没有延迟。我检查了缓冲区中的第一张(正面)图像 cb[1]
和最后一张(背面)图像,当缓冲区已满 cb[1999]
并且它们都是相同的,也与最后捕获的图像相同流。
就好像缓冲区只保存最后捕获的图像并为任何调用的插槽检索它。知道为什么缓冲区没有存储它应该存储的所有图像吗?
感谢 Sean Cline 我能够解决这个问题。
我不得不更改我的捕获线程:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
pthread_mutex_lock(&mtx);
cb.push_back(frame);
pthread_mutex_unlock(&mtx);
}
}
至:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
Mat image;
frame.copyTo(image);
pthread_mutex_lock(&mtx);
cb.push_back(image);
pthread_mutex_unlock(&mtx);
}
}
这样压入缓冲区的Mat
对象在同一个while
循环中声明。我用了copyTo()
,因为在while
循环的条件中使用了Mat
帧。这使得 Mat image
的所有实例都有自己的缓冲区,而不是共享一个缓冲区。
我正在处理视频流,在 C++ 代码中使用 Mat
的增强循环缓冲区。一个线程推送 Mat
个对象:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
pthread_mutex_lock(&mtx);
cb.push_back(frame);
pthread_mutex_unlock(&mtx);
}
}
其他线程处理并弹出:
void* process(void* arg){
while (1){
Mat frame;
pthread_mutex_lock(&mtx);
frame =cb.front();
cb.pop_front();
pthread_mutex_unlock(&mtx);
scan(frame);
}
}
但是,即使缓冲区已满,与视频流相比也没有延迟。我检查了缓冲区中的第一张(正面)图像 cb[1]
和最后一张(背面)图像,当缓冲区已满 cb[1999]
并且它们都是相同的,也与最后捕获的图像相同流。
就好像缓冲区只保存最后捕获的图像并为任何调用的插槽检索它。知道为什么缓冲区没有存储它应该存储的所有图像吗?
感谢 Sean Cline 我能够解决这个问题。 我不得不更改我的捕获线程:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
pthread_mutex_lock(&mtx);
cb.push_back(frame);
pthread_mutex_unlock(&mtx);
}
}
至:
void* capture(void* arg){
boost::circular_buffer<Mat> cb(2000);
Mat frame;
while ((VideoCapture >> frame), !frame.empty()) {
Mat image;
frame.copyTo(image);
pthread_mutex_lock(&mtx);
cb.push_back(image);
pthread_mutex_unlock(&mtx);
}
}
这样压入缓冲区的Mat
对象在同一个while
循环中声明。我用了copyTo()
,因为在while
循环的条件中使用了Mat
帧。这使得 Mat image
的所有实例都有自己的缓冲区,而不是共享一个缓冲区。