/*
 * main.cpp
 *
 *  Created on: 21/ott/2013
 *      Author: eridia
 */

//this program performe the histogram equalization of the input image




#include <opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <string>
#include <iostream>
#include <cstdlib>
//#include "opencv2/core/core.hpp"


int main (int argc, char *argv[])
{



	Mat image = imread("mare2.jpg"),input; // read image
	Mat image1 = imread("carAHE.jpg"),carAHE; // read image


	cvtColor( image, input, CV_BGR2GRAY );

	int height = input.rows;
	int width =input.cols;


	Mat output(height,width, CV_8UC1,Scalar(255));

	Mat output1(height,width, CV_8UC1,Scalar(255)); //equalized image
	Mat output3(height,width, CV_8UC1,Scalar(255));//equalized histogram image

	Mat output2(height,width, CV_32FC1);
	Mat output_eq(height,width, CV_8UC1,Scalar(255));



	double funzioneF[256];

	double histogram[256];
	double histogram_rescale[256];

	double histogramEq[256];
	double histogram_rescale2[256];

//--------IMMAGINE EQUALIZZATA------------------------

   equalizeHist(input,output1);



/*
   //--------------------compute histogram--------------------------------


   	for (int k=0; k<256; k++)              //scorro asse k istogramma
   	{
   		int count=0;

   		for(int x=0;x<input.rows;x++)         //scorro la matrice della mia immagine
   		{
   			for(int y=0;y<input.cols;y++)

   			{
   				if(input.at<unsigned char>(x,y) == k)
   				{
   					count=count +1;
   				}

   			}


   			histogram[k]= double(count);


   		}
   		histogram[k]=(double) (count)/(width*height); //istogramma con valori compresi fra 0 e 1 (il massimo è circa 0.33)

   	}



*/


 //--------------------CALCOLO L'ISTOGRAMMA-dell'immagine originale--------------------------------


	for (int k=0; k<256; k++)              //scorro asse k istogramma
	{
		int count=0;

		for(int x=0;x<input.rows;x++)         //scorro la matrice della mia immagine
		{
			for(int y=0;y<input.cols;y++)

			{
				if(input.at<unsigned char>(x,y) == k)
				{
					count=count +1;
				}

			}


			histogram[k]= double(count);


		}
		histogram[k]= double(count)/(width*height); //istogramma con valori compresi fra 0 e 1 (il massimo è circa 0.33)

	}


	//--------------------CALCOLO L'ISTOGRAMMA equalizzata-opencv--------------------------------


		for (int k=0; k<256; k++)              //scorro asse k istogramma
		{
			int count=0;

			for(int x=0;x<output1.rows;x++)         //scorro la matrice della mia immagine
			{
				for(int y=0;y<output1.cols;y++)

				{
					if(output1.at<unsigned char>(x,y) == k)
					{
						count=count +1;
					}

				}


				histogramEq[k]= double(count);


			}
			histogramEq[k]= double(count)/(width*height); //istogramma con valori compresi fra 0 e 1 (il massimo è circa 0.33)

		}



//--------------Rescale of histogram ...just for show on video
	for (int k=0; k<256; k++)
		{
			histogram_rescale[k]=histogram[k]*(width*height)/100;

		}


//--------------draw histogram--------------------

	for (int t=0;t<256; t++)

	{
		int inizio= output.rows-histogram_rescale[t];


			for(int x= inizio; x< output.rows; x++)
			{
				output.at<unsigned char>(x,t)= 0;
			}
	}


	//--------------Rescale histogram-------------------------------
		for (int k=0; k<256; k++)
			{
				histogram_rescale2[k]=histogramEq[k]*(width*height)/100;

			}


	//--------------draw equalized histogram --------------------

		for (int t=0;t<256; t++)

		{
			int inizio= output3.rows-histogram_rescale2[t];


				for(int x= inizio; x< output3.rows; x++)
				{
					output3.at<unsigned char>(x,t)= 0;
				}
		}






	namedWindow( "immagine originale", WINDOW_AUTOSIZE );// Create a window for display
	imshow("immagine originale", input);
	imwrite("originale.jpg",input);

	namedWindow( "istogramma imm originale", WINDOW_AUTOSIZE );// Create a window for display
	imshow("istogramma imm originale", output);
	imwrite("istogramma imm originale.jpg",output);

	namedWindow( "immagine equalizzata", WINDOW_AUTOSIZE );// Create a window for display
	imshow("immagine equalizzata", output1);
	imwrite("immagine equalizzata.jpg",output1);

	namedWindow( "istogramma equalizzato", WINDOW_AUTOSIZE );// Create a window for display
	imshow("istogramma equalizzato", output3);
	imwrite("istogramma equalizzato.jpg",output3);


/*


//------------------division in cells and CLAHE--------
	int x,y;   //indici dei blocchi!saltano di blocco in blocco e nn di pixel in pixel
	int height1=64;
	int width1=64; //dimensioni del blocco di interesse

	Mat outputClahe(height,width, CV_8UC1,Scalar(255)); //immagine delll'istogramma normale

	Mat inputCell=input(Rect(x,y,width1,height1)); //rect divido in celle di dimensione 64x64
	Mat outputCell= outputClahe(Rect(x,y,width1,height1));

	for (int x=0;x<input.rows; x=x+64)

			{
					for(int y=0; x< input.cols; y=y+64)
					{
						  equalizeHist(input(Rect(x,y,width1,height1)),outputClahe(Rect(x,y,width1,height1)));

					}

			}

	Mat m= imread("mare2.jpg",CV_LOAD_IMAGE_GRAYSCALE); //input image
	imshow("mare2_GRAYSCALE",m);

	Ptr<CLAHE> clahe = createCLAHE();
	clahe->setClipLimit(4);

	Mat dst;
	clahe->apply(m,dst);
	imshow("mare_CLAHE",dst);

*/


	waitKey(0);

}





