openCV学习笔记(十八) —— 人脸识别 —— 程序 —— LBPH局部二值模式直方图

#include 
#include 
#include 

#include 
#include 
#include 

using namespace std;
using namespace cv;
using namespace cv::face;

void read_csv(string& filename, vector& images, vector& labels, char separator = ';')
{
	ifstream file;
	file.open(filename.c_str(), ifstream::in);
	if (!file)
	{
		printf("can't open file: %s\n", filename.c_str());
		exit(-1);
	}

	string line, path, classlabel;
	while (getline(file, line))
	{
		stringstream liness(line);
		getline(liness, path, separator);
		getline(liness, classlabel);
		if (!path.empty() && !classlabel.empty())
		{
			images.push_back(imread(path, 0));
			labels.push_back(atoi(classlabel.c_str()));
		}
	}
}

int main()
{
	string filename = string(".\\face_recg\\face\\at.txt");

	vector images;
	vector labels;

	try
	{
		read_csv(filename, images, labels);
	}
	catch (const cv::Exception& e)
	{
		printf("failed opening file %s reason %s\n", filename.c_str(), e.msg.c_str());
		exit(1);
	}

	if (images.size() <= 1)
	{
		printf("This demo needs at least 2 images to work. Please add more images to your data set!\n");
		exit(1);
	}

	Mat testSample = images[images.size() - 1];
	int testLabel = labels[labels.size() - 1];

	images.pop_back();
	labels.pop_back();

	Ptr model = LBPHFaceRecognizer::create();
	model->train(images, labels);

	int predictLabel = model->predict(testSample);

	printf("test label = %d, predict label = %d\n", testLabel, predictLabel);

	//model->setThreshold(0.0);
	//predictLabel = model->predict(testSample);

	//printf("test label = %d, predict label = %d\n", testLabel, predictLabel);

	//打印参数
	int radius = model->getRadius();	//中心像素点到周围像素点的距离
	int neibs = model->getNeighbors();	//周围像素点的个数
	int grid_x = model->getGridX();		//将一张图片在x方向分成几块
	int grid_y = model->getGridY();		//将一张图片在y方向分成几块
	double threshold = model->getThreshold();	//相似度阈值

	printf("radius = %d\n", radius);
	printf("neibs = %d\n", neibs);
	printf("grid_x = %d\n", grid_x);
	printf("grid_y = %d\n", grid_y);
	printf("threshold = %e\n", threshold);

	vector histograms = model->getHistograms();
	printf("Size of the histograms: %zd\n", histograms[0].total());

	waitKey(0);

	return 0;
}

 

你可能感兴趣的:(openCV,openCV,人脸识别,算法)