1、加载一张图片
main函数:
cv::Mat img = cv::imread("5.jpg");
vector<Point2f> points_vec = dectectFace68(img);
2、人脸68特征识别函数
在这里vector<Point2f> dectectFace68(Mat src)
{
vector<Point2f> points_vec;
int* pResults = NULL;
//在检测函数中使用了pBuffer。
//如果你调用多个线程中的函数,请为每个线程创建一个缓冲区!
unsigned char* pBuffer = (unsigned char*)malloc(DETECT_BUFFER_SIZE);
if (!pBuffer)
{
fprintf(stderr, "Can not alloc buffer.\n");
//return 100;
}
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
int doLandmark = 1;// do landmark detection
pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
1.2f, 2, 48, 0, doLandmark);
int cxa = *pResults;
ofstream file("facedata.txt", ios::out);
//打印检测结果
if (0 == cxa)
{
}
else
{
for (int i = 0; i < (pResults ? *pResults : 0); i++)
{
short* p = ((short*)(pResults + 1)) + 142 * i;
//rectangle(src, Rect(p[0], p[1], p[2], p[3]), Scalar(0, 255, 0), 2);
if (doLandmark)
{
for (int j = 0; j < 68; j++)
{
char c[8];
_itoa(j, c, 10);
Point2f ff(p[6 + 2 * j], p[6 + 2 * j + 1]);
points_vec.push_back(ff);
file << ff.x << "\t" << ff.y << endl;
/* circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 3, Scalar(0, 0, 255), 3);
CvPoint font;
putText(src, c, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 23, 0), 1);*/
}
}
}
}
return points_vec;
}
插入代码片
3、结果