aboutsummaryrefslogtreecommitdiff
path: root/fly-tools/background/main.cpp
blob: b01305e57c9bc287156ee78c5600e03723759ecc (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>

#define round(x)((int)((x)+0.5))
#define array(i,j,k,l) (array[height*width*i + width*j + k + l])

int nImages;
unsigned long height = 0;
unsigned long width = 0;

int findmax(long *p, int n) {
  int mx = 0, v = p[0];
  for (int i = 1; i < n; i++) {
    if (p[i] > v) {
      v = p[i];
      mx = i;
    }
  }
  return mx;
}

using namespace std;
using namespace cv;

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

  string usage = "derive-background -i <input-list> -o <output-filename>";
  string output_file;
  string inputFileName;
  int c;

  while ((c = getopt (argc, argv, "i:o:h")) != -1)
    switch (c) {
      case 'i':
        inputFileName = optarg;
        break;
      case 'o':
        output_file = optarg;
        break;
      case 'h':
        cout << usage << endl;
        exit(EXIT_FAILURE);
        break;
      default:
        break;
    }

  if( inputFileName.empty() || output_file.empty() ) {
    cout << usage << endl;
    exit(EXIT_FAILURE);
  }

  // Okay Declare all our working variables here.

  IplImage *first_image = NULL;
  IplImage *output_image = NULL;

  int i,j, k;
  int height = 0;
  int width = 0;

  string filename;

  // open first image in input_file to deterimine the height and width
  ifstream input_file(inputFileName.c_str());

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


  // Read first line for our first image and rewind
  input_file>>filename; 

  first_image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_UNCHANGED);

  if(!first_image) {
    cerr << "couldn't read first image" << endl;
    exit(1);
  } 

  height = first_image->height;
  width = first_image->width;

  cvReleaseImage(&first_image);

  if (height == 0 || width == 0) {
    cerr << "height or width = 0!" << endl;
    exit(1);
  }

  cout << "height: " << height << " width: " << width << endl;

  // count number of images we have in the file

  input_file.clear();
  input_file.seekg(0);
  while(input_file>>filename) {
    nImages++;
  }

  cout << "number of images: " << nImages << endl;

  // initialize the storage arrays
  long *array = (long *)malloc(height*width*256*3*sizeof(long));
  if(array == NULL) {
    cerr << "could not allocate the proper memory, sorry!" << endl;
    exit(EXIT_FAILURE);
  }

  input_file.clear();
  input_file.seekg(0);

  while (input_file>>filename) {

    IplImage *input_image = NULL;
    input_image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_UNCHANGED);

    cout << "reading: " << filename << endl;

    for (i=0; i<height; i++) {
      for (j=0; j<width; j++) {
        CvScalar s;
        s = cvGet2D(input_image, i, j);

        int r = s.val[2];
        int g = s.val[1];
        int b = s.val[0];

        array(r,i,j,0) += 1;
        array(g,i,j,1) += 1;
        array(b,i,j,2) += 1; 
      }
    }
    cvReleaseImage(&input_image);
  }

  // calculate histograms
  output_image = cvCreateImage(cvSize(width,height),IPL_DEPTH_32F,3);

  for (j = 0; j < height; j++) {
    for (k = 0; k < width; k++) {

      CvScalar s;

      long red_histogram[255] = { 0 };
      long blue_histogram[255] = { 0 };
      long green_histogram[255] = { 0 };

      for (i = 0; i < 256; i++) {
        red_histogram[i] += array(i,j,k,0);
        green_histogram[i] += array(i,j,k,1);
        blue_histogram[i] += array(i,j,k,2);
      }

      int red_val = findmax(red_histogram, 255);
      int blue_val = findmax(blue_histogram, 255);
      int green_val = findmax(green_histogram, 255);

      s.val[2] = red_val;
      s.val[1] = blue_val; 
      s.val[0] = green_val; 

      cvSet2D(output_image, j, k, s);
    }
  }

  if(!cvSaveImage(output_file.c_str(), output_image)) {
    cerr << "Could not save "<< output_file << endl;
    exit(EXIT_FAILURE);
  }

  return EXIT_SUCCESS;
}