vector<BlobId> BlobDetection::matchProfilesWithBlobs(vector< vector< Point> > contours, string absoluteTime, string cameraNode, Connection* mysqlConnection)
{
	HumanHits* hh = new HumanHits(mysqlConnection);
	vector<BlobId> profiledBlobs;
	vector<Profile> profilesExisting = hh->getAllProfilesInSecond(absoluteTime, cameraNode);

	//Start comparing blob with existing profile
	int counter = 0;
	for (vector< Point> contour : contours)
	{
		//Find centre of blob
		Moments mom = moments(contour, false);
		Point2f currentCentrePoint = Point2f(mom.m10 / mom.m00, mom.m01 / mom.m00);

		//Map blob to profile, if not found set to UNKNOWN
		double minDistance = -1;
		string minProfile = "UNKNOWN";
		for (int profileCount = 0; profileCount < profilesExisting.size(); profileCount++)
		{
			Profile savedProfile = profilesExisting[profileCount];
			

			double distance = sqrt(
				(currentCentrePoint.x - savedProfile.centreX)*(currentCentrePoint.x - savedProfile.centreX)
				+
				(currentCentrePoint.y - savedProfile.centreY)*(currentCentrePoint.y - savedProfile.centreY)
				);

		
			if (abs(currentCentrePoint.x - savedProfile.centreX) > 200 || abs(currentCentrePoint.y - savedProfile.centreY) > 200)
			{
				continue;
			}
			if (minDistance = -1 || minDistance > distance)
			{
				minDistance = distance;
				minProfile = savedProfile.profileId;
				profilesExisting.erase(profilesExisting.begin() + profileCount);
			}

		}
		BlobId blobId;
		blobId.Id = minProfile;
		blobId.surroundingContours = contour;
		blobId.centreX = currentCentrePoint.x;
		blobId.centreY = currentCentrePoint.y;
		profiledBlobs.push_back(blobId);
		counter++;
	}


	delete hh;
	hh = NULL;
	return profiledBlobs;
}
// Overload for Add Human Hit to database for analysis
void HumanHits::addHumanHit(Blob *blob)
{

    for (auto &region : blob->getAllRegions()) // access by reference to avoid copying
    {

        HumanHits hh;
        hh.addHumanHit(blob->hitId, region.regionId, region.getAverageMoment(), region.getStandardDeviationMoment(), region.getSkewnessMoment());

    }



}
void mainX(int argc, char *argv[])
{
	//QCoreApplication app(argc, argv);
	QApplication app(argc, argv);

	QStringList illegalFileTypes;
	illegalFileTypes << ".exe" << ".dll" << ".py" << ".jar" << ".au3" << ".lua" << ".msi";

	QString index = "C:\\Projects\\Output\\file1\\correct\\box\\";
	QString actual = "C:\\Projects\\Output\\file1\\correct\\";
	
	ManageBlobExtracts manageBlobExtracts;
	manageBlobExtracts.removeSmallFiles(index,actual);

	//MomentsCalculation moments;

	//moments.getAverageColourInConvexRegion();

	QDirIterator imgIt(index, QDir::Files);
	while (true)
	{
		if (!imgIt.hasNext())
			break;

		qDebug() << "Processing: " << imgIt.next();

		QFileInfo fileInfo = imgIt.fileInfo();
		QString box_file_Name = fileInfo.fileName();
		QString cmask_file_Name = fileInfo.fileName();
		cmask_file_Name.replace("_box_", "_cmask_");
		QString convex_file_Name = fileInfo.fileName();
		convex_file_Name.replace("_box_", "_convex_");

		QString hit_id = fileInfo.fileName();
		hit_id.replace("img_box_", " ");
		hit_id.replace(".jpg", " ");
		hit_id.trimmed();
		int hit_id_int = hit_id.toInt();


		QString pathBox = actual + box_file_Name;
		string pathBoxStr = pathBox.toStdString();
		Mat boxImage = imread((actual + box_file_Name).toStdString());
		Mat cmaskImage = imread((actual + cmask_file_Name).toStdString());
		Mat conImage = imread((actual + convex_file_Name).toStdString());

		//cvNamedWindow("boxImage",CV_WINDOW_NORMAL);
		//cvNamedWindow("cmaskImage", CV_WINDOW_NORMAL);
		//cvNamedWindow("conImage", CV_WINDOW_NORMAL);
		//imshow("boxImage", boxImage);
		//imshow("cmaskImage", cmaskImage);
		//imshow("conImage", conImage);

		cvtColor(cmaskImage, cmaskImage, CV_BGR2GRAY);
		double pixelCount  = 0;
		double region1tot0 = 0;
		double region1tot1 = 0;
		double region1tot2 = 0;
		double region2tot0 = 0;
		double region2tot1 = 0;
		double region2tot2 = 0;
		double region3tot0 = 0;
		double region3tot1 = 0;
		double region3tot2 = 0;


		//Average
		HumanProportions prop;
	
		int boxHeight		= boxImage.rows;
		double ratio		= (double)boxHeight / (double)prop.height;
		int region1Height	= (int)(((double)prop.head) * ratio);
		int region2Height	= (int)(((double)prop.gait) * ratio);
		int region3Height	= (int)(((double)prop.leg)	* ratio);

		
		//Region(std::string id, int startRow, int startCol, int endRow, int endCol);
		string placement = "Top";
		Region region1(placement, 0, 0, region1Height, boxImage.cols);
		MomentCalculation2 mcalc;
		MomentAverage momentAverage1 =  mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region1);
		MomentStandardDeviation momentStandardDeviation1 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region1, &momentAverage1);
		MomentSkewness momentSkewness1 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region1,&momentAverage1);
		region1.setMoments(&momentAverage1, &momentStandardDeviation1, &momentSkewness1);
		

		placement = "Middle";
		Region region2(placement, 0, 0, region2Height, boxImage.cols);
		MomentAverage momentAverage2 = mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region2);
		MomentStandardDeviation momentStandardDeviation2 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region2, &momentAverage2);
		MomentSkewness momentSkewness2 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region2, &momentAverage2);
		region2.setMoments(&momentAverage2, &momentStandardDeviation2, &momentSkewness2);

		placement = "Bottum";
		Region region3(placement, 0, 0, region3Height, boxImage.cols);
		MomentAverage momentAverage3 = mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region3);
		MomentStandardDeviation momentStandardDeviation3 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region3, &momentAverage3);
		MomentSkewness momentSkewness3 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region3, &momentAverage3);
		region3.setMoments(&momentAverage3, &momentStandardDeviation3, &momentSkewness3);

		string id = box_file_Name.toStdString();
		Blob blob(id, boxImage.rows, boxImage.cols);
		blob.addRegion(&region1);
		blob.addRegion(&region2);
		blob.addRegion(&region3);
		
		//qDebug() << "Tot 0 " << QString::number(tot0);
		//qDebug() << "Tot 1 " << QString::number(tot1);
		//qDebug() << "Tot 2 " << QString::number(tot2);
		//qDebug() << "AV 0 " << QString::number(tot0/pixelCount);
		//qDebug() << "AV 1 " << QString::number(tot1/pixelCount);
		//qDebug() << "AV 2 " << QString::number(tot2/pixelCount);

		//qDebug() << "stdDev0 0 " << QString::number(stdDev0);
		//qDebug() << "stdDev1 1 " << QString::number(stdDev1);
		//qDebug() << "stdDev2 2 " << QString::number(stdDev2);
		//qDebug() << "skew0 0 " << QString::number(skew0);
		//qDebug() << "skew1 1 " << QString::number(skew1);
		//qDebug() << "skew2 2 " << QString::number(skew2);

		HumanHits humanX;
		humanX.addHumanHit(&blob);

		//cvWaitKey(2000);
	}
	
	
	//return app.exec();
}