c++ - 使用 MFC 在 opencv 中的函数 cvInitImageHeader 中输入 roi 错误

标签 c++ opencv mfc

我正在使用 MFC 编写 GUI,允许使用 opencv 2.4.10 显示来自网络摄像头的视频。但是,我在 http://youtu.be/j9Z-oKrDMoU 中记录了一个错误.我使用线程来显示视频。我使用线程打开并检测视频。线程定义为

UINT CDialogResizeDlg::StartThread (LPVOID param)
{
    THREADSTRUCT*    ts = (THREADSTRUCT*)param;

    //here is the time-consuming process 
    //which interacts with your dialog
    AfxMessageBox ("Thread is started!");
    // TODO: Add your control notification handler code here
    cap.open(0);
    //VideoCapture cap("C:/Users/lsf-admin/Pictures/Camera Roll/video000.mp4");
    cv::vector<cv::Rect> faces;
    cv::Mat frame;
    cv::Mat graySacleFrame;
    cv::Mat original;
    if (!cap.isOpened())
    {

        AfxMessageBox(_T("Failed to load video file"));
        return 1;
        //return exit(1);
    }

    while (true)
    {
        cap >> frame;
        if (!frame.empty()){
        frame=ts->_this->detectFace(frame);
        //Display result
        CDC* vDC;
        vDC =ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
        CRect rect;
        ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
        IplImage* image2=cvCloneImage(&(IplImage)frame);
        ts->_this->DisplayIplImageToPictureBox(image2, vDC, rect); //img is IplImage* variable.
        ts->_this->ReleaseDC(vDC);
        cvReleaseImage(&image2);
        }
        if (cv::waitKey(30) >= 0) break;
    }

    //you can also call AfxEndThread() here
    return 1;
}

而且我在 detectFace 函数中发现了问题。如果我不使用它,该程序运行良好。但是,当我这样做时,1 分钟后,我单击关闭按钮。有错误

Unhandled exception at at 0x760C4598 in DialogResize.exe: Microsoft C++ exception: cv::Exception at memory location 0x01A5EFB8.

检测人脸函数是

cv::Mat CDialogResizeDlg::detectFace(cv::Mat image)
{
    // Load Face cascade (.xml file)
    //lbpcascades/lbpcascade_frontalface.xml
    if(image.empty())
    {
        image=NULL;
        return image;
    }
    // Detect faces
    std::vector<cv::Rect> faces;
     //detect face in gray image
    //convert image to gray scale and equalize
    cv::Mat graySacleFrame;
    cv::cvtColor(image, graySacleFrame, CV_BGR2GRAY);
    //equalizeHist(graySacleFrame, graySacleFrame);         
    face_cascade.detectMultiScale(graySacleFrame, faces, 1.1, 3, 0, cv::Size(90, 90));

    // Draw circles on the detected faces
    for( int i = 0; i < faces.size(); i++ )
    { 
        rectangle(image, faces[i], CV_RGB(0, 255, 0), 1);

    }

    return image;
}

我认为问题是可用内存问题或检测面部尝试检测空图像。我不知道。你能在上面的代码中找到我的问题吗?对于完整的代码。您可以在here下载

最佳答案

某些 OpenCV 方法抛出异常。你需要这样处理:

try
{
    ... // call OpenCV
}
catch( cv::Exception& e )
{
    const char* err_msg = e.what();
    std::cout << "exception caught: " << err_msg << std::endl;
}

关于c++ - 使用 MFC 在 opencv 中的函数 cvInitImageHeader 中输入 roi 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340680/

相关文章:

c# - 如何通过 Assembly 访问泛型类型来解决它们?

c++ - 像素修改不覆盖整个图像

c++ - CPP + 正则表达式来验证 URL

C++ 使用 cppcheck 建议的显式

c++ - 用另一个新语句重置动态数组,需要先删除吗?

opencv - 有什么办法用流中的伪像过滤图像?

c++ - 如何解决 Qt Creator 变量 -“<not accessible>” 行为 (Opencv)

c++ - 从文本文件加载字符串后,字符串未相应格式化

c++ - 使用 MFC CFile 读写文件

c++ - 当你想实现一个可以返回 "nothing"的函数时,何时使用 boost::optional 以及何时使用 std::unique_ptr ?