From bda4a425da1073ca38b7ccf006d02f65b25a4755 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Wed, 14 May 2014 15:23:05 -0400 Subject: add changes for histogramming --- fly-tools/FlyTrackingMain.cpp | 907 ++++++++++++++++++++++-------------------- fly-tools/Makefile | 4 +- 2 files changed, 486 insertions(+), 425 deletions(-) diff --git a/fly-tools/FlyTrackingMain.cpp b/fly-tools/FlyTrackingMain.cpp index 30dc3db..d44d546 100644 --- a/fly-tools/FlyTrackingMain.cpp +++ b/fly-tools/FlyTrackingMain.cpp @@ -8,12 +8,13 @@ #include #include -#include +#include #include #include #include #include +#include #include "FrameInfo.h" @@ -139,7 +140,48 @@ void bubbleSort(vector & fov) { } } -void writeHist(const char* filename, map dataMap) { + +void getMeanStdDev(map dataMap, double *mean, double *standardDev) { + // mean = 1/N*(sum(i*H_i)) for i = 0 to M-1 + double sumOfValues = 0.0; + double i = 0.0; + double N = 0.0; + double M = 0.0; + + vector currentHistogramValues; + + for(i = 0; i < dataMap.size(); i++) { + sumOfValues = sumOfValues + i*dataMap[i]; + N += dataMap[i]; + + cout << "sum:" << sumOfValues << endl; + cout << "N:" << N << endl; + cout << "i:" << i << endl; + } + + // mean + *mean = sumOfValues/N; + double lmean = 0; + lmean = sumOfValues/N; + cout << "mean:" << *mean << endl; + cout << "mean:" << lmean << " = " << sumOfValues << "/" << N << endl; + + // sigma^2 = (sum( (i-mean)^2*H_i ) )/(N-1) for i = 0 to M-1 + *standardDev = 0.0; + double sumSquaredResults = 0.0; + int j = 0; + for ( i = 0.0; i < dataMap.size(); i++) { + sumSquaredResults += pow((i-*mean), 2.0)*dataMap[j]; + *output << "sumsqres:" << sumSquaredResults << endl; + j++; + } + + // standard deviation + *standardDev = sumSquaredResults/(N-1); + *standardDev = sqrt(*standardDev); +} + +double writeHist(const char* filename, map dataMap) { *output << "In the beginning of the write hist" << endl; *output << "dataMap size " << dataMap.size() << endl; @@ -148,7 +190,7 @@ void writeHist(const char* filename, map dataMap) { ofstream fout(filename); fout <<"No entry in the histogram and size is " << dataMap.size() << endl; fout.close(); - return; + return 0; } @@ -183,7 +225,8 @@ void writeHist(const char* filename, map dataMap) { catch (...) { cerr << "Bad memory loc for opening file" << endl; } - + + return 0; } double calculateDotProduct(pair v, pair eV); @@ -1839,465 +1882,130 @@ void processASequence(int startOfATrackSequence, int endOfATrackSequence) { } -int main(int argc, char **argv) { - - int c; - bool verbose = false; - string usage = "Usage: FlyTracking -i -o -f -m -p "; - opterr = 0; - while ((c = getopt (argc, argv, "i:f:m:p:o:hxv")) != -1) - switch (c) { - case 'i': - inputFileName = optarg; - break; - case 'o': - origImagePath = optarg; - break; - case 'f': - finalOutputPath = optarg; - break; - case 'm': - maskImagePath = optarg; - break; - case 'p': - outputFilePrefix = optarg; - break; - case 'h': - cout << usage << endl; - exit(EXIT_FAILURE); - break; - case 'x': - writeFinalImages = true; - break; - case 'v': - verbose = true; - break; - default: - break; - } +// is set each time the start is found +// useful when one blob is detected on the border of the mask image +bool isFoundStartPoint = false; +int hitTheFly(Image* maskImage, int &intersectX, int &intersectY) { - (verbose) ? output = &cout : output = &nullLog; + //Image* maskImage = new Image(fileName.c_str()); - *output << "verbose logging out" << endl; + for (int i=bresenhamLine.size()-1; i>=0; i--) { - if( inputFileName.empty() || origImagePath.empty() || finalOutputPath.empty() || maskImagePath.empty() || outputFilePrefix.empty() ) { - cerr << usage << endl; - cerr << "input name: " << inputFileName << endl; - cerr << "original path: " << origImagePath << endl; - cerr << "output path: " << finalOutputPath << endl; - cerr << "mask path: " << maskImagePath << endl; - cerr << "output prefix: " << outputFilePrefix << endl; - exit(EXIT_FAILURE); - } + pair tempP = bresenhamLine[i]; - string fileName; - ifstream inputFile(inputFileName.c_str()); + int x = tempP.first; - if (inputFile.fail() ) { - cerr << "cannot open the input file that contains name of the input images\n"; - exit(EXIT_FAILURE); - } + int y = tempP.second; - string statFileName = finalOutputPath + outputFilePrefix + "_statFile.txt"; - foutSt.open(statFileName.c_str()); + ColorMono currPixelColor = ColorMono(maskImage->pixelColor(x,y)); - if (foutSt.fail()) { - cerr<<"cannot open the statfile"< > shape; - vector tempFOV; + int width = 0; + int height = 0; - // to find the new head direction - //bool currentlyCalculatingHead = true; + int x0, y0, x1, y1; - while (inputFile>>fileName) { + x0 = cen_x; + y0 = cen_y; - int fi = fileName.find("_"); - // Be aware that this limits us to sample size of 99,999 (55.55 minutes) - // current sequence numbers spans from 0 - 18019, so 5 digits are needed - int span = 5; - string tempString = fileName.substr(fi+1,span); - int frameCounter = atoi(tempString.c_str()); - //*output << frameCounter<columns(),height = img->rows(); - diagLength= static_cast ( sqrt( (height*height) + (width*width) ) ); + vector > foundShape; + vector > shape; - //*output << "Diagonal length is "<columns(); + height = image->rows(); - if ( s > 0 ) - { - // *output << "size of the object is: " << s < eigenVal = covariantDecomposition(shape); - { - //objCount++; + sprintf(buffer,"%ix%i",width,height); - double velocity_x=0.0, velocity_y=0.0; - // save the object information - FlyObject tempFO(s, - pair (eigenVal[6], eigenVal[7]), - pair (eigenVal[4], eigenVal[5]), - pair (velocity_x, velocity_y), - false, - pair (eigenVal[4], eigenVal[5]), - 0.0); - tempFOV.push_back(tempFO); - } - } - } - } + // the residual image should be newed + residual = new Image(buffer, "black"); - delete img; - delete residual; + *output<<"Detecting the male object for finding the start point"< 0 ) + { - for (int ti=0; ti tmpCentroid = getCentroid(shape); + foutDebugCen<<"tmpCentroid.first, tmpCentroid.second = ("< 1) { + pair point = shape[l]; + *output << "point.first, point.second "<= 15 to be a single blob state, so pass false parameter. - drawTheSequence(startOfAOneObject, endOfAOneObject,0, true, false); - startOfAOneObject = -1; - endOfAOneObject = -1; - //startOfATrackSequence = 1; - sequenceSize = 1; - } - - - if(seqCond == STUCKING_TO_A_SINGLE_BLOB) { - *output << "StartIndex "< 15 when the size of the sequence is > 16. - if ((endOfATrackSequence - startOfATrackSequence) >=15 ) { - processASequence(startOfATrackSequence, endOfATrackSequence); - *output << "Done processing"< 15 ) { - - *output << "Last sequence that does not stick to a single blob status startIndex "<(totalMaleLookingAtFemale+totalFemaleLookingAtMale)/static_cast(totalSeparated); - percentageLookingAt *= 100.0; - - double percentageSingleBlob = static_cast(totalSingleBlob)/static_cast(fileCounter); - percentageSingleBlob *= 100.0; - - foutSt<<"Total number of single blob "<=0; i--) { - - pair tempP = bresenhamLine[i]; - - int x = tempP.first; - - int y = tempP.second; - - ColorMono currPixelColor = ColorMono(maskImage->pixelColor(x,y)); - - if (currPixelColor.mono() == true) { - *output << "Hit the target fly"<<" "< > foundShape; - vector > shape; - - Image *image, *mask; - - cout << "Segmented image "<columns(); - height = image->rows(); - - sprintf(buffer,"%ix%i",width,height); - - // the residual image should be newed - residual = new Image(buffer, "black"); - - *output<<"Detecting the male object for finding the start point"< 0 ) - { - - *output << "size of the object is: " << s < tmpCentroid = getCentroid(shape); - foutDebugCen<<"tmpCentroid.first, tmpCentroid.second = ("< point = shape[l]; - *output << "point.first, point.second "< > shape; + vector tempFOV; + + // to find the new head direction + //bool currentlyCalculatingHead = true; + + while (inputFile>>fileName) { + + int fi = fileName.find("_"); + // Be aware that this limits us to sample size of 99,999 (55.55 minutes) + // current sequence numbers spans from 0 - 18019, so 5 digits are needed + int span = 5; + string tempString = fileName.substr(fi+1,span); + int frameCounter = atoi(tempString.c_str()); + //*output << frameCounter<columns(),height = img->rows(); + diagLength= static_cast ( sqrt( (height*height) + (width*width) ) ); + + //*output << "Diagonal length is "< 0 ) + { + // *output << "size of the object is: " << s < eigenVal = covariantDecomposition(shape); + { + //objCount++; + + double velocity_x=0.0, velocity_y=0.0; + // save the object information + FlyObject tempFO(s, + pair (eigenVal[6], eigenVal[7]), + pair (eigenVal[4], eigenVal[5]), + pair (velocity_x, velocity_y), + false, + pair (eigenVal[4], eigenVal[5]), + 0.0); + tempFOV.push_back(tempFO); + } + } + } + } + + delete img; + delete residual; + + // *output<<"Sorting the objects according to size"< 1) { + + FrameInfo prevFI = fIVector[fileCounter-1]; + + int seqCond = sequenceCondition(prevFI, currentFI); + + if (seqCond == STUCKING_TO_A_SINGLE_BLOB) { + + endOfATrackSequence = fileCounter-1; + + // save the index for printing the one object later on + startOfAOneObject = fileCounter; + + } else if (seqCond == SEPARATING_FROM_SINGLE_BLOB) { + + startOfATrackSequence = fileCounter; + + // draw the single blob sequence + endOfAOneObject = fileCounter - 1; + *output << "Only one object StartIndex "<= 15 to be a single blob state, so pass false parameter. + drawTheSequence(startOfAOneObject, endOfAOneObject,0, true, false); + startOfAOneObject = -1; + endOfAOneObject = -1; + //startOfATrackSequence = 1; + sequenceSize = 1; + } + + + if(seqCond == STUCKING_TO_A_SINGLE_BLOB) { + *output << "StartIndex "< 15 when the size of the sequence is > 16. + if ((endOfATrackSequence - startOfATrackSequence) >=15 ) { + processASequence(startOfATrackSequence, endOfATrackSequence); + *output << "Done processing"< 15 ) { + + *output << "Last sequence that does not stick to a single blob status startIndex "<(totalMaleLookingAtFemale+totalFemaleLookingAtMale)/static_cast(totalSeparated); + percentageLookingAt *= 100.0; + + double percentageSingleBlob = static_cast(totalSingleBlob)/static_cast(fileCounter); + percentageSingleBlob *= 100.0; + + foutSt<<"Total number of together "<