#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "FrameInfo.h" using namespace Magick; using namespace std; 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); vector covariantDecomposition(vector > & points); pair getCentroid(vector > & points); bool isInterface(Image* orig, unsigned int x, unsigned int y); void writeFrameImage(int fn, string imS); int roundT(double v) {return int(v+0.5);} const double PI = atan(1.0)*4.0; const double FACTOR_EIGEN = 100; Image* residual; ostream &operator<<(ostream &out, FlyObject & fO) { fO.output(out); return out; } ostream &operator<<(ostream &out, FrameInfo & fI) { fI.output(out); return out; } vector > > shapeVectors; vector > shape; vector > sizeNIndexVector; void bubbleSort() { for(int i=1; i a = sizeNIndexVector[j]; pair b = sizeNIndexVector[j+1]; if (a.first < b.first) { pair c = sizeNIndexVector[j]; sizeNIndexVector[j] = sizeNIndexVector[j+1]; sizeNIndexVector[j+1] = c; } } } } void fillResidualWithObj(vector > & obj, ColorRGB c) { for (unsigned int i = 0; ipixelColor(obj[i].first, obj[i].second, c); } void writeHist(const char* filename, map & len) { map::iterator front = len.begin(), back = len.end(); back--; unsigned int first = front->first, last = back->first; /*if (cutoff != -1 && cutoff < int(last)) last = cutoff; */ cout << "Min: " << first << endl << "Max: " << last << endl << "Count: " << last-first << endl; //vector hist(last-first, 0); vector hist(last+1, 0); cout << "hist size: " << hist.size() << endl; try{ for(unsigned int j = 0; j= int(hist.size()) ) hist.resize(j-first,0); hist[roundT(j-first)] = len[j]; */ /*if ( roundT(j) >= int(hist.size()) ) hist.resize(j,0); hist[roundT(j)] = len[j]; */ hist[j] = len[j]; } } catch (...) { cerr << "Bad histogram bucketing" << endl; } /*if ( (cutoff >= 0) && (cutoff " << endl; // input file contains name of the // input image files return -1; } MagickCore::SetMagickResourceLimit(MagickCore::MemoryResource, 1536); MagickCore::SetMagickResourceLimit(MagickCore::MapResource, 2048); string fileName; ifstream inputFile(argv[1]); if (inputFile.fail() ) { cout << "cannot open the input file that contains name of the input images\n"; exit(1); } // get the output file name along with the location from argv[4] string outputFileLocation(argv[4]); string outputFileName = outputFileLocation + "final/outputFile.txt"; ofstream outputFile(outputFileName.c_str()); // get the location of the input mask file string inputMaskFileLocation(argv[3]); int frameCounter = 0; // use 15, 20 or 10 // 15 found to be working correct double ratioSecondLargestToLargest = atof(argv[2]); cout << "Ratio is 1/"<columns(),height = img->rows(); Image* imgForFilter; sprintf(buffer,"%ix%i",width,height); // residual image is initialized with black representing not visited. residual = new Image(buffer, "black"); imgForFilter = new Image(buffer, "white"); cout << "Reading "< 0) cout << "black object size is "<pixelColor(shape[i].first, shape[i].second, "black"); } // store the intermediate file in temp folder under the output location // string oFilteredFileName = "output/filtered/temp/"+outputFile; string oFilteredFileName = outputFileLocation +"temp/"+savedFileName; cout << "Saving the filtered image "<< oFilteredFileName<write(oFilteredFileName.c_str()); delete residual; /* residual = new Image(buffer, "black"); shapeVectors.clear(); sizeNIndexVector.clear(); // find the two largest object int objectCounter = 0; for (int x=0; x0) { shapeVectors.push_back(shape); cout << "new object pushed back at position "< si(s, objectCounter); sizeNIndexVector.push_back(si); objectCounter++; } } } // sort the sizes and take the largest to find average largest cout<<"shapeVectors size = "<(sizeNIndexVector[0].first); cout << "Current total size is "< (totalSize/frameCounter); cout << "Average largest size is "<>fileName) { //string inputFileName = "output/filtered/temp/"+fileName; string inputFileName = outputFileLocation + "temp/"+fileName; Image* img = new Image(inputFileName.c_str()); int width = img->columns(); int height = img->rows(); outputFile<<"File name is "< 0) { shapeVectors.push_back(shape); cout << "New object found at position ("< si(s, objectCounter); sizeNIndexVector.push_back(si); objectCounter++; } } } cout << "Total object found "<(sizeNIndexVector[0].first); double secondLargest = 0; double ratio = 0; if (sizeNIndexVector.size() > 1) { secondLargest = static_cast(sizeNIndexVector[1].first); ratio = secondLargest/currentLargestSize; cout << "Ratio is "<pixelColor(shapeVectors[ sizeNIndexVector[n].second ][i].first, shapeVectors[ sizeNIndexVector[n].second ][i].second, "white"); } } //string finalImageName = "output/filtered/final/"+fileName; string finalImageName = outputFileLocation + "final/"+fileName; imgFinal->write( finalImageName.c_str() ); // writing the single in red if (numberOfObjects == 1) { Image* singleObjectFinal = new Image(buffer, "black"); int totalPoints = sizeNIndexVector[0].first; cout << "Output the single object of size = "<pixelColor(shapeVectors[ sizeNIndexVector[0].second ][i].first, shapeVectors[ sizeNIndexVector[0].second ][i].second, "red"); } //string singleImageName = "output/filtered/single/"+fileName; string singleImageName = outputFileLocation + "single/"+fileName; singleObjectFinal->write(singleImageName.c_str()); delete singleObjectFinal; } outputFile<<"----------------------------------------------------\n"; delete img; delete residual; delete imgFinal; } inputFile.close(); outputFile.close(); return 0; } 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 { fourConnObj(img, x, y, shape, colorLookingFor); } } int barrier = 1000; void fourConnObj(Image* img, int x, int y, vector > & obj, bool color) { int width = img->columns(),height = img->rows(); // boundary violation check if ( (x >= (width)) || (x < 0) || (y >= (height) ) || (y < 0) ) return; // residualpixel.mono() == true implies it is visited. Otherwise not visited. ColorMono residualpixel = ColorMono(residual->pixelColor(x,y)); // originalpixel.mono() == true implies it is an object pixel. Otherwise it is blank region pixel. ColorMono originalpixel = ColorMono(img->pixelColor(x,y)); // If the current pixel is already visited then return if (residualpixel.mono() == true) return; // Else if current pixel is not visited and it is black, which means it is not an object pixel; so return else if (residualpixel.mono() == false && originalpixel.mono() != color) return; // If current pixel is not visited and its value is white, which means a new object is found. else if (residualpixel.mono() == false && originalpixel.mono() == color) { // Save the coordinates of the current pixel into the vector and make the pixel visited in the residual image pair p; p.first = x; p.second = y; obj.push_back(p); // if (obj.size() > barrier) { // cout<pixelColor(x,y, ColorMono(true)); // Recursively call all of it's eight neighbours. fourConnObj(img, x+1, y, obj, color); fourConnObj(img, x, y-1, obj, color); fourConnObj(img, x-1, y, obj, color); fourConnObj(img, x, y+1, obj, color); } } void eightConnObj(Image* img, int x, int y, vector > & obj, bool color) { int width = img->columns(),height = img->rows(); // boundary violation check if ( (x >= (width)) || (x < 0) || (y >= (height) ) || (y < 0) ) return; // residualpixel.mono() == true implies it is visited. Otherwise not visited. ColorMono residualpixel = ColorMono(residual->pixelColor(x,y)); // originalpixel.mono() == true implies it is an object pixel. Otherwise it is blank region pixel. ColorMono originalpixel = ColorMono(img->pixelColor(x,y)); // If the current pixel is already visited then return if (residualpixel.mono() == true) return; // Else if current pixel is not visited and it is black, which means it is not an object pixel; so return else if (residualpixel.mono() == false && originalpixel.mono() != color) return; // If current pixel is not visited and its value is white, which means a new object is found. else if (residualpixel.mono() == false && originalpixel.mono() == color) { // Save the coordinates of the current pixel into the vector and make the pixel visited in the residual image pair p; p.first = x; p.second = y; obj.push_back(p); // if (obj.size() > barrier) { // //cout<pixelColor(x,y, ColorMono(true)); // Recursively call all of it's eight neighbours. eightConnObj(img, x+1, y, obj, color); eightConnObj(img, x+1, y-1, obj, color); eightConnObj(img, x, y-1, obj, color); eightConnObj(img, x-1, y-1, obj, color); eightConnObj(img, x-1, y, obj, color); eightConnObj(img, x-1, y+1, obj, color); eightConnObj(img, x, y+1, obj, color); eightConnObj(img, x+1, y+1, obj, color); } } // Aspect Ratio pair getCentroid(vector > & points) { pair centroid; centroid.first = 0; centroid.second = 0; for (unsigned int i = 0; i covariantDecomposition(vector > & points) { unsigned int i,j,k; pair centroid = getCentroid(points); vector retval; gsl_matrix* matrice = gsl_matrix_alloc(2, 2); double sumX2 = 0, sumXY = 0, sumY2 = 0; for (k = 0; ksize; i++) retval.push_back(gsl_vector_get(eigenVal, i)); for (j = 0; jsize2; j++) for (i = 0; isize1; i++) retval.push_back(gsl_matrix_get(eigenVec, i, j)); retval.push_back(static_cast(centroid.first)); retval.push_back(static_cast (centroid.second)); // for (i=0; i<2; i++) { // gsl_vector_view evec_i = gsl_matrix_column (eigenVec, i); // //printf ("eigenvalue = %g\n", eval_i); // cout<<"eigenvector = \n"; // gsl_vector_fprintf (stdout, &evec_i.vector, "%g"); // } gsl_vector_free(eigenVal); gsl_matrix_free(matrice); gsl_matrix_free(eigenVec); return retval; } // isInterface for binary image bool isInterface(Image* orig, unsigned int x, unsigned int y) { // Get the current pixel's color ColorMono currentpixel = (ColorMono)orig->pixelColor(x,y); // If the current pixel is black pixel then it is not boundary pixel // error check if (currentpixel.mono() == false) return false; // If the current pixel is not black then it is white. So, now we need // to check whether any four of its neighbor pixels (left, top, right, // bottom ) is black. If any of this neighbor is black then current // pixel is a neighbor pixel. Otherwise current pixel is not neighbor // pixel. ColorMono leftneighborpixel = (ColorMono)orig->pixelColor(x-1,y); ColorMono topneighborpixel = (ColorMono)orig->pixelColor(x,y-1); ColorMono rightneighborpixel = (ColorMono)orig->pixelColor(x+1,y); ColorMono bottomneighborpixel = (ColorMono)orig->pixelColor(x,y+1); // If leftneighborpixel is black and currentpixel is white then it is // boundary pixel if ( leftneighborpixel.mono() != currentpixel.mono()) return true; // If topneighborpixel is black and currentpixel is white then it is // boundary pixel else if (topneighborpixel.mono() != currentpixel.mono()) return true; // If rightneighborpixel is black and currentpixel is white then it // is boundary pixel else if (rightneighborpixel.mono() != currentpixel.mono()) return true; // If bottomneighborpixel is black and currentpixel is white then it // is boundary pixel else if (bottomneighborpixel.mono() != currentpixel.mono()) return true; // Else all of its neighbor pixels are white so it can not be a // boundary pixel else return false; }