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

#include <opencv2/core.hpp>
#include <opencv2/face.hpp>
#include <opencv2/highgui.hpp>

#include <iostream>
#include <fstream>
#include <sstream>

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

void read_csv(string& filename, vector<Mat>& images, vector<int>& 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<Mat> images;
	vector<int> 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<LBPHFaceRecognizer> 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<Mat> histograms = model->getHistograms();
	printf("Size of the histograms: %zd\n", histograms[0].total());

	waitKey(0);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值