图片人脸特征点检测

注:这是默认装上dlib的情况

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/opencv.h>  
#include <iostream>
#include<vector>
#include<math.h>
#include <opencv2/opencv.hpp> 
using namespace dlib;
using namespace std;


std::vector<string> getTrainimg(string str){
cv::Directory dir;
string path1 = "C:/Users/Administrator/Desktop/表情库/训练/" + str;
string exten1 = "*.jpg";//JAFFE .jpg ,CK .bmp
bool addPath1 = true;
std::vector<string> filenames = dir.GetListFiles(path1, exten1, addPath1);

return filenames;
}
int main(int argc, char** argv)
{
try
{
// This example takes in a shape model file and then a list of images to
// process.  We will take these filenames in as command line arguments.
// Dlib comes with example images in the examples/faces folder so give
// those as arguments to this program.
//if (argc == 1)
//{
// cout << "Call this program like this:" << endl;
// cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
// cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
// cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
// return 0;
//}
//std::cout << "argc:" << argc << std::endl;
// We need a face detector.  We will use this to get bounding boxes for
// each face in an image.
frontal_face_detector detector = get_frontal_face_detector();
// And we also need a shape_predictor.  This is the tool that will predict face
// landmark positions given an image and face bounding box.  Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
shape_predictor sp;
// std::cout << "argv[1]:" << argv[1] << std::endl;
deserialize("shape_predictor_68_face_landmarks.dat") >> sp;


image_window win;// win_faces;
// Loop over all the images provided on the command line.
std::vector<string> filenames=getTrainimg("happy");
int filesize = filenames.size;
for (int i = 3; i < filesize; ++i)
{
array2d<bgr_pixel> img;
string filename = "C://Users//74205//Desktop//rawImage//";
filename.append(to_string(i));
filename.append(".jpg");
load_image(img, filename);
// Make the image larger so we can detect small faces.
pyramid_up(img);


// Now tell the face detector to give us a list of bounding boxes
// around all the faces in the image.
std::vector<rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << endl;


// Now we will go ask the shape_predictor to tell us the pose of
// each face we detected.
std::vector<full_object_detection> shapes;
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);
cout << "number of parts: " << shape.num_parts() << endl;
cout << "pixel position of first part:  " << shape.part(0) << endl;
cout << "pixel position of second part: " << shape.part(1) << endl;
// You get the idea, you can get all the face part locations if
// you want them.  Here we just store them in shapes so we can
// put them on the screen.
shapes.push_back(shape);
cv::Mat temp = dlib::toMat(img);


for (int k = 0; k < 68; ++k){
circle(temp, cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);//j代表检测到的第几个人脸
putText(temp, to_string(k), cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0), 1, 4);//k代表68个特征点
}
}


// Now let's view our face poses on the screen.
win.clear_overlay();
string newFilename = "C://Users//74205//Desktop//detection//";
newFilename.append(to_string(i));
newFilename.append(".jpg");
const char *file = newFilename.data();
cv::Mat temp = dlib::toMat(img);//将img图像进行保存,转化成mat类型的对象
//将处理好的图像进行由mat形式转化为IplImage形式
IplImage *src;
src = &IplImage(temp);
cvSaveImage(file, src);
win.set_image(img);
//win.add_overlay(render_face_detections(shapes));


We can also extract copies of each face that are cropped, rotated upright,
and scaled to a standard size as shown here:
//dlib::array<array2d<rgb_pixel> > face_chips;
//extract_image_chips(img, get_face_chip_details(shapes), face_chips);
//win_faces.set_image(tile_images(face_chips));
cout << "加上vector向量之后" << endl;
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
system("pause");

}

留作备份

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于C语言的人脸68特征点检测是一种基于计算机视觉和图像处理技术的人脸识别方法。该算法可以自动在图像或视频中定位并标记出人脸的68个关键点,包括眼睛、眉毛、鼻子、嘴巴等面部特征。 在C语言中实现人脸68特征点检测涉及到图像预处理、特征提取和分类器训练等多个步骤。首先,需要对输入的图像进行灰度化处理,以减少计算量并提高检测精度。然后,使用基于Haar特征或其他特征提取方法,从图像中提取人脸区域。接下来,使用级联分类器(如AdaBoost算法)将提取的人脸区域进行分类,确定其是否为人脸。 一旦确定了人脸区域,下一步就是利用形状模型或回归算法对人脸特征点进行定位。形状模型是通过对大量标注好的人脸图片进行统计建模得到的,可以用来预测人脸关键点的位置。回归算法则是通过训练一个回归模型来预测关键点的坐标。这些方法都需要使用大量的训练数据进行模型的训练和优化。 最后,将检测到的人脸特征点绘制在原始图像上,以便用户可视化并使用。这样就完成了基于C语言的人脸68特征点检测。 虽然用C语言实现人脸68特征点检测可能会比较复杂和繁琐,但由于C语言具有高效、可移植和广泛应用的特点,它仍然是人脸识别领域中常用的编程语言之一。因此,掌握C语言并学习如何实现人脸68特征点检测可以为计算机视觉研究人员和从业者提供更多的工具和技术选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值