opencv使用线程刷新问题

发布于 2024-10-31 16:32:21 字数 1487 浏览 0 评论 0原文

我使用两个线程:

线程 1 是从相机获取帧并处理它们的线程 线程 2 是使用 cvshowimage 显示它们的线程

在第二个线程中我使用 cvWaitKey(200); (我也尝试了其他值..)

问题是显示了第一个图像,但过了一会儿它们就不再显示了(当您尝试移动窗口时会发生相同的情况。 它冻结了,有时图像变成空白。

知道如何解决这个问题吗?

编辑: 当我在线程中显示图像时,我会丢失帧。应该是正常的吧?

编辑2: 我还尝试在线程中可视化 2 个较旧的帧而不是新的帧,但输出相同。Edit3

: 这就是我或多或少正在做的事情:

void *showImages( void *ptr )
{
  bool showit = false;
  while (!MainThreadHasFinished) 
  {

    pthread_mutex_lock( &mutex1 );
    if(ImageGenerated = true) 
       showit = true;
     else
       showit = false;
    pthread_mutex_unlock( &mutex1 );

    showit = true;

   if(showit == true)
     cvShowImage( "RGB Image", RGBImage); 

    cvWaitKey(500);
}}


IplImage *RGBImage;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
bool ImageGenerated;

int main(int argc, char** argv)
{
//init camera and other stuff
  int frameCounter=0;
  RGBImage = cvCreateImage( cvSize(RGB_RES_X,RGB_RES_Y),8,3); 
  int iret1 = pthread_create( &showImagesThread, NULL, showImages, (void*) message1);

   for (;;) {
    pthread_mutex_lock( &mutex1 );
    ImageGenerated = false;
    pthread_mutex_unlock( &mutex1 );

        //get frame here in showImg

    frameCounter++;
    if(frameCounter == 10) frameCounter=0;

    if(frameCounter == 2)
       cvCopy(&showImg,RGBImage);  
   pthread_mutex_lock( &mutex1 );
   ImageGenerated = true;
   pthread_mutex_unlock( &mutex1 );

//other stuff
}

}

干杯

I'm using two threads:

thread 1 is the the one that takes the frames from the camera and process them
thread 2 is the one that displays them using cvshowimage

In the second thread I'm using cvWaitKey(200); (i tried also other values..)

The problem is that the first images are showed but after a while they are not anymore (the same situation occurs when you try to move the window.
It freezes and sometimes the image becomes blank..

Any idea of how can I solve this problem?

Edit:
when I show images in the thread I loose frames. Should it be normal?

Edit2:
I tried also to visualize in the thread 2 older frames instead of the new one but same output..

Edit3:
This is what I'm doing more or less:

void *showImages( void *ptr )
{
  bool showit = false;
  while (!MainThreadHasFinished) 
  {

    pthread_mutex_lock( &mutex1 );
    if(ImageGenerated = true) 
       showit = true;
     else
       showit = false;
    pthread_mutex_unlock( &mutex1 );

    showit = true;

   if(showit == true)
     cvShowImage( "RGB Image", RGBImage); 

    cvWaitKey(500);
}}


IplImage *RGBImage;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
bool ImageGenerated;

int main(int argc, char** argv)
{
//init camera and other stuff
  int frameCounter=0;
  RGBImage = cvCreateImage( cvSize(RGB_RES_X,RGB_RES_Y),8,3); 
  int iret1 = pthread_create( &showImagesThread, NULL, showImages, (void*) message1);

   for (;;) {
    pthread_mutex_lock( &mutex1 );
    ImageGenerated = false;
    pthread_mutex_unlock( &mutex1 );

        //get frame here in showImg

    frameCounter++;
    if(frameCounter == 10) frameCounter=0;

    if(frameCounter == 2)
       cvCopy(&showImg,RGBImage);  
   pthread_mutex_lock( &mutex1 );
   ImageGenerated = true;
   pthread_mutex_unlock( &mutex1 );

//other stuff
}

}

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雅心素梦 2024-11-07 16:32:22

我不确定尝试从另一个线程在 GUI 上显示某些内容是一个好主意。除非我误解了你的意思,否则你应该直接从 GUI 线程调用 cvshowimage

I'm not sure that trying to show something on GUI from another thread is a very good idea. Unless I've misunderstood what you're saying, you should call cvshowimage from the GUI thread directly.

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