Hi... i am trying to display the images from the .csv file. I am a begginer with opencv and c++ concepts, but am uanble to understand the need of some functions ....Here is the code which am trying to figure out. Someone help me with this problem it wil be a great help.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/face.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
using namespace cv;
using namespace cv::face;
static Mat norm_0_255(InputArray _src)
{
Mat src = _src.getMat();
Mat dst;
switch(src.channels())
{
case 1:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
break;
case 2:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
break;
default:
src.copyTo(dst);
break;
}
return dst;
}
void read_csv(const String filename, vector<Mat> & images,vector<int> & labels, char separator=';')
{
std::ifstream myfile(filename.c_str(),ifstream::in);
if(!myfile)
{
string error_message="File does not exist";
CV_Error(Error::StsBadArg, error_message);
}
string line, path,classlabel;
while(getline(myfile,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(int argc, const char *argv[])
{
if(argc!=2)
{
cout<<"usage:"<<"argv[0]"<<"<csv.txt> <output_folder>\n"<<endl;
return -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";
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<BasicFaceRecognizer>model = createFisherFaceRecognizer();
model->train(images, labels);
Mat mean = model->getMean();
if(argc == 2) {
imshow("mean", norm_0_255(mean.reshape(1,height)));
} else {
imwrite(format("%s/mean.jpeg", output_folder.c_str()), norm_0_255(mean.reshape(1,height)));
}
if(argc==2)
{
waitKey(0);
}
return 0;
}