【发布时间】:2016-09-12 16:52:51
【问题描述】:
我正在创建一个通过网络摄像头捕获视频流并检测面部并模糊它们的应用程序
这是我的代码
#include <iostream>
#include <vector>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/objdetect/objdetect.hpp>
using namespace std;
using namespace cv;
int blur_value = 50;
Mat src; Mat dst;
int main(int argc, char* argv[])
{
VideoCapture cap(0);
bool read = true;
char winName[] = "Blured WebCam";
String face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
if(!cap.isOpened())
{
cout<<"Error while opening your webcam\n";
return 0;
}
for(;;)
{
cap.read(src);
vector <Rect> faces;
Mat blk;
cvtColor( src, blk, COLOR_BGR2GRAY );
equalizeHist( blk, blk );
face_cascade.detectMultiScale( blk, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
blur( src, dst, Size( faces[i].width/2, faces[i].height/2), Point(-1,-1) );
}
imshow(winName, dst);
switch(waitKey(10))
{
case 27:
return 0;
}
}
return 0;
}
编译过程中没有错误,当我尝试运行应用程序时出现错误
我收到了这个错误
OpenCV 错误:断言失败 (size.width>0 && size.height>0) in 显示,文件 /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, 在抛出一个实例后调用第 269 行终止 'cv::异常'什么(): /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: 错误:(-215) size.width>0 && size.height>0 in function imshow
中止(核心转储)
问题出在哪里?
【问题讨论】:
-
断言告诉你问题出在哪里。使用调试器并回溯它。
-
如果您在第一帧没有看到任何人脸,那么
dst将不会被初始化并且您的imshow会失败,因为您显示的是一张空图像。当faces.empty()时你应该显示src,或者调整你的逻辑