From 7bf74b986e4408c76136a1add6f25bb2266ede03 Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Thu, 27 Sep 2012 18:14:08 -0400 Subject: convert to OpenCV and C++ initial commit --- fly-tools/background/main.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 fly-tools/background/main.cpp (limited to 'fly-tools/background') diff --git a/fly-tools/background/main.cpp b/fly-tools/background/main.cpp new file mode 100644 index 0000000..2c9fd60 --- /dev/null +++ b/fly-tools/background/main.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include + +#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 (uint8_t *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 -o "; + 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); + } + + IplImage *first_image, *input_image, *output_image; + + int i,j, k; + int image = 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; + // input_file.seekg (0, ios::beg); + + cout << filename << endl; + first_image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_UNCHANGED); + + if(!first_image) { + cerr << "couldn't read first image" << endl; + exit(1); + } + // Get our height and width + int height = first_image->height; + int width = first_image->width; + + 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 + uint8_t * array = (uint8_t *)malloc(nImages*height*width*3*sizeof(uint8_t)); + 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) { + + input_image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_UNCHANGED); + + cout << "Image number " << image << "Filename " << filename << endl; + + for (j=0; j