aboutsummaryrefslogtreecommitdiff
path: root/fly-tools
diff options
context:
space:
mode:
authormutantturkey <mutantturke@gmail.com>2012-08-02 18:45:09 -0400
committermutantturkey <mutantturke@gmail.com>2012-08-02 18:45:09 -0400
commitd0e43aba2d491ba0b28a29c59205e611debfa2e2 (patch)
tree2fe8a6258497e14a449f3aad8c9acee1cfe0a77d /fly-tools
parentf91318923bf5ebb3a1aa51313ed149a8cf2d221b (diff)
inital blob filtering using opencv and cvBlob commit
Diffstat (limited to 'fly-tools')
-rw-r--r--fly-tools/filter/main.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/fly-tools/filter/main.cpp b/fly-tools/filter/main.cpp
new file mode 100644
index 0000000..6cfc5aa
--- /dev/null
+++ b/fly-tools/filter/main.cpp
@@ -0,0 +1,118 @@
+#include <iostream>
+#include <opencv/cv.h>
+#include <opencv/highgui.h>
+#include <cvblob.h>
+
+using namespace std;
+using namespace cvb;
+
+bool cmpArea(const pair<CvLabel, CvBlob*> &p1, const pair<CvLabel, CvBlob*> &p2)
+{
+ return p1.second->area < p2.second->area;
+}
+
+int main(int argc, char* argv[]) {
+
+ int c;
+ int drawNumber = 2;
+ double ratio;
+
+ unsigned int result;
+
+ bool ratioSet;
+
+ string usage = "filter-mask -i <input-file> -o <output-file> -r <ratio>";
+ string inputFileName;
+ string outputFileName;
+
+ CvBlobs blobs;
+ CvBlobs largeBlobs;
+
+ IplImage *img;
+ IplImage *imgOut;
+ IplImage *grey;
+ IplImage *labelImg;
+
+ vector< pair<CvLabel, CvBlob*> > blobList;
+
+ while ((c = getopt (argc, argv, "i:o:r:h")) != -1)
+ switch (c) {
+ case 'i':
+ inputFileName = optarg;
+ break;
+ case 'o':
+ outputFileName = optarg;
+ break;
+ case 'r':
+ ratioSet = true;
+ ratio = atoi(optarg);
+ break;
+ case 'h':
+ cout << usage << endl;
+ exit(1);
+ break;
+ default:
+ break;
+ }
+
+ if ( inputFileName.empty() || outputFileName.empty() || !ratioSet ) {
+ cerr << usage << endl;
+ exit(1);
+ }
+
+ img = cvLoadImage(inputFileName.c_str(), 1);
+ imgOut = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvZero(imgOut);
+
+ grey = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
+
+ cvCvtColor(img, grey, CV_BGR2GRAY);
+
+ labelImg = cvCreateImage(cvGetSize(grey),IPL_DEPTH_LABEL,1);
+ result = cvLabel(grey, labelImg, blobs);
+
+ // copy and sort blobs
+ copy(blobs.begin(), blobs.end(), back_inserter(blobList));
+ sort(blobList.begin(), blobList.end(), cmpArea);
+
+ // if no blobs.
+ if(blobList.size() == 0) {
+ 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;
+ drawNumber = 1;
+ }
+
+ // if the ratio of the of the two blobs is smaller than the input ratio.
+ if( ((double)blobList[blobList.size()-2].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;
+ drawNumber = 1;
+ }
+
+ cout << "blob area 1: " << (double)blobList[blobList.size()-2].second->area << endl;
+ cout << "blob area 2: " << (double)blobList[blobList.size()-1].second->area << endl;
+ cout << "blobs ratio:" << ((double)blobList[blobList.size()-2].second->area / (double)blobList[blobList.size()-1].second->area)<< endl;
+ cout << "input ratio:" << (1/ratio ) << endl;
+
+ for (int i=blobList.size()-drawNumber; i<blobList.size(); i++) {
+ largeBlobs.insert( CvLabelBlob(blobList[i].first, blobList[i].second) );
+ cout << "Blob #" << blobList[i].first << " -> " << (*blobList[i].second) << endl;
+ }
+
+ // draw the selected blobsto imgOut and write the image.
+ cvFilterLabels(labelImg, imgOut, largeBlobs);
+ cvSaveImage(outputFileName.c_str(), imgOut);
+
+ // Release all the memory
+ cvReleaseImage(&imgOut);
+ cvReleaseImage(&grey);
+ cvReleaseImage(&labelImg);
+ cvReleaseImage(&img);
+ cvReleaseBlobs(blobs);
+
+ return 0;
+}