1

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

nanz
  • 11
  • 3

1 Answers1

0

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.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • the problem is that cvshowimage needs cvWaitKey(X) and then if I show the images in the main thread I loose frames because X makes me loose ms (where a frame could arrive) – nanz Apr 11 '11 at 10:46
  • it's thousand of lines the whole code, I'm going to post a case base code to explain better ok – nanz Apr 11 '11 at 10:51
  • @nanz yes sure, as long as it shows what the problem is you're having – Tony The Lion Apr 11 '11 at 10:52