/* * Copyright (c) 2011. Philipp Wagner . * Released to public domain under terms of the BSD Simplified license. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the organization nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * See */ #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/contrib/contrib.hpp" #include #include #include using namespace cv; using namespace std; static void read_csv(const string& filename, vector& images, vector& labels, std::map& labelsInfo, char separator = ';') { ifstream csv(filename.c_str()); if (!csv) CV_Error(CV_StsBadArg, "No valid input file was given, please check the given filename."); string line, path, classlabel, info; while (getline(csv, line)) { stringstream liness(line); path.clear(); classlabel.clear(); info.clear(); getline(liness, path, separator); getline(liness, classlabel, separator); getline(liness, info, separator); if(!path.empty() && !classlabel.empty()) { cout << "Processing " << path << endl; int label = atoi(classlabel.c_str()); if(!info.empty()) labelsInfo.insert(std::make_pair(label, info)); // 'path' can be file, dir or wildcard path String root(path.c_str()); vector files; glob(root, files, true); for(vector::const_iterator f = files.begin(); f != files.end(); ++f) { cout << "\t" << *f << endl; Mat img = imread(*f, CV_LOAD_IMAGE_GRAYSCALE); static int w=-1, h=-1; static bool showSmallSizeWarning = true; if(w>0 && h>0 && (w!=img.cols || h!=img.rows)) cout << "\t* Warning: images should be of the same size!" << endl; if(showSmallSizeWarning && (img.cols<50 || img.rows<50)) { cout << "* Warning: for better results images should be not smaller than 50x50!" << endl; showSmallSizeWarning = false; } images.push_back(img); labels.push_back(label); } } } } int main(int argc, const char *argv[]) { // Check for valid command line arguments, print usage // if no arguments were given. if (argc != 2 && argc != 3) { cout << "Usage: " << argv[0] << " [arg2]\n" << "\t - path to config file in CSV format\n" << "\targ2 - if the 2nd argument is provided (with any value) " << "the advanced stuff is run and shown to console.\n" << "The CSV config file consists of the following lines:\n" << ";