我试图cacadeClassifier_GPU
在 OpenCV 中同时检测面部和眼睛。检测面部工作正常,但它没有检测到任何眼睛。我在 Visual Studio 2010 中使用 opencv 2.4.9(gpu 版本)。
这是我的代码。
for(;;)
{
cap >> frame; // get a new frame from camera
double t = (double)getTickCount();
GpuMat faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY); // convert to gray image as face detection do NOT use color info
int scale = 2;
cv::Mat resized_frame_gray((int)(frame_gray.rows/ scale), (int)(frame_gray.cols/ scale),CV_8UC1);
cv::resize(frame_gray, resized_frame_gray, resized_frame_gray.size() );
GpuMat gray_gpu(resized_frame_gray); // copy the gray image to GPU memory
equalizeHist(resized_frame_gray,resized_frame_gray);
int detect_num = cascadeFace.detectMultiScale(gray_gpu, faces, 1.2, 4, Size(10, 10) ); // call face detection routine
Mat obj_host;
faces.colRange(0, detect_num).download(obj_host); // retrieve results from GPU
Rect* cfaces = obj_host.ptr<Rect>(); // results are now in "obj_host"
for(int i=0;i<detect_num;++i)
{
Point pt1 = cfaces[i].tl();
pt1.x *= scale;
pt1.y *= scale;
Size sz = cfaces[i].size();
Point pt2(pt1.x+sz.width*scale, pt1.y+sz.height*scale);
rectangle(frame, pt1, pt2, Scalar(255));
Mat faceROI = resized_frame_gray( cfaces[i] );
GpuMat faceROIgpu(faceROI);
GpuMat eyes;
//-- In each face, detect eyes
int detect_num2 = cascade_eye.detectMultiScale( faceROIgpu, eyes, 1.2, 4, Size(10, 10) );
Mat obj_host_eye;
eyes.colRange(0, detect_num2).download(obj_host_eye);
Rect* cEyes = obj_host_eye.ptr<Rect>();
//cout<<detect_num2<<endl;
for( int j = 0; j < detect_num2; j++ )
{
Point pt1_e = cEyes[i].tl();
pt1_e.x *= scale;
pt1_e.y *= scale;
Size sz_e = cEyes[i].size();
Point pt2_e(pt1_e.x+sz_e.width*scale, pt1_e.y+sz_e.height*scale);
rectangle(frame, pt1_e, pt2_e, Scalar(255,0,255));
}
imshow("faces", frame);
waitKey(1);
}