在运行官网的facedetect这个demo的时候,总是不会出来result的图形,电脑右下角提示的错误是“显示器驱动程序已停止响应,而且已恢复 windows 8(R)”。
&2 前期处理
&3 成功解决
首先,打开注册表,找到HKEY_LOCAL_MACHINE,在SYSTEM中的CurrentControlSet中的Control的GrphicsDrivers上面点击右键,新建QEORD(64位)值(Q),数值名称为:TdrDelay,数值数据为:8,基数不用改变,选择十六进制即可。
然后,在你的项目编译文件夹内加入四个文件,haarcascade_eye_tree_eyeglasses.xml和haarcascade_frontalface_alt.xml、opencv_ffmpeg310_64.dll及opencv_world310d.dll;
在opencv的环境配置中,(前面有博文介绍),去掉可执行文件目录,去掉附加依赖项的opencv_world310.lib,至此,所有的环境配置方面已经完成。
&4 demo的代码和运行结果
注意:在opencv安装文件夹sources\samples\cpp中的文件facedetect.cpp即是源代码。
1 #include "opencv2/objdetect.hpp" 2 #include "opencv2/highgui.hpp" 3 #include "opencv2/imgproc.hpp" 4 #include5 6 using namespace std; 7 using namespace cv; 8 9 static void help() 10 11 { 12 cout << "\nThis program demonstrates the cascade recognizer. Now you can use Haar or LBP features.\n" 13 "This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n" 14 "It's most known use is for faces.\n" 15 "Usage:\n" 16 "./facedetect [--cascade= this is the primary trained classifier such as frontal face]\n " 17 " [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n" 18 " [--scale=]\n " 19 " [--try-flip]\n" 20 " [filename|camera_index]\n\n" 21 "see facedetect.cmd for one call:\n" 22 "./facedetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\" --scale=1.3\n\n" 23 "During execution:\n\tHit any key to quit.\n" 24 "\tUsing OpenCV version " << CV_VERSION << "\n" << endl; 25 } 26 27 void detectAndDraw(Mat& img, CascadeClassifier& cascade, 28 CascadeClassifier& nestedCascade, 29 double scale, bool tryflip); 30 31 string cascadeName; 32 string nestedCascadeName; 33 34 int main(int argc, const char** argv) 35 { 36 VideoCapture capture; 37 Mat frame, image; 38 string inputName; 39 bool tryflip; 40 CascadeClassifier cascade, nestedCascade; 41 double scale; 42 43 cv::CommandLineParser parser(argc, argv, 44 "{help h||}" 45 "{cascade|haarcascade_frontalface_alt.xml|}" 46 "{nested-cascade|haarcascade_eye_tree_eyeglasses.xml|}" 47 "{scale|1|}{try-flip||}{@filename|lena.jpg|}" 48 ); 49 50 51 if (parser.has("help")) 52 { 53 help(); 54 return 0; 55 } 56 cascadeName = parser.get<string>("cascade"); 57 nestedCascadeName = parser.get<string>("nested-cascade"); 58 scale = parser.get<double>("scale"); 59 if (scale < 1) 60 scale = 1; 61 tryflip = parser.has("try-flip"); 62 inputName = parser.get<string>("@filename"); 63 if (!parser.check()) 64 { 65 parser.printErrors(); 66 return 0; 67 } 68 if (!nestedCascade.load(nestedCascadeName)) 69 cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; 70 if (!cascade.load(cascadeName)) 71 { 72 cerr << "ERROR: Could not load classifier cascade" << endl; 73 help(); 74 return -1; 75 } 76 if (inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1)) 77 { 78 int c = inputName.empty() ? 0 : inputName[0] - '0'; 79 if (!capture.open(c)) 80 cout << "Capture from camera #" << c << " didn't work" << endl; 81 } 82 else if (inputName.size()) 83 { 84 image = imread(inputName, 1); 85 if (image.empty()) 86 { 87 if (!capture.open(inputName)) 88 cout << "Could not read " << inputName << endl; 89 } 90 } 91 else 92 { 93 image = imread("../data/lena.jpg", 1); 94 if (image.empty()) cout << "Couldn't read ../data/lena.jpg" << endl; 95 } 96 97 if (capture.isOpened()) 98 { 99 cout << "Video capturing has been started ..." << endl; 100 101 for (;;) 102 { 103 capture >> frame; 104 if (frame.empty()) 105 break; 106 107 Mat frame1 = frame.clone(); 108 detectAndDraw(frame1, cascade, nestedCascade, scale, tryflip); 109 110 int c = waitKey(10); 111 if (c == 27 || c == 'q' || c == 'Q') 112 break; 113 } 114 } 115 else 116 { 117 cout << "Detecting face(s) in " << inputName << endl; 118 if (!image.empty()) 119 { 120 detectAndDraw(image, cascade, nestedCascade, scale, tryflip); 121 waitKey(0); 122 } 123 else if (!inputName.empty()) 124 { 125 /* assume it is a text file containing the 126 list of the image filenames to be processed - one per line */ 127 FILE* f = fopen(inputName.c_str(), "rt"); 128 if (f) 129 { 130 char buf[1000 + 1]; 131 while (fgets(buf, 1000, f)) 132 { 133 int len = (int)strlen(buf), c; 134 while (len > 0 && isspace(buf[len - 1])) 135 len--; 136 buf[len] = '\0'; 137 cout << "file " << buf << endl; 138 image = imread(buf, 1); 139 if (!image.empty()) 140 { 141 detectAndDraw(image, cascade, nestedCascade, scale, tryflip); 142 c = waitKey(0); 143 if (c == 27 || c == 'q' || c == 'Q') 144 break; 145 } 146 else 147 { 148 cerr << "Aw snap, couldn't read image " << buf << endl; 149 } 150 } 151 fclose(f); 152 } 153 } 154 } 155 156 return 0; 157 } 158 159 void detectAndDraw(Mat& img, CascadeClassifier& cascade, 160 CascadeClassifier& nestedCascade, 161 double scale, bool tryflip) 162 { 163 double t = 0; 164 vectorfaces, faces2; 165 const static Scalar colors[] = 166 { 167 Scalar(255, 0, 0), 168 Scalar(255, 128, 0), 169 Scalar(255, 255, 0), 170 Scalar(0, 255, 0), 171 Scalar(0, 128, 255), 172 Scalar(0, 255, 255), 173 Scalar(0, 0, 255), 174 Scalar(255, 0, 255) 175 }; 176 Mat gray, smallImg; 177 178 cvtColor(img, gray, COLOR_BGR2GRAY); 179 double fx = 1 / scale; 180 resize(gray, smallImg, Size(), fx, fx, INTER_LINEAR); 181 equalizeHist(smallImg, smallImg); 182 183 t = (double)cvGetTickCount(); 184 cascade.detectMultiScale(smallImg, faces, 185 1.1, 2, 0 186 //|CASCADE_FIND_BIGGEST_OBJECT 187 //|CASCADE_DO_ROUGH_SEARCH 188 | CASCADE_SCALE_IMAGE, 189 Size(30, 30)); 190 if (tryflip) 191 { 192 flip(smallImg, smallImg, 1); 193 cascade.detectMultiScale(smallImg, faces2, 194 1.1, 2, 0 195 //|CASCADE_FIND_BIGGEST_OBJECT 196 //|CASCADE_DO_ROUGH_SEARCH 197 | CASCADE_SCALE_IMAGE, 198 Size(30, 30)); 199 for (vector ::const_iterator r = faces2.begin(); r != faces2.end(); r++) 200 { 201 faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height)); 202 } 203 } 204 t = (double)cvGetTickCount() - t; 205 printf("detection time = %g ms\n", t / ((double)cvGetTickFrequency()*1000.)); 206 for (size_t i = 0; i < faces.size(); i++) 207 { 208 Rect r = faces[i]; 209 Mat smallImgROI; 210 vector nestedObjects; 211 Point center; 212 Scalar color = colors[i % 8]; 213 int radius; 214 215 double aspect_ratio = (double)r.width / r.height; 216 if (0.75 < aspect_ratio && aspect_ratio < 1.3) 217 { 218 center.x = cvRound((r.x + r.width*0.5)*scale); 219 center.y = cvRound((r.y + r.height*0.5)*scale); 220 radius = cvRound((r.width + r.height)*0.25*scale); 221 circle(img, center, radius, color, 3, 8, 0); 222 } 223 else 224 rectangle(img, cvPoint(cvRound(r.x*scale), cvRound(r.y*scale)), 225 cvPoint(cvRound((r.x + r.width - 1)*scale), cvRound((r.y + r.height - 1)*scale)), 226 color, 3, 8, 0); 227 if (nestedCascade.empty()) 228 continue; 229 smallImgROI = smallImg(r); 230 nestedCascade.detectMultiScale(smallImgROI, nestedObjects, 231 1.1, 2, 0 232 //|CASCADE_FIND_BIGGEST_OBJECT 233 //|CASCADE_DO_ROUGH_SEARCH 234 //|CASCADE_DO_CANNY_PRUNING 235 | CASCADE_SCALE_IMAGE, 236 Size(30, 30)); 237 for (size_t j = 0; j < nestedObjects.size(); j++) 238 { 239 Rect nr = nestedObjects[j]; 240 center.x = cvRound((r.x + nr.x + nr.width*0.5)*scale); 241 center.y = cvRound((r.y + nr.y + nr.height*0.5)*scale); 242 radius = cvRound((nr.width + nr.height)*0.25*scale); 243 circle(img, center, radius, color, 3, 8, 0); 244 } 245 } 246 imshow("result", img); 247 }
结果: