通过try/catch block载入文件
今天在《Mastering OpenCV with practical computer vision algorithm》一书中看到一段代码,感觉很清晰,很整洁,特在此处记录一下,以方便以后的使用。代码如下:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
String face_detector_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
try {
face_cascade.load(face_detector_name);
}
catch (Exception e) {}
if (face_cascade.empty()) {
cerr << "Error: Could not load face Detector (";
cerr << face_detector_name << ")!"<<endl;
exit(1);
}
return 0;
}