基于信号量的同步用于基于 opencv 的图像处理

发布于 2024-11-24 16:02:12 字数 1644 浏览 1 评论 0原文

我正在尝试使用信号量来同步 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-12-01 16:02:12

CaptureImageProcessing 正在不同的线程中运行,不是吗?我曾经尝试从不同的线程更新 HighGui 窗口,但没有成功。

这实际上是大多数(如果不是全部)窗口系统的问题:您无法从不同线程进行调用来更新窗口。

尝试将两个 cvShowImage 调用放在同一个线程中。对 cvWaitKey 的调用可能也必须在同一线程内进行。

可能的情况是,必须在使用 cvNamedWindow 初始化窗口的同一线程中调用 cvShowImage。不过我对此并不确定。

Capture and ImageProcessing 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 to cvWaitKey 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 using cvNamedWindow. I'm not sure about this, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文