I am using MFC to write GUI that allows to display video from webcam using opencv 2.4.10. However, I have a error that recorded in youtube.I using thread to show video. I used a thread to open and detect video.The thread is defined as
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;
}
And I found the problem in the detectFace function. If I don't use it, the program works well. However, when I do it, and after 1 minutes, I click close button. it has error
Unhandled exception at at 0x760C4598 in DialogResize.exe: Microsoft C++ exception: cv::Exception at memory location 0x01A5EFB8.
The detectFace function is
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;
}
I think the problem is free memory problem or detect face try to detect a empty image. I am not sure. Could you find my problem in above code. For full code. You can download at here