title: OpenCV: processing multiple images in a C++ vector using pthreads
body: <p>I have a large number images in a file that I need to perform various processing operations on. Here is what I am trying to do</p>
  
  <p>1) Read the images into a file, and put them in a C++ vector named imageQueue (a mutable array)</p>
  
  <p>2) Create a number of threads</p>
  
  <p>3) Each thread grabs an image from imageQueue, and then erases that image from the vector</p>
  
  <p>4) Each thread then goes ahead and processes that image</p>
  
  <p>5) When finished processing, each thread grabs the next image from the vector</p>
  
  <p>6) This entire process runs until there are no more images in the imageQueue, at which point the program ends. (currently I have 4 photos in the file that I am using for tests, which is why in my loops I run from i = 0 to i &lt; 4. When I complete this, I will have many more photos. </p>
  
  <p>I have named each of the images in the file 00.jpg, 01.jpg, 02.jpg....</p>
  
  <p>For testing purposes, right now I am simply trying to have each thread display the image it grabbed. However, when I run this only purely white windows pop up, instead of the actual image. Any help on why this is happening and how to correct it? 
  Thanks! </p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;pthread.h&gt;
  #include &lt;vector.h&gt;
  
  #define NUM_THREADS 2
  
  using namespace std;
  using namespace cv;
  
  /* Function Declarations */
  void* startProcessing(void* args);
  
  /* Global Variables */
  vector &lt;Mat&gt; imageQueue;
  
  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  
  int main(int argc, char** argv)
  {
    VideoCapture cap;
    cap.open("/Users/nlauer/Documents/ImageSequence/%02d.jpg");
    for(int i = 0; i &lt; 4; i++)
      {
        Mat frame;
        vector&lt;Mat&gt;::iterator it;
        cap &gt;&gt; frame;
        it = imageQueue.end();
        it = imageQueue.insert(it, frame);
      }
  
   /* Create the threads */
   pthread_t tids[NUM_THREADS];
   for(int i = 0; i &lt; NUM_THREADS; i++)
     {
       pthread_create(&amp;tids[i], NULL, startProcessing, NULL);
     }
  
   /* Reap the threads */
   for(int i = 0; i &lt; NUM_THREADS; i++)
     {
       pthread_join(tids[i], NULL);
     }
   imageQueue.clear();
   return 0;
  }
  
  void* startProcessing(void* args)
  {
     /* Each thread grabs an image from imageQueue, removes it from the                                                
        queue, and then processes it. The grabbing and removing are done                                               
        under a lock */
    Mat image;
    Mat emptyImage;
    /* Get the first image for each thread */
    pthread_mutex_lock(&amp;mutex);
    if(!imageQueue.empty()) {
       image = imageQueue[0];
       vector&lt;Mat&gt;::iterator it;
       it = imageQueue.begin();
       it = imageQueue.erase(it);
    }
    pthread_mutex_unlock(&amp;mutex);
  
    while(!image.empty())
      {
        /* Process the image - right now I just want to display it */
        namedWindow("window", CV_WINDOW_AUTOSIZE);
        imshow("window", image);
        sleep(10);
  
        /* Obtain the next image in the queue */
        pthread_mutex_lock(&amp;mutex);
        if(!imageQueue.empty()) {
          image = imageQueue[0];
          vector&lt;Mat&gt;::iterator it;
          it = imageQueue.begin();
          it = imageQueue.erase(it);
        } else {
          image = emptyImage;
        }
        pthread_mutex_unlock(&amp;mutex);
      }
  
     return NULL;
  }
  </code></pre>