aboutsummaryrefslogtreecommitdiff
path: root/fly-tools/fly-tracker/main.cpp
blob: 7be88e81ce21289d4e4ac47f8500d1dc2c713e42 (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
133
#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;

  bool verbose = false;

  string usage = "fly-tracker -i <input-list> -o <output-folder> -t <type> -n <number>";

  string inputFileName;
  string outputFolderName;

  int setNumber;
  bool setNumberSet;
  string setType;

  vector< pair<CvLabel, CvBlob*> > blobList;

  while ((c = getopt (argc, argv, "i:o:t:n:hv")) != -1)
    switch (c) {
      case 'i':
        inputFileName = optarg;
        break;
      case 'o':
        outputFolderName = optarg;
        break;
      case 'v':
        verbose = true;
        break;
      case 't':
        setType = optarg;
        break;
      case 'n':
        setNumberSet = true;
        setNumber = atoi(optarg);
        break;
      case 'h':
        cout << usage << endl;
        exit(EXIT_SUCCESS);
        break;
      default:
        break;
    }

  if ( inputFileName.empty() || outputFolderName.empty() || !setNumberSet || setType.empty() ) {
    cerr << usage << endl;
    exit(EXIT_FAILURE);
  }	

  (verbose) ? output = &cout : output = &nullLog; 

  *output << "Verbose logging enabled" << endl;

  // read input file

  ifstream inputFile(inputFileName.c_str());

  if (inputFile.fail() ) {
    cerr << "cannot open the input file that contains name of the input images\n";
    exit(EXIT_FAILURE);
  }

  string fileName;
  long int fileNumber = 0;
  long int totalFrames = 0;
  long int togetherFrames = 0;

  bool togetherState = false;

  while (inputFile>>fileName) {

    totalFrames += 1;

    IplImage *inputImg, *outputImg, *labelImg;
    inputImg = cvLoadImage(fileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE);

    CvBlobs blobs;

    vector< pair<CvLabel, CvBlob*> > blobList;

    // label blobs
    labelImg = cvCreateImage(cvGetSize(inputImg),IPL_DEPTH_LABEL,1);

    cvLabel(inputImg, labelImg, blobs);

    // copy and sort blobs
    copy(blobs.begin(), blobs.end(), back_inserter(blobList));
    sort(blobList.begin(), blobList.end(), cmpArea);

    if (blobList.size() == 1) {
      togetherState = true;
      togetherFrames += 1;
    } else {  
      togetherState = false;
    }

    // List detected blob in each file
    *output << "File: " << fileName << endl;
    *output << "\t Together:" << togetherState << endl;
    for (int i=0; i<(int)blobList.size(); i++) {
    *output << "\t Blob #" << blobList[i].first << " -> " << (*blobList[i].second) << endl;
    }

    // Release all the memory
    cvReleaseImage(&labelImg);
    cvReleaseImage(&inputImg);
    cvReleaseBlobs(blobs);

  }

    cout << "Final Information" << endl;
    cout << "\t Total Number of frames in video: " << totalFrames << endl; 
    cout << "\t Number of frames where there flies are together: " << togetherFrames << endl; 
    cout << "\t Percent of video where there flies are together: " << (long double)togetherFrames / totalFrames << endl; 
  return 0;
}