#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <cv.h>
#include <highgui.h>

using namespace std;


//Ocho vecinos
#define A0 data[u*step+v*channels+k]
#define A1 data[u*step+(v+1)*channels+k]
#define A2 data[(u-1)*step+(v+1)*channels+k]
#define A3 data[(u-1)*step+(v)*channels+k]
#define A4 data[(u-1)*step+(v-1)*channels+k]
#define A5 data[u*step+(v-1)*channels+k]
#define A6 data[(u+1)*step+(v-1)*channels+k]
#define A7 data[(u+1)*step+(v)*channels+k]
#define A8 data[(u+1)*step+(v+1)*channels+k]

typedef struct{
	int x;
	int y;
}posicion;
typedef struct{
	int R,G,B;
}color;


typedef struct{
	posicion ini;
	posicion fin;
}cuadrado;

typedef struct {
	IplImage *img;
	char name[32];
	vector<cuadrado> mano; 
	vector<cuadrado> nomano; 
}imagen;

int trackbar=0;


void visor(int position){	
}


int main(){
	FILE * fmano;
	FILE * fnomano;
	FILE * fresult;
	IplImage * BINimg;



	char buffer[200];
	int height,width;
	vector<imagen> imgs;
	imagen img;
	cuadrado cuad;
	int cont=0;

	fopen_s(&fmano,"MANO.txt","r");
	if(fmano==NULL){
		printf("Error en el fichero MANO.txt\n");
		exit(0);
	}

	fopen_s(&fnomano,"NoMANO.txt","r");
	if(fnomano==NULL){
		printf("Error en el fichero NoMANO.txt\n");
		exit(0);
	}
	fopen_s(&fresult,"Resultados.txt","w");
	if(fresult==NULL){
		printf("Error en el fichero Resultados.txt\n");
		exit(0);
	}

	//Leer cuadrados Mano
	while( fgets(buffer,200,fmano) != NULL ){
		if(buffer[0] != '\t'){
			int length=strlen(buffer);
			strncpy_s(img.name, buffer,length-1);
			img.img = cvLoadImage(img.name,1);
			imgs.push_back(img);
		}
		else{
			sscanf_s(buffer,"\t(%d,%d)  (%d,%d)",&cuad.ini.x, &cuad.ini.y, &cuad.fin.x, &cuad.fin.y);
			imgs.back().mano.push_back(cuad);
		}
	}
	//Leer cuadrados NoMano
	int k=-1;
	while( fgets(buffer,200,fnomano) != NULL ){
		if(buffer[0] != '\t'){
			k++;
		}
		else{
			sscanf_s(buffer,"\t(%d,%d)  (%d,%d)",&cuad.ini.x, &cuad.ini.y, &cuad.fin.x, &cuad.fin.y);
			imgs[k].nomano.push_back(cuad);
		}
	}
	if(imgs.size() ==0){
		printf("No se ha cargado ninguna imagen\n");
		return 0;
	}
	
	char BIN[]="BIN";
	char BINname[40]="";

	

	for(int i=0; i<imgs.size(); i++){
		strcat_s(BINname,40,BIN);
		strcat_s(BINname,40,imgs[i].name);
		BINimg = cvLoadImage(BINname,0);
		if(BINimg == NULL)
			exit(0);

		float VP=0;
		float VN=0;
		float FP=0;
		float FN=0;
		float total;
		
		int step= BINimg->widthStep;
		uchar* data = (uchar *) BINimg->imageData;
		for( int j = 0; j < imgs[i].mano.size(); j++)
			for( int u = imgs[i].mano[j].ini.y; u <= imgs[i].mano[j].fin.y; u++){
				for( int v = imgs[i].mano[j].ini.x; v <= imgs[i].mano[j].fin.x; v++){
					if(data[u*step+v] != 0)
						VP++;				
					else
						FN++;
				}
			}
		for( int j = 0; j < imgs[i].nomano.size(); j++)
		for( int u = imgs[i].nomano[j].ini.y; u <= imgs[i].nomano[j].fin.y; u++){
			for( int v = imgs[i].nomano[j].ini.x; v <= imgs[i].nomano[j].fin.x; v++){
				if(data[u*step+v] == 0)
					VN++;				
				else
					FP++;
			}
		}
		total=(FP+FN+VP+VN);
		printf("%s: error= %.3f\t FP= %5d\t FN= %5d\t T= %5d\n",BINname, 100*(FP+FN)/total, int(FP), int(FN) ,int(total) ); 
		fprintf(fresult,"%d %.3f %d %d %d\n",i, 100*(FP+FN)/total, int(FP), int(FN) ,int(total) ); 
		BINname[0]=0;
	}
	fflush(fresult);
	while(1){
		int key=(cvWaitKey(10) & 255);
		if(key==27) break;
	}

	fclose(fmano);
	fclose(fnomano);
	fclose(fresult);
	int n = imgs.size();
	for(int k=0; k < n; k++){
		cvReleaseImage(&imgs[k].img);
	}
}