尝试使用线程运行 CamShift

发布于 2024-12-18 04:32:01 字数 1964 浏览 3 评论 0 原文

我编写了创建 2 个线程的代码(我使用的是 glib)。第一个线程运行一个名为“Camera”的函数,该函数刚刚开始从相机捕获,并在屏幕上显示捕获的帧。第二个函数是算法 CamShift,它使用第一个函数捕获的图像开始运行。我创建了第一个从相机捕获的函数,因为稍后我将添加更多算法,例如 CamShift 来访问第一个函数的捕获。

我的问题是我希望这两个函数继续运行,直到我告诉它们停止。但我是新使用线程和我编写代码的方式,它可以很好地编译并运行两个函数,但它们只是在启动后立即“暂停”。下面是我的两个函数的代码。

//**********Sensa iluminacion (hilo)******************************************
GThread      *idGHilo,*idGHilo1, *idGHilo2, *idGHilo3, *idGHilo4;
GError       **error = NULL;
char *valorDevuelto = NULL;/* Valor que va a devolver el thread hijo */
if(!g_thread_supported()) // se inicializa el sistema de hilos (se emplea cuando  se
    g_thread_init( NULL );   // emplean más de un hilo


idGHilo1 = g_thread_create( (GThreadFunc) Camara, NULL, TRUE, error );//esto lo  cambie ayer 23
/* Comprobamos el error al arrancar el thread */
if(error) {
    g_print( "Error: %s\n", error[0]->message );
    g_error_free( error[0] );
    //exit (-1);
}
sleep( 10 );          // se da un retardo para dar tiempo a que termine el hilo
idGHilo2 = g_thread_create( (GThreadFunc) CamShift2, NULL, FALSE, error );
if(error) {
    g_print( "Error: %s\n", error[0]->message );
    g_error_free( error[0] );
    //exit (-1);
}
sleep( 10 ); //10...5
g_thread_join( idGHilo1 );
g_thread_join( idGHilo2 );


//****************************
// This is the camera function
void Camara() {

    capture = cvCaptureFromCAM( 0 );
    while( stop != 's' ) {

        // get a frame 
        frame = cvQueryFrame( capture );

        // always check 
        if( !frame ) break;

        // 'fix' frame 
        cvFlip( frame, frame, 2 );
        frame->origin = 0;

        cvNamedWindow( "Camara", CV_WINDOW_AUTOSIZE );
        cvShowImage( "Camara", frame );

        // quit if user press 'q' 

        stop = cvWaitKey( 10 );
    }
}

另一个函数是 OpenCV 附带的常规 CamShift 算法。我刚刚修改它以使用从Camera 函数捕获的帧。这工作正常,但问题是,就像我之前说的,这两个函数启动然后暂停。

I wrote code that creates 2 threads (I'm using glib). The first thread runs a function called Camera that just starts capturing from the camera, and shows the captured frames on the screen. The second function is the algorithm CamShift that uses the captured image from the first function to start running. I made the first function to capture from the camera because later I will add more algorithms like CamShift that will access the captures from the first function.

My problem is that I want these 2 functions to continue running until I tell them to stop. But I'm new using threads and the way I wrote the code it compiles fine and runs the 2 functions, but they just "pause" immediately after they start. Below is the code of my 2 functions.

//**********Sensa iluminacion (hilo)******************************************
GThread      *idGHilo,*idGHilo1, *idGHilo2, *idGHilo3, *idGHilo4;
GError       **error = NULL;
char *valorDevuelto = NULL;/* Valor que va a devolver el thread hijo */
if(!g_thread_supported()) // se inicializa el sistema de hilos (se emplea cuando  se
    g_thread_init( NULL );   // emplean más de un hilo


idGHilo1 = g_thread_create( (GThreadFunc) Camara, NULL, TRUE, error );//esto lo  cambie ayer 23
/* Comprobamos el error al arrancar el thread */
if(error) {
    g_print( "Error: %s\n", error[0]->message );
    g_error_free( error[0] );
    //exit (-1);
}
sleep( 10 );          // se da un retardo para dar tiempo a que termine el hilo
idGHilo2 = g_thread_create( (GThreadFunc) CamShift2, NULL, FALSE, error );
if(error) {
    g_print( "Error: %s\n", error[0]->message );
    g_error_free( error[0] );
    //exit (-1);
}
sleep( 10 ); //10...5
g_thread_join( idGHilo1 );
g_thread_join( idGHilo2 );


//****************************
// This is the camera function
void Camara() {

    capture = cvCaptureFromCAM( 0 );
    while( stop != 's' ) {

        // get a frame 
        frame = cvQueryFrame( capture );

        // always check 
        if( !frame ) break;

        // 'fix' frame 
        cvFlip( frame, frame, 2 );
        frame->origin = 0;

        cvNamedWindow( "Camara", CV_WINDOW_AUTOSIZE );
        cvShowImage( "Camara", frame );

        // quit if user press 'q' 

        stop = cvWaitKey( 10 );
    }
}

The other function is the regular CamShift algorithm that comes with OpenCV. I just modified it to use the captured frames from the Camera function. That works fine, but the problem is, like I said before, the 2 functions start and then just pause.

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

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

发布评论

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

评论(1

情定在深秋 2024-12-25 04:32:01

不要使用 sleep 尝试同步线程。您需要使用 frame 变量>GMutex,或 GStaticRWLock。这将防止该共享资源出现竞争状况。您还可以使用 CamShift 线程帧已准备就绪(在 Camera 线程中) /2.30/glib-Threads.html#GCond" rel="nofollow">GCond 条件变量结构。这将允许 CamShift 线程阻塞,直到在 Camera 线程中捕获帧为止。

希望这有帮助!

Don't use sleep to try to synchronize the threads. You need to be protecting the frame variable with either a GMutex, or a GStaticRWLock. This will prevent race conditions from occurring with that shared resource. You could also notify the CamShift thread that a frame is ready (in the Camera thread) using the GCond condition variable structure. This will allow the CamShift thread to block until a frame is captured in the Camera thread.

Hope that is helpful!

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