aboutsummaryrefslogtreecommitdiff
path: root/fly-tools/filter/main.cpp
blob: 4f78650cf5e2ae5102d382778d70478ccdf1dab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <sstream>
#include <fstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cvblob.h>

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;
}

int main(int argc, char* argv[]) {

  int c;
  int drawNumber = 2;

  double ratio = 0;

  bool verbose = false;
  bool ratioSet = false;

  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:hv")) != -1)
    switch (c) {
      case 'i':
        inputFileName = optarg;
        break;
      case 'o':
        outputFileName = optarg;
        break;
      case 'v':
        verbose = true;
        break;
      case 'r':
        ratioSet = true;
        ratio = atoi(optarg);
        break;
      case 'h':
        cout << usage << endl;
        exit(0);
        break;
      default:
        break;
    }

  if ( inputFileName.empty() || outputFileName.empty() || !ratioSet ) {
    cerr << usage << endl;
    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);
  grey = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);

  cvCvtColor(img, grey, CV_BGR2GRAY);

  // label blobs
  labelImg = cvCreateImage(cvGetSize(grey),IPL_DEPTH_LABEL,1);
  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 ) {
    *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) ) {
    *output << "the second largest blob is smaller than the ratio. only drawing largest blob" << endl;
    drawNumber = 1; 
  }

  for (int i=blobList.size()-drawNumber; i<(int)blobList.size(); i++) {
    largeBlobs.insert( CvLabelBlob(blobList[i].first, blobList[i].second) );
    *output << "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;
}