[英]Trying to run CamShift using threads
I wrote code that creates 2 threads (I'm using glib). 我编写了创建2个线程的代码(我正在使用glib)。 The first thread runs a function called
Camera
that just starts capturing from the camera, and shows the captured frames on the screen. 第一个线程运行一个名为
Camera
的功能,该功能仅从Camera
开始捕获,并在屏幕上显示捕获的帧。 The second function is the algorithm CamShift
that uses the captured image from the first function to start running. 第二个函数是
CamShift
算法,该算法使用从第一个函数捕获的图像开始运行。 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. 我制作了第一个从相机捕获的功能,因为稍后我将添加更多的算法(例如
CamShift
,这些算法将从第一个功能访问捕获的图像。
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. 另一个功能是OpenCV随附的常规
CamShift
算法。 I just modified it to use the captured frames from the Camera
function. 我刚刚对其进行了修改,以使用“
Camera
功能捕获的帧。 That works fine, but the problem is, like I said before, the 2 functions start and then just pause. 效果很好,但是问题是,就像我之前说的那样,这两个函数先启动然后暂停。
Don't use sleep
to try to synchronize the threads. 不要使用
sleep
尝试同步线程。 You need to be protecting the frame
variable with either a GMutex , or a GStaticRWLock . 您需要使用GMutex或GStaticRWLock保护
frame
变量。 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. 您还可以使用GCond条件变量结构通知
CamShift
线程(在Camera
线程中)帧已准备就绪。 This will allow the CamShift
thread to block until a frame is captured in the Camera
thread. 这将允许
CamShift
线程阻塞,直到在Camera
线程中捕获到一帧为止。
Hope that is helpful! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.