gpt4 book ai didi

c++ - 奥斯卡自拍中的眼睛检测

转载 作者:太空宇宙 更新时间:2023-11-03 22:52:33 26 4
gpt4 key购买 nike

我必须对著名的奥斯卡自拍图像中的每张脸进行眼部检测。我尝试在面部上使用 Haar Casacades,因为它们中的大多数都在近正面,但眼部检测完全是随机的,没有眼睛被检测到完全认可。

enter image description here

我已尝试使用相同的 haar 级联 xml 文件对单面图像进行眼睛检测,效果很好。

我可以采取哪些步骤来正确检测眼睛?

我用于眼睛检测的图像可以从这里下载:

https://drive.google.com/file/d/0B3jt6sHgpxO-d1plUjg5eU5udW8/view?usp=sharing

下面是我为面部和眼睛检测编写的代码。基本思路是,我首先使用 viola jones 算法检测人脸,然后在每张脸上尝试检测眼睛。

#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <opencv2/objdetect/objdetect.hpp>
#include <vector>

using namespace cv;
using namespace std;

int x,y,w,h;

int main(int argc, const char** argv)
{
Mat image = imread("oscarSelfie.jpg",CV_LOAD_IMAGE_UNCHANGED);
Mat gray_img;
cvtColor(image, gray_img, CV_BGR2GRAY);
string faceCascade_file = "haarcascade_frontalface_alt2.xml";
string eyeCascade_file = "haarcascade_eye.xml";

CascadeClassifier faceCascade;
CascadeClassifier eyeCascade;
//Cascade classifier is a class which has a method to load the classifier from file
if( !faceCascade.load( faceCascade_file ) )
{ cout<<"--(!)Error loading\n"; return -1; };
//If it returns zero, it means an error has occured in loading the classifier

if( !eyeCascade.load( eyeCascade_file ) )
{ cout<<"--(!)Error loading\n"; return -1; };

equalizeHist(gray_img, gray_img);
//Increases contrast and make image more distingushable

/***** Detecting Faces in Image *******/
vector<Rect> faces;
vector<Rect> eyes;
//Rect is a class handling the rectangle datatypes
faceCascade.detectMultiScale(gray_img, faces, 1.1, 1, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
//faces.size()-it will return number of faces detected
for( int i = 0; i < faces.size(); i++ )
{
x = faces[i].x;
y = faces[i].y;
w = faces[i].width;
h = faces[i].height;
//Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
//ellipse( image, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
rectangle(image, cvPoint(x,y), cvPoint(x+w,y+h), CV_RGB(0,255,0), 2, 8 );

/******** Detecting eyes ***********/
eyeCascade.detectMultiScale(gray_img, eyes, 1.1, 50, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

for(int j=0; j < eyes.size(); j++)
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( image, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}

namedWindow("oscarSelfie :)", CV_WINDOW_AUTOSIZE);
imshow("oscarSelfie :)", image);
waitKey(0);
destroyWindow("pic");
return 0;

}`

最佳答案

我用 facedetect.cpp 得到以下结果(使用 haarcascade_eye_tree_eyeglasses.xml)

不要指望找到所有的面孔和眼睛 enter image description here

我也尝试了 dlib 的 face_landmark_detection_ex.cpp 来比较结果 enter image description here

dlib 有一个额外的功能,可以让你对齐面,如下所示

enter image description here

关于c++ - 奥斯卡自拍中的眼睛检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36236167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com