// Beispielhafter Aufruf: BUILD_x86_64/progs/testSemanticSegmentation -config <CONFIGFILE>

/**
* @file testSemanticSegmentation.cpp
* @brief test semantic segmentation routines
* @author Erik Rodner
* @date 03/20/2008
*/

#ifdef NICE_USELIB_OPENMP
#include <omp.h>
#endif

#include <objrec/baselib/Config.h>
#include <objrec/baselib/StringTools.h>
#include <objrec/baselib/ICETools.h>

#include <objrec/semanticsegmentation/SemanticSegmentation.h>
#include <objrec/semanticsegmentation/SemSegLocal.h>
#include <objrec/semanticsegmentation/SemSegSTF.h>
#include <objrec/semanticsegmentation/SemSegCsurka.h>
#include <objrec/semanticsegmentation/SemSegCsurka2.h>
#include <objrec/semanticsegmentation/SemSegRegionBased.h>

#include <fstream>

using namespace OBJREC;

using namespace NICE;

using namespace std;

/**
 test semantic segmentation routines
*/
int main(int argc, char **argv)
{
	Config conf(argc, argv);

	MultiDataset md(&conf);

	const ClassNames & classNames = md.getClassNames("train");
	
	const LabeledSet *testFiles = md["test"];

	set<int> forbidden_classes;

	std::string forbidden_classes_s = conf.gS("analysis", "forbidden_classes", "");

	classNames.getSelection(forbidden_classes_s, forbidden_classes);

	LOOP_ALL_S(*testFiles)
	{
		EACH_INFO(classno, info);

		std::string file = info.img();

		NICE::Image lm;
		GenericImage<double> probabilities;
		
		if (info.hasLocalizationInfo())
		{
			const LocalizationResult *l_gt = info.localization();

			lm.resize(l_gt->xsize, l_gt->ysize);
			lm.set(0);
			l_gt->calcLabeledImage(lm, classNames.getBackgroundClass());
		}

		//semseg->semanticseg(file, lm, probabilities);

		NICE::Image lm_gt;

		if (info.hasLocalizationInfo())
		{
			const LocalizationResult *l_gt = info.localization();

			lm_gt.resize(l_gt->xsize, l_gt->ysize);
			lm_gt.set(0);

			fprintf(stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n");
			l_gt->calcLabeledImage(lm_gt, classNames.getBackgroundClass());
		}
		
		set<int> classes;
		for(int x = 0; x < lm_gt.width(); x++)
		{
		  for(int y = 0; y < lm_gt.height(); y++)
		  {
		    classes.insert(lm_gt.getPixel(x,y));
		  }
		}
		
		
		
		// write allowed classes
		string cndir = conf.gS("SemSegCsurka", "cndir", "");
		std::vector< std::string > list;
		StringTools::split (file, '/', list);
		cout << cndir<< "/" << list.back() << ".dat" << endl;
		
		string cname = list.back();
		
		if(cndir != "")
		{
			string fname = cndir+"/"+cname+".dat";
			cout << fname << endl;
			ofstream outfile(fname.c_str());
			
			set<int>::iterator theIterator;
			for( theIterator = classes.begin(); theIterator != classes.end(); theIterator++ ) {
			  outfile << *theIterator << endl;
			}     
			
		}
		else
		{
			cerr << "please define directory for writing filenames in config: SemSegCsurka::cndir" << endl;
			exit(-1);
		}
	}

	return 0;
}