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

#define INF 999999999
#define KERNEL 3
#define K 9


using namespace std;

typedef struct{
	int mano;
	int val0, val1, val2;
}pixel;

int trackbar=0;

typedef struct{
	double dist;
	int valor;
}clasif;
void my_mouse_callback(int event, int x, int y, int flags, void* param){
	IplImage* image = (IplImage*) param;
	uchar *data=(uchar *)image->imageData;
	int height,width;
	int step,channels;	
	height    = image->height;
	width     = image->width;
	step	  = image->widthStep;
	channels  = image->nChannels;
	if(event==CV_EVENT_LBUTTONUP){
		int R,G,B;
		R=data[y*step+x*channels+2];
		G=data[y*step+x*channels+1];
		B=data[y*step+x*channels];

		printf("R=%d G=%d B=%d\n\tR-B=%d   R-G=%d   B-G=%d\n\n",R,G,B,R-B,R-G,B-G);
	}
}
void visor(int position){	
}
double eval_clasif(vector<pixel> entr , vector<pixel> test, double *v_error,int k=K, int kernel = KERNEL);
int clasif_vector(vector<pixel> entr, vector<pixel> test_pixel, int k=K, int kernel=KERNEL);

int main(){

	char buffer[200];
	char *p_buffer;
	vector<pixel> entrenamiento;
	int v;
	pixel pix_aux;

	CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
	if( !capture ) {
		fprintf( stderr, "ERROR: capture is NULL \n" );
		getchar();
		return -1;
	}
	IplImage* frame = cvQueryFrame( capture );
		
	cvNamedWindow( "Original", CV_WINDOW_AUTOSIZE );
	cvSetMouseCallback("Original",my_mouse_callback,(void*) frame);
	//cvCreateTrackbar("Foto","Original",&trackbar,18, visor);	


	uchar *data;
	int height,width;
	int step,channels;
	


	IplImage* img = cvCreateImage(cvGetSize(frame),8,1);;
	
	cvNamedWindow( "Clasificada", CV_WINDOW_AUTOSIZE );
	cvShowImage("Clasificada",img);
	
	cvSetMouseCallback("Clasificada",my_mouse_callback,(void*) frame);


	


	vector<pixel> test_pixel;
	pixel aux;
	

	// get the image data
	height    = frame->height;
	width     = frame->width;
	step	  = frame->widthStep;
	channels  = frame->nChannels;
	data      = (uchar *)frame->imageData;
	uchar *data2    =  (uchar *)img->imageData;
	int step2	  = img->widthStep;



	int key;

	int dif_v2_v0; //Rojo_Azul
	int dif_v2_v1; //Rojo_Verde
	int dif_v0_v1; //Azul_Verde
	int borde= int (KERNEL/2);
	int n_pix= KERNEL*KERNEL;

	while(1){
		
		frame = cvQueryFrame( capture );
		if( !frame ) {
			fprintf( stderr, "ERROR: frame is null...\n" );
			getchar();
			break;
		}


		/*for(int i= borde; i < height; i++){
			for(int j= borde; j < width; j++){ 

				aux.val0 = data[(i)*step+(j)*channels+0];
				aux.val1 = data[(i)*step+(j)*channels+1];
				aux.val2 = data[(i)*step+(j)*channels+2];

				dif_v2_v0 = aux.val2-aux.val0;
				dif_v2_v1 = aux.val2-aux.val1;
				dif_v0_v1 = aux.val0-aux.val1;

				if( (dif_v2_v0> 27 && dif_v2_v0<89)  && (dif_v2_v1>-2 && dif_v2_v1<78) && (dif_v0_v1>-55 && dif_v0_v1<0)  ){
					data2[i*step2+j]=255;
				}else {
					data2[i*step2+j]=0;
				}
			}
		}*/
		for(int i= borde; i < height; i++){
			for(int j= borde; j < width; j++){ 
				dif_v2_v0 = 0;
				dif_v2_v1 = 0;
				dif_v0_v1 = 0;
				
				for(int m = -1*borde; m <= borde; m++){
					for(int n = -1*borde; n <= borde; n++){

						aux.val0 = data[(i+m)*step+(j+n)*channels+0];
						aux.val1 = data[(i+m)*step+(j+n)*channels+1];
						aux.val2 = data[(i+m)*step+(j+n)*channels+2];

						dif_v2_v0 += aux.val2-aux.val0;
						dif_v2_v1 += aux.val2-aux.val1;
						dif_v0_v1 += aux.val0-aux.val1;
					
					}
				}

				dif_v2_v0 = int(dif_v2_v0/n_pix) ;
				dif_v2_v1 = int(dif_v2_v0/n_pix) ;
				dif_v0_v1 = int(dif_v2_v0/n_pix) ;

				if( (dif_v2_v0> 27 && dif_v2_v0<89)  && (dif_v2_v1>-2 && dif_v2_v1<78) && (dif_v0_v1>-55 && dif_v0_v1<50)  ){
					data2[i*step2+j]=255;
				}else {
					data2[i*step2+j]=0;
				}

			}
		}


		key=(cvWaitKey(10) & 255);
		cvShowImage("Original",frame);
		cvShowImage("Clasificada",img);
		if(key==27) return 0;
	}


	return 0;
}

