From 25ba5b17f86b76c1c99f6d09942362b13dcb18f8 Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Mon, 19 Nov 2012 14:23:44 -0500 Subject: Clean ups left and right, Use proper exit codes, remove a useless header, alphabetize the function lists, concat declarations and more --- fly-tools/FlyTrackingMain.cpp | 258 ++++++++++++++++++------------------------ 1 file changed, 107 insertions(+), 151 deletions(-) diff --git a/fly-tools/FlyTrackingMain.cpp b/fly-tools/FlyTrackingMain.cpp index 5dd94f7..30dc3db 100644 --- a/fly-tools/FlyTrackingMain.cpp +++ b/fly-tools/FlyTrackingMain.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -21,14 +20,28 @@ using namespace Magick; using namespace std; +// One of these output streams will be used. If directed towards null, nothing happens, towards output, it will be printed ofstream nullLog; ostream* output; +// Our output files for debugging and information +ofstream foutLPS; +ofstream foutSt; +ofstream foutDebugCen; +ofstream foutDebugSpeed; + + const double PI = atan(1.0)*4.0; const double FACTOR_EIGEN = 100; const int STUCKING_TO_A_SINGLE_BLOB = 1; const int SEPARATING_FROM_SINGLE_BLOB = 2; +bool isInFemaleBlob; +int maskImageHeight; +int maskImageWidth; +int diagLength; + +vector > bresenhamLine; Image* residual; vector fOVector; @@ -60,12 +73,12 @@ int sequenceSize=1; int startOfAOneObject = -1; int endOfAOneObject = -1; -vector fnVector; -string inputFileName; - +// This decides if we want to output an overlay. You'll get the same results +// either way but you won't be able to verify it. taken as an argument. bool writeFinalImages = false; // GLOBAL PATHS +string inputFileName; string maskImagePath; string origImagePath; string finalOutputPath; @@ -73,6 +86,7 @@ string outputFilePrefix; vector > velocityDirectionsF; vector > velocityDirectionsS; +vector fnVector; pair avgVelocityF; pair avgVelocityS; @@ -83,6 +97,7 @@ pair overAllVelocityS; vector > evDirectionF; vector > evDirectionS; +// Information about frames that will be written out at the end of proessing. int totalMaleLookingAtFemale = 0; int totalFemaleLookingAtMale = 0; int totalSingleBlob = 0; @@ -93,7 +108,6 @@ map centroidDistanceMap; map headDirAngleMap; map speedMap; - void initSequence(){ startOfATrackSequence = -1; endOfATrackSequence = -1; @@ -125,8 +139,7 @@ void bubbleSort(vector & fov) { } } -void writeHist(const char* filename, map dataMap) -{ +void writeHist(const char* filename, map dataMap) { *output << "In the beginning of the write hist" << endl; *output << "dataMap size " << dataMap.size() << endl; @@ -143,7 +156,6 @@ void writeHist(const char* filename, map dataMap) back = dataMap.end(); back--; - unsigned int first = front->first, last = back->first; *output << "Min: " << first << " " << "Max: " << last << " " << "Count: " << last-first << endl; vector hist(last+1, 0); @@ -174,24 +186,22 @@ void writeHist(const char* filename, map dataMap) } -void findObj(Image* img, int x, int y, vector > & shape ,bool eightCon=true, bool colorLookingFor=true); -void eightConnObj(Image* img, int x, int y, vector > & obj, bool color=true); -void fourConnObj(Image* img, int x, int y, vector > & obj, bool color=true); +double calculateDotProduct(pair v, pair eV); +void calculateHeadVector(FlyObject fO, pair &headDirection); vector covariantDecomposition(vector > & points); -pair getCentroid(vector > & points); -bool isInterface(Image* orig, unsigned int x, unsigned int y); -void writeFrameImage(int fn, string imS); +void determineHeadDirection(int fileCounter); void drawTheFlyObject(FrameInfo currentFI, string fileName, int isFirst, bool singleBlob=false,bool unprocessed = false); void drawTheSequence(int startIndex, int endIndex, int isFirst, bool singleBlob = false, bool unprocessed = false); +void eightConnObj(Image* img, int x, int y, vector > & obj, bool color=true); double euclideanDist(FlyObject a, FlyObject b); +void findObj(Image* img, int x, int y, vector > & shape ,bool eightCon=true, bool colorLookingFor=true); +void fourConnObj(Image* img, int x, int y, vector > & obj, bool color=true); +pair getCentroid(vector > & points); +bool isInterface(Image* orig, unsigned int x, unsigned int y); bool identifyFlyObjectFromFrameToFrame(FrameInfo prevFI, FrameInfo& currentFI, bool gotRidOfSingleBlob=false) ; int roundT(double v) {return int(v+0.5);} -void determineHeadDirection(int fileCounter); - - void normalizeVector(pair &a); -double calculateDotProduct(pair v, pair eV); -void calculateHeadVector(FlyObject fO, pair &headDirection); +void writeFrameImage(int fn, string imS); void normalizeVector(pair &a) { double temp = a.first*a.first + a.second*a.second; @@ -632,10 +642,7 @@ if (inWhite) } }*/ -bool isInFemaleBlob; -int maskImageHeight; -int maskImageWidth; -vector > bresenhamLine; + void putPixel(Image* maskImage, int x, int y) { @@ -969,7 +976,7 @@ int draw_line_bm(Image* maskImage, int x0, int y0, int x1, int y1) { default: *output << "No octant which should be a bug\n"; - exit(0); + exit(EXIT_FAILURE); break; } @@ -977,14 +984,20 @@ int draw_line_bm(Image* maskImage, int x0, int y0, int x1, int y1) { } -double euclideanDist(pair newLocation, pair initLocation) { - +inline double euclideanDist(pair newLocation, pair initLocation) { double temp = pow((newLocation.first - initLocation.first), 2.0) + pow((newLocation.second - initLocation.second), 2.0); temp = sqrt(temp); return temp; +} + +inline double getSpeed(pair vector) { + double value = vector.first*vector.first + vector.second*vector.second; + value = sqrt(value); + return value; } + int sequenceCondition(FrameInfo prevFI,FrameInfo currentFI) { bool prevFIsSingleBlob = prevFI.getIsSingleBlob(); bool currentFIsSingleBlob = currentFI.getIsSingleBlob(); @@ -1022,7 +1035,6 @@ void drawTheSequence(int startIndex, int endIndex, int isFirst, bool singleBlob, } - void objectHeadDirection(FlyObject prevFO, FlyObject ¤tFO) { // take the head direction from the previous frame @@ -1200,11 +1212,9 @@ void velocityDirection(int st, int end, pair &velDirectionF, pa pair cFFCentroid = cFFirstFO.getCentroid(); pair cFSCentroid = cFSecondFO.getCentroid(); - pair pFFCentroid = pFFirstFO.getCentroid(); pair pFSCentroid = pFSecondFO.getCentroid(); - int velXFirst = cFFCentroid.first - pFFCentroid.first; int velYFirst = cFFCentroid.second - pFFCentroid.second; @@ -1220,23 +1230,13 @@ void velocityDirection(int st, int end, pair &velDirectionF, pa velDirectionF = cFVV; velDirectionS = cSVV; - - } -double getSpeed(pair vector) { - double value = vector.first*vector.first + vector.second*vector.second; - value = sqrt(value); - - return value; - -} void velocityDirections(int stIndex, int endIndex) { - velocityDirectionsF.clear(); velocityDirectionsS.clear(); int i = 0; @@ -1309,7 +1309,6 @@ void velocityDirections(int stIndex, int endIndex) { } -ofstream foutLPS; void largestIncreasingPositiveDotProductSeq(vector > velocityDirs, int &startIndex, int &endIndex) { int positiveVelSeqSize = 0; @@ -1347,8 +1346,6 @@ void largestIncreasingPositiveDotProductSeq(vector > veloci //*output << "end "< cFOVector = currentFI.getFOVector(); FlyObject cFFO = cFOVector[object-1]; - pair cFVV = cFFO.getVelocityV(); + //pair cFVV = cFFO.getVelocityV(); //*output << "Velocity before normalization "< tempVelocity = cFSecondFO.getVelocityV(); + //pair tempVelocity = cFSecondFO.getVelocityV(); //*output << "Velocity was "< tempFOV; // to find the new head direction - bool currentlyCalculatingHead = true; + //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); @@ -1997,7 +1973,7 @@ int main(int argc, char **argv) // residual image is initialized with black representing not visited. residual = new Image(buffer, "black"); - //*output<<"reading file "< 0 ) { - // *output << "size of the object is: " << s < eigenVal = covariantDecomposition(shape); - { //objCount++; @@ -2028,14 +2002,11 @@ int main(int argc, char **argv) pair (eigenVal[4], eigenVal[5]), 0.0); tempFOV.push_back(tempFO); - } } - } } - delete img; delete residual; @@ -2045,10 +2016,8 @@ int main(int argc, char **argv) fOVector.clear(); for (int ti=0; ti > foundShape; + vector > shape; + + Image *image, *mask; + cout << "Segmented image "<columns(); - int height = image->rows(); + width = image->columns(); + height = image->rows(); - char buffer[100]; sprintf(buffer,"%ix%i",width,height); // the residual image should be newed residual = new Image(buffer, "black"); - bool found = false; - vector > foundShape; - vector > shape; *output<<"Detecting the male object for finding the start point"< eigenVal = covariantDecomposition(foundShape); - int x0, y0, x1, y1; - - x0 = cen_x; - y0 = cen_y; - - double ev_x; - double ev_y; - if (eVDirection == true) { ev_x = static_cast (x0) + static_cast (diagLength)*eigenVal[4]; ev_y = static_cast (y0) + static_cast (diagLength)*eigenVal[5]; @@ -2367,19 +2336,19 @@ void findTheStartPoint(string fileName, int desiredSize, int otherSize, int cen_ *output<<"Endpoint: centroid (x0,y0)==("< point = foundShape[i]; - maskImage->pixelColor(point.first, point.second,"white"); + mask->pixelColor(point.first, point.second,"white"); } - /*int hits= */ draw_line_bm(maskImage, x1, y1, x0, y0); + /*int hits= */ draw_line_bm(mask, x1, y1, x0, y0); - //maskImage->strokeColor("red"); - //maskImage->draw(DrawableLine(x1, y1, x0, y0)); - //maskImage->write("test.png"); + //mask->strokeColor("red"); + //mask->draw(DrawableLine(x1, y1, x0, y0)); + //mask->write("test.png"); *output<<"BresenhamLine size is "< 0) { pair temp = bresenhamLine[bresenhamLine.size()-1]; *output << "Finding the starting point: Hits source at "<pixelColor(temp.first, temp.second); + ColorMono c = mask->pixelColor(temp.first, temp.second); //maleSP_x = prev_x; //maleSP_y = prev_y; @@ -2403,7 +2372,7 @@ void findTheStartPoint(string fileName, int desiredSize, int otherSize, int cen_ } else { isFoundStartPoint = false; - *output<<"The object is at the border in the mask image. BresenhamLine vector is empty. Size : "< 0) { pair temp = bresenhamLine[bresenhamLine.size()-1]; *output << "Finding the starting point: Hits source at "<pixelColor(temp.first, temp.second); + ColorMono c = mask->pixelColor(temp.first, temp.second); //maleSP_x = prev_x; //maleSP_y = prev_y; if (c.mono() == true) { *output << "start point from the source object should be black"<strokeColor("blue"); pair velocityV = currentFO.getVelocityV(); ev_x = static_cast(centroid.first) + 30.0 * velocityV.first; ev_y = static_cast(centroid.second) + 30.0 * velocityV.second; img->draw(DrawableLine( centroid.first, centroid.second, static_cast(ev_x), static_cast(ev_y) )); - - // draw the historical head vector img->strokeColor("white"); pair headV = currentFO.getHead(); @@ -2717,16 +2683,13 @@ void drawTheFlyObject(FrameInfo currentFI, string fileName, int isFirst, bool si ev_y = static_cast (centroid.second) + 25.0*headV.second; img->draw( DrawableLine(centroid.first, centroid.second, static_cast (ev_x), static_cast (ev_y)) ); - //draw the object tracking circle if (n == isFirst and n==0) { *output << "Tracking the n = "<strokeColor("yellow"); img->draw(DrawableCircle(centroid.first, centroid.second, centroid.first+5, centroid.second)); img->pixelColor(prev_x, prev_y, "red"); - } else if ( n == isFirst and n==1) { - *output << "Tracking the "<strokeColor("yellow"); img->fillColor("none"); @@ -2734,7 +2697,6 @@ void drawTheFlyObject(FrameInfo currentFI, string fileName, int isFirst, bool si img->draw(DrawableCircle(centroid.first, centroid.second, centroid.first+5, centroid.second)); } - } } } @@ -2743,11 +2705,9 @@ void drawTheFlyObject(FrameInfo currentFI, string fileName, int isFirst, bool si img->write(outputFileName.c_str()); delete img; } - - // when do not want to identify on the original comment below line and uncomment the above one - delete maskImage; + // TODO: Wtf is going on here? if (isHitting == 1 || isHittingFemaleToMale == 0) calculateStatistics(currentFI, fileName, isFirst, singleBlob, true, false, unprocessed); else if (isHitting == 0 || isHittingFemaleToMale == 1) @@ -2760,10 +2720,8 @@ void drawTheFlyObject(FrameInfo currentFI, string fileName, int isFirst, bool si } -void findObj(Image* img, int x, int y, vector > & shape ,bool eightCon, bool colorLookingFor) -{ +void findObj(Image* img, int x, int y, vector > & shape ,bool eightCon, bool colorLookingFor) { assert(residual != NULL); - if (eightCon == true) eightConnObj(img, x, y, shape, colorLookingFor); else { @@ -2772,8 +2730,7 @@ void findObj(Image* img, int x, int y, vector > & shape ,bool eigh } -void fourConnObj(Image* img, int x, int y, vector > & obj, bool color) -{ +void fourConnObj(Image* img, int x, int y, vector > & obj, bool color) { int width = img->columns(),height = img->rows(); // boundary violation check @@ -2813,8 +2770,7 @@ void fourConnObj(Image* img, int x, int y, vector > & obj, bool c } -void eightConnObj(Image* img, int x, int y, vector > & obj, bool color) -{ +void eightConnObj(Image* img, int x, int y, vector > & obj, bool color) { int width = img->columns(),height = img->rows(); // boundary violation check -- cgit v1.2.1