openCV和线程问题 [英] openCV and Threads issue

查看:281
本文介绍了openCV和线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的QT代码中安装来自openCV的facedetect,在我决定为自己的openCV代码创建线程之前,一切运行良好,以便我可以在启用面部检测的同时运行其他事情.

I'm trying to fit facedetect from openCV on my QT code, all run fine until i decided to create a thread for my openCV code so I can run another things while the face detect is on.

问题是如果我调用class-> start();我的程序在run()中的while循环中中断,但是如果我调用class.run(); (像正常功能一样)照常运行!有什么问题吗?

the problem is if i call class->start(); my program breaks in the while loop in the run() but if i call the class.run(); (like a normal function) it runs as usual! what can be wrong?

代码:

faceTracker::faceTracker()
{ 

qDebug("teste1");
filename = "/Users/marcomartins/Documents/QT/DisplUM/haarcascades/haarcascade_frontalface_alt_tree.xml";

/* load the classifier
    note that I put the file in the same directory with this code */
cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );

/* setup memory buffer; needed by the face detector */
storage = cvCreateMemStorage( 0 );

/* initialize camera */
capture = cvCaptureFromCAM( 0 );

/* always check */
assert( cascade && storage && capture );

/* create a window */
cvNamedWindow( "video DisplUM", 1 );


}

void faceTracker::detectFaces( IplImage *img )
{

/* detect faces */
        faces = cvHaarDetectObjects(
        img,
        cascade,
        storage,
        1.1,
        3,
        0 /*CV_HAAR_DO_CANNY_PRUNNING*/,
        cvSize( 40, 40 ) );

/* for each face found, draw a red box */
for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
    CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
    cvRectangle( img,
                 cvPoint( r->x, r->y ),
                 cvPoint( r->x + r->width, r->y + r->height ),
                 CV_RGB( 255, 0, 0 ), 1, 8, 0 );
    qDebug("caras: %d", faces->total);
}

/* display video */
cvShowImage( "video", img );
}


void faceTracker::run( )
{
qDebug("teste2");

while( key != 'q' ) {
    /* get a frame */
    frame = cvQueryFrame( capture );
qDebug("teste3");
    /* always check */
    if( !frame ) break;

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

    /* detect faces and display video */
    detectFaces( frame );

    /* quit if user press 'q' */
    key = cvWaitKey( 10 );

}

/* free memory */
cvReleaseCapture( &capture );
cvDestroyWindow( "video" );
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
}

主要代码:

int main(int argc, char *argv[])
{
  faceTracker * ft = new faceTracker();
  ft->start();
}

非常感谢!

推荐答案

解决方案: 我无法在主线程之外创建窗口,这就是它崩溃的原因.如果我评论该窗口的创建一切正常(包括面部检测)

Solution: I can't create windows outside the main thread, that is why it was crashing. If i comment the window creation all works good (face detection included)

这篇关于openCV和线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆