基于信号量的同步用于基于 opencv 的图像处理
我正在尝试使用信号量来同步 OpenCV 图像处理的两个线程。一个线程不断从网络摄像头捕获帧并将其推入循环缓冲区,另一个流从缓冲区提取图像并处理它们。我正在删除处理后使用的框架。
我正在使用 boost 循环缓冲区库来实现缓冲区。
请在下面找到确切的代码片段。我已经消除了大部分初始化并强调这是必要的。我面临的问题是我能够流式传输和捕获网络摄像头的帧。但有时窗口就会冻结。在某些情况下,捕获的帧无法正确显示。我可以知道我哪里出错了吗?
sem_t semaphore;
using namespace cvb;
using namespace std;
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
IplImage* img0;
IplImage* img1;
sem_t semaphore;
boost::circular_buffer<IplImage*> cb1(200);
int ret = sem_init(&semaphore, 1, 10); //Initializing the semaphore
void* Capture(void* arg){
while(1) {
sem_wait(&semaphore);
img0 = cvQueryFrame( capture );
cb1.push_back(img0);
cvShowImage( "mywindow", img0 );
sem_post(&semaphore);
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
}
void* ImageProcessing(void* arg) {
while(1) {
if(!cb1.empty()) {
sem_wait(&semaphore);
img2 = cvShowImage("result",img2);
cb1.pop_front();
sem_post(&semaphore);
}
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
cvReleaseCapture( &capture );
cvReleaseImage( &img2 );
}
I am trying to use semaphores to synchronize two threads of OpenCV image processes. One thread keeps capturing frames from the webcam and pushes it into a circular buffer and the other stream pulls an image from the buffer and processes them. I am deleting the frame that I use after I process them.
I am using boost circular buffer libraries to implement the buffer.
Please find the exact code snippet below. I have eliminated most part of the initialization and highlighting thats necessary. The problem that I am facing is that I am able to stream and capture my webcam's frames. But the window just freezes after sometimes. On some occasions, the captured frames are not displayed correctly. May I know where am I going wrong?
sem_t semaphore;
using namespace cvb;
using namespace std;
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
IplImage* img0;
IplImage* img1;
sem_t semaphore;
boost::circular_buffer<IplImage*> cb1(200);
int ret = sem_init(&semaphore, 1, 10); //Initializing the semaphore
void* Capture(void* arg){
while(1) {
sem_wait(&semaphore);
img0 = cvQueryFrame( capture );
cb1.push_back(img0);
cvShowImage( "mywindow", img0 );
sem_post(&semaphore);
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
}
void* ImageProcessing(void* arg) {
while(1) {
if(!cb1.empty()) {
sem_wait(&semaphore);
img2 = cvShowImage("result",img2);
cb1.pop_front();
sem_post(&semaphore);
}
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
cvReleaseCapture( &capture );
cvReleaseImage( &img2 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Capture
和ImageProcessing
正在不同的线程中运行,不是吗?我曾经尝试从不同的线程更新 HighGui 窗口,但没有成功。这实际上是大多数(如果不是全部)窗口系统的问题:您无法从不同线程进行调用来更新窗口。
尝试将两个
cvShowImage
调用放在同一个线程中。对cvWaitKey
的调用可能也必须在同一线程内进行。可能的情况是,必须在使用
cvNamedWindow
初始化窗口的同一线程中调用cvShowImage
。不过我对此并不确定。Capture
andImageProcessing
are being run in different threads, aren't they? I once tried to update HighGui windows from different threads, but it didn't work.This is actually a problem with most if not all windowing systems: you can't make calls from different threads to update a window.
Try putting both
cvShowImage
calls in the same thread. Calls tocvWaitKey
probably have to be made from within the same thread too.It may be the case that
cvShowImage
must be called in the same thread in which you initialize the windows usingcvNamedWindow
. I'm not sure about this, though.