2014-01-21 201 views
1

我正在尋找在2D圖像中找到人臉的應用程序,後來在同一圖像中我想找到嘴巴,但我現在有一些問題。這是我的代碼到目前爲止:OpenCv斷言失敗

for (int i = 0; i < faces.size(); i++) 
{ 
     Point pt1(faces[i].x, faces[i].y); 
     Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width)); 
     rectangle(frame, pt1, pt2, Scalar(255,0 , 0), 2, 8, 0); 

        //I WANT ROI(FOR MOUTH DETECTION) TO BE ONLY HALF OF THE RECTANGLE WITH FACE 
     Rect mouthROI; 
     mouthROI.x = (faces[i].x); 
     mouthROI.y = faces[i].y*(1.5); 
     mouthROI.width = (faces[i].x + faces[i].height); 
     mouthROI.height = (faces[i].y + faces[i].width); 

        //I CHECK IF NEW RECTANGLE IS EXACTLY BOTTOM HALF OF PREVIOUS ONE 
     Point ptAA(mouthROI.x, mouthROI.y); 
     Point ptBB(mouthROI.width, mouthROI.height); 
     rectangle(frame, ptBB, ptAA, Scalar(0,0 , 255), 2, 2, 0); 


     Mat image_roi = frame(mouthROI); 

     cvtColor(frame, frame_gray, COLOR_BGR2GRAY); 
     equalizeHist(frame_gray, frame_gray); 

        // DETECTING MOUTH INSIDE ROI OF EARLIER DETECTED FACE 
     mouth_cascade.detectMultiScale(image_roi, mouths, 1.1, 2, 0, Size(30, 30)); 

     for(int i = 0; i < mouths.size(); i++) 
     { 
      Point pt1(mouths[i].x, mouths[i].y); // Display detected faces on main window - live stream from camera 
      Point pt2((mouths[i].x + mouths[i].height), (mouths[i].y + mouths[i].width)); 
      rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0); 
     } 

} 

不幸的是,這段代碼不起作用。我得到這樣的錯誤:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat(const cv::Mat&, const cv::Rect&), file C:/build/2_4_PackSlave-win32-vc11-shared/opencv/modules/core/src/matrix.cpp, line 323 

我該如何解決這個錯誤。謝謝大家的幫助!

+0

您傳遞給引發聲明的函數的參數不正確。使用調試器來確定哪個函數正在拋出assert並將其跟蹤到您的代碼中發起調用的行。 –

+0

錯誤在於此地點'mat image_roi = frame(mouthROI);',但我仍然不知道爲什麼... – user2592968

+0

檢查出[類似鏈接](http://stackoverflow.com/questions/20855192/assertion- error-in-grabcut)並查看你是否沒有提交相同的錯誤。 – BlueSword

回答

5

您初始化mouthROI的方式不正確。它應該是這樣的,而不是,

<previous code> 
mouthROI.width = (faces[i].height); 
mouthROI.height = (faces[i].width); 

Point ptAA(mouthROI.x, mouthROI.y); 
Point ptBB(mouthROI.x+mouthROI.width, mouthROI.y+mouthROI.height); 

記住,簡歷::矩形不走位置矩形的,它需要左上角和寬度位置和高度 ..有關更多詳細信息,請參閱documentation

HTH