Ask Your Question

Revision history [back]

Hi How we can use posix threads in opencv program

Hi ,

I am new on opencv development . I want open more than 2 video files independently I mean by using posix threads . But when I am doing that my application is crashing , no imshow working . I posing my c code . please anybody can help for that . Thanks ......

Code snapshot :

#include "opencv2/highgui/highgui.hpp"
#include <sys/types.h>
#include <pthread.h>
#include <iostream>

using namespace cv;
using namespace std;


void * VideoCap(void *);


void * VideoCap(void *arg)
{
        char window_name[100]={0};

        VideoCapture cap((char *)arg); // open the video file for reading

        if ( !cap.isOpened() )  // if not success, exit program
        {
                 cout << "Cannot open the video file" << endl;
                 exit(1);
        }

    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

     cout << "Frame per seconds : " << fps << endl;

//     sprintf(window_name , "window_%s" ,(char *) arg);
     namedWindow(window_name,CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while(1)
    {
        Mat frame;
        static int count = 1;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                        cout << count << endl;
                       break;
        }
                        cout << "frame :" << bSuccess << (char *)arg << endl;
                        count++;

//        imshow(window_name, frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
       {
                cout << "esc key is pressed by user" << endl;
                break;
       }
    }

}


int main(int argc, char* argv[])
{

        int ret ;
        pthread_t th[2];

        ret = pthread_create(&th[0] , NULL , VideoCap , (void *)"cctv3.mp4");
        if(0 == ret)
        {
                cout << "Thread 1 is created successfull" << endl;
        }
        ret = pthread_create(&th[1] , NULL , VideoCap , (void *)"cctv10.mp4");
        if(0 == ret)
        {
                cout << "Thread 2 is created successfull" << endl;
        }
        pthread_join(th[0] , NULL);
        pthread_join(th[1] , NULL);

    return 0;
}
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
~