// OpenCV3.4.1_Test.cpp : 定義控制臺應用程式的入口點。
//
#include "stdafx.h"
#include <opencv.hpp>
#include <face.hpp>
#include <iostream>
#include <face/facerec.hpp>
using namespace cv;
using namespace cv::face;
using namespace std;
int main(int argc, char** argv) {
string filename = string("H:/images/orl_faces/image.csv");
ifstream file(filename.c_str(), ifstream::in);
if (!file) {
printf("could not load file correctly...\n");
return -1;
}
string line, path, classlabel;
vector<Mat> images;
vector<int> labels;
char separator = ';';
while (getline(file, line)) {
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
if (!path.empty() && !classlabel.empty()) {
//printf("path : %s\n", path.c_str());
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
if (images.size() < 1 || labels.size() < 1) {
printf("invalid image path...\n");
return -1;
}
int height = images[0].rows;
int width = images[0].cols;
printf("height : %d, width : %d\n", height, width);
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
images.pop_back();
labels.pop_back();
// train it
Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create();
model->train(images, labels);
// recognition face
int predictedLabel = model->predict(testSample);
printf("actual label : %d, predict label : %d\n", testLabel, predictedLabel);
Mat eigenvalues = model->getEigenValues();
Mat W = model->getEigenVectors();
Mat mean = model->getMean();
Mat meanFace = mean.reshape(1, height);
Mat dst;
if (meanFace.channels() == 1) {
normalize(meanFace, dst, 0, 255, NORM_MINMAX, CV_8UC1);
}
else if (meanFace.channels() == 3) {
normalize(meanFace, dst, 0, 255, NORM_MINMAX, CV_8UC3);
}
imshow("Mean Face", dst);
waitKey(0);
getchar();
return 0;
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130064.html
標籤:C++ 語言
上一篇:回圈鏈表約瑟夫問題!!
