Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/core/core.hpp"
- #include "opencv2/contrib/contrib.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- using namespace cv;
- using namespace std;
- static Mat norm_0_255(InputArray _src) {
- Mat src = _src.getMat();
- // Create and return normalized image:
- Mat dst;
- switch(src.channels()) {
- case 1:
- cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
- break;
- case 3:
- cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
- break;
- default:
- src.copyTo(dst);
- break;
- }
- return dst;
- }
- static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
- std::ifstream file(filename.c_str(), ifstream::in);
- if (!file) {
- string error_message = "No valid input file was given, please check the given filename.";
- CV_Error(CV_StsBadArg, error_message);
- }
- string line, path, classlabel;
- while (getline(file, line)) {
- stringstream liness(line);
- getline(liness, path, separator);
- getline(liness, classlabel);
- if(!path.empty() && !classlabel.empty()) {
- Mat t = imread(path,0);
- if (t.rows < 1) {
- continue;
- } else {
- cout << path << endl;
- images.push_back(t);
- labels.push_back(atoi(classlabel.c_str()));
- }
- }
- }
- }
- int main(int argc, const char *argv[]) {
- if (argc < 2) {
- cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;
- exit(1);
- }
- string output_folder = ".";
- if (argc == 3) {
- output_folder = string(argv[2]);
- }
- string fn_csv = string(argv[1]);
- vector<Mat> images;
- vector<int> labels;
- try {
- read_csv(fn_csv, images, labels);
- } catch (cv::Exception& e) {
- cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
- exit(1);
- }
- if(images.size() <= 1) {
- string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
- CV_Error(CV_StsError, error_message);
- }
- int height = images[0].rows;
- //Mat testSample = images[images.size() - 1];
- //int testLabel = labels[labels.size() - 1];
- //images.pop_back();
- //labels.pop_back();
- Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
- model->train(images, labels);
- Mat eigenvalues = model->getMat("eigenvalues");
- Mat W = model->getMat("eigenvectors");
- Mat mean = model->getMat("mean");
- if(argc == 2) {
- imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));
- } else {
- imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));
- }
- for (int i = 0; i < min(10, W.cols); i++) {
- Mat ev = W.col(i).clone();
- Mat grayscale = norm_0_255(ev.reshape(1, height));
- Mat cgrayscale;
- applyColorMap(grayscale, cgrayscale, COLORMAP_BONE);
- if(argc == 2) {
- imshow(format("fisherface_%d", i), cgrayscale);
- } else {
- imwrite(format("%s/fisherface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));
- }
- }
- if(argc == 2) {
- waitKey(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement