使用opencv在相机上线程face_detection

时间:2013-03-23 09:55:00

标签: c multithreading opencv

我正在训练在脸部,鼻子,眼睛检测上使用线程。因为,当我没有,相机工作得非常慢。我写了这段代码。我在代码中找不到错误。但是当我编译它时,在taskCollection选项卡上给出异常

pChore-> m_pFunction(pChore);错误。

    #include <iostream>
    #include "cv.h"
    #include "highgui.h"
    #include <pthread.h>
    #include <stdio.h>

    struct parameter_t{
        IplImage* capturedImg;
        CvHaarClassifierCascade* pCascade_face;
        CvMemStorage* storage;
    };

    void* threadface_func(void* parameter){

        CvSeq * detectRect_face;
        parameter_t *p =(parameter_t*)parameter;

        detectRect_face=cvHaarDetectObjects(p->capturedImg,p->pCascade_face,p->storage,1.15, 3, 0,cvSize(50,50));

        for(int i=0;i<(detectRect_face ? detectRect_face->total:0); i++ )
            {
                CvRect* r = (CvRect*)cvGetSeqElem(detectRect_face, i); 
                CvPoint pt1 = { r->x, r->y }; 
                CvPoint pt2 = { r->x + r->width, r->y + r->height }; 
                cvRectangle(p->capturedImg, pt1, pt2, CV_RGB(255,0,0), 1,8, 0); 

            }
        return 0;
    }

    int main ()
    {
            CvCapture* capture = cvCaptureFromCAM(0);
            IplImage* capturedImg;
            int resCount = 1;
            int flags = CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH;

            CvHaarClassifierCascade * pCascade_face;
            pthread_t threadface;


            pCascade_face = (CvHaarClassifierCascade *)cvLoad("C:/Users/Furkan/Desktop/Computer Vision/Programlar/opencv/data/haarcascades/haarcascade_frontalface_alt.xml");
            cvNamedWindow("FaceDetection");

            while (true)
            {

                CvMemStorage * storage = 0;
                capturedImg = cvQueryFrame(capture);
                storage = cvCreateMemStorage(0);

                parameter_t my_parameters;
                my_parameters.capturedImg=capturedImg;
                my_parameters.storage=storage;
                my_parameters.pCascade_face=pCascade_face;

                int k=pthread_create(&threadface,0,threadface_func,(void*)&my_parameters);

                if(k!=0)
                {
                    printf("Create thread failed! error");
                    return 1;
                }

                cvShowImage("FaceDetection", capturedImg);

            }

        cvDestroyWindow("FaceDetection");

        cvReleaseCapture(&capture);
        pthread_exit(NULL);
        return 0;
    }

请帮助。

1 个答案:

答案 0 :(得分:0)

  1. 从捕获中获得的IplImages指向视频驱动程序内存。要在另一个线程中使用它们,你必须克隆()它们。 (我明白了,你甚至试图将其吸引到那里)。
  2. 你在那里以荒谬的高速率产生新的线程,而不是等待一个完成
  3. 我在代码中看不到任何锁/互斥锁
  4. 请重新考虑使用多个线程。至少,它不会像这样工作

    (似乎你的opencv版本和你的api使用也需要升级......)