【发布时间】:2011-04-12 17:01:03
【问题描述】:
我正在尝试在我的 QT 代码上安装来自 openCV 的 facedetect,一切运行良好,直到我决定为我的 openCV 代码创建一个线程,这样我就可以在面部检测开启时运行其他东西。
问题是如果我调用 class->start();我的程序在 run() 的 while 循环中中断,但如果我调用 class.run(); (就像一个正常的功能)它像往常一样运行!有什么问题?
代码:
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();
}
非常感谢!
【问题讨论】:
-
class->start() 在哪里以及是什么?如果您需要更多帮助,则需要共享更多代码。如果没有更多信息,我们无法判断出了什么问题。
-
“我的程序在 while 循环中中断”是什么意思?
-
代码已编辑以包含主要内容。关于 while 循环,我的意思是 qDebug("teste3");不显示所以问题出在 frame = cvQueryFrame( capture );这是我的想法
标签: c++ multithreading opencv