Have you ever tried using OpenCV where a single frame of image is processed for different processes like template matching, blob analysis etc? Have you ever faced a problem where it takes a few seconds to update a new frame? If you are working on a problem that has any of the two above scenarios, the solution would be to use multiple threads in your code.
Why multiple threads?
Multiple threads allow you to process your image as individual processes. For e.g : Image capture and Image processing can be executed under different cores of your processor. This accelerates the performance of your system.
Problems related to multiple threads:
The main problem behind using multiple threads is that there may be variables that might be shared between threads. In the case of splitting the image capture and image processing, the captured frames are shared between two threads. This problem is overcome by using mutex locks or semaphores.
The concept of mutex locks and semaphores has been explained in simple English in this link. I created a header file using the information available from the link.
I faced another issue where my threads were two asynchronous processes. My image capture rate was 30 frames/sec but my image processing rate was 5 frames/sec. I had to use a circular buffer to save the remaining 25 images. The circular buffer examples have been very clearly explained in this link. I used the boost circular buffer libraries to store my captured frames.
I created a buffer size of 200 frames ( Just in case…. 😉 .
Even the simplest problem’s solution wouldn’t start working right away. I had issues related to the highgui windows stalling when I executed my code. I could capture the frames only for a few seconds and the windows would stall from updating the frames.
Earlier, the process flow was as shown in the figure below:
Stack overflow came to my rescue. I posted my question here.
So the person who answered my question asked me to do it like this:
The problem got resolved! Since the documentation was very scarce for multi-threaded opencv processing, I thought I must share my experience. I am also sharing the sample file and the additional header file required for trying this simple solution. You may download the files from here. Code header file
WordPress doesn’t allow .cpp files and .h files. I believe that my pdf files could be used a reference to write your own!