diff options
-rw-r--r-- | fly-tools/filter/main.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/fly-tools/filter/main.cpp b/fly-tools/filter/main.cpp index ec7899f..4f78650 100644 --- a/fly-tools/filter/main.cpp +++ b/fly-tools/filter/main.cpp @@ -1,4 +1,6 @@ #include <iostream> +#include <sstream> +#include <fstream> #include <opencv/cv.h> #include <opencv/highgui.h> #include <cvblob.h> @@ -6,6 +8,9 @@ using namespace std; using namespace cvb; +ofstream nullLog; +ostream* output; + bool cmpArea(const pair<CvLabel, CvBlob*> &p1, const pair<CvLabel, CvBlob*> &p2) { return p1.second->area < p2.second->area; @@ -16,13 +21,13 @@ int main(int argc, char* argv[]) { int c; int drawNumber = 2; - double ratio; - - unsigned int result; + double ratio = 0; - bool ratioSet; + bool verbose = false; + bool ratioSet = false; string usage = "filter-mask -i <input-file> -o <output-file> -r <ratio>"; + string inputFileName; string outputFileName; @@ -36,7 +41,7 @@ int main(int argc, char* argv[]) { vector< pair<CvLabel, CvBlob*> > blobList; - while ((c = getopt (argc, argv, "i:o:r:h")) != -1) + while ((c = getopt (argc, argv, "i:o:r:hv")) != -1) switch (c) { case 'i': inputFileName = optarg; @@ -44,6 +49,9 @@ int main(int argc, char* argv[]) { case 'o': outputFileName = optarg; break; + case 'v': + verbose = true; + break; case 'r': ratioSet = true; ratio = atoi(optarg); @@ -61,15 +69,25 @@ int main(int argc, char* argv[]) { exit(1); } + if(verbose) { + output = &cout; + } else { + output = &nullLog; + } + + *output << "Verbose logging enabled" << endl; + + // read input file img = cvLoadImage(inputFileName.c_str(), 1); - imgOut = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvZero(imgOut); + imgOut = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvZero(imgOut); grey = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvCvtColor(img, grey, CV_BGR2GRAY); + // label blobs labelImg = cvCreateImage(cvGetSize(grey),IPL_DEPTH_LABEL,1); - result = cvLabel(grey, labelImg, blobs); + cvLabel(grey, labelImg, blobs); // copy and sort blobs copy(blobs.begin(), blobs.end(), back_inserter(blobList)); @@ -77,26 +95,26 @@ int main(int argc, char* argv[]) { // if no blobs. if(blobList.size() == 0) { - cerr << "No blobs found." << endl; + cerr << "No blobs found" << endl; cvSaveImage(outputFileName.c_str(), imgOut); exit(1); } // if only one blob is detected. if(blobList.size() == 1 ) { - cout << "Only one blob found!" << endl; + *output << "Only one blob found" << endl; drawNumber = 1; } // if the ratio of the of the two blobs is smaller than the input ratio. if( ((double)blobList[blobList.size()-drawNumber].second->area / (double)blobList[blobList.size()-1].second->area) < (1/ratio) ) { - cout << "the second largest blob is smaller than the ratio. only drawing largest blob!" << endl; + *output << "the second largest blob is smaller than the ratio. only drawing largest blob" << endl; drawNumber = 1; } - for (int i=blobList.size()-drawNumber; i<blobList.size(); i++) { + for (int i=blobList.size()-drawNumber; i<(int)blobList.size(); i++) { largeBlobs.insert( CvLabelBlob(blobList[i].first, blobList[i].second) ); - cout << "Blob #" << blobList[i].first << " -> " << (*blobList[i].second) << endl; + *output << "Blob #" << blobList[i].first << " -> " << (*blobList[i].second) << endl; } // draw the selected blobsto imgOut and write the image. |