The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Apps/unix/eyefinder/main.cc

Go to the documentation of this file.
00001 /*
00002  *  main.cc 
00003  *
00004  *  Authors: Ryan Dahl, Ian Fasel, Javier Movellan
00005  *  Fixes:
00006  *
00007  *  Copyright (c) 2003 Machine Perception Laboratory
00008  *  University of California San Diego.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00011  *
00012  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00013  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00014  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  */
00019 
00020 #include "eyefinderBinary.h"
00021 #include "faceobject.h"
00022 #include <list>
00023 #include <Magick++.h>
00024 #include <iostream>
00025 
00026 extern "C" {
00027 #include <math.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030 #include <stdlib.h>
00031 }
00032 
00033 #define FALSE 0
00034 #define TRUE 1 
00035 
00036 double difftv(timeval t1, timeval t0) {
00037         double s;
00038         double secs = t1.tv_sec - t0.tv_sec;
00039         double usecs = ((double)t1.tv_usec) - ((double)t0.tv_usec);
00040         usecs *= 1e-6;
00041         s = secs+usecs;
00042         return s;
00043 }
00044 
00045 using namespace Magick;
00046 
00047 #define MAX_NUM_IMAGES 20
00048 struct arguments {
00049   char *args[MAX_NUM_IMAGES];   // up to xx images can be processed at once
00050 };
00051 
00052 int main (int argc, char **argv)
00053 {  
00054         int i;
00055   struct arguments arguments;
00056   for(i = 0; i<MAX_NUM_IMAGES; i++)
00057     arguments.args[i] = 0;
00058     
00059   // get values from command line
00060   if(argc==2) {
00061         arguments.args[0] = (char *)malloc(256 * sizeof(char));
00062     strcpy(arguments.args[0],argv[1]);
00063         }
00064   else{
00065                 for (i = 0; i < argc-1 && i < MAX_NUM_IMAGES; i++) {
00066                         arguments.args[i] = (char *)malloc(256 * sizeof(char));
00067         strcpy(arguments.args[i],argv[i+1]);
00068                 }
00069   }
00070 
00071 
00072   struct timeval tv_now, last;
00073   MPEyeFinder *eyefinder;
00074   eyefinder = new MPEyeFinderBinary();
00075 
00076         int im = 0;
00077         while (arguments.args[im] && im < MAX_NUM_IMAGES) {
00078     // Try and open the file.  
00079     try { 
00080       Image image( arguments.args[im] );
00081 
00082       // 
00083       // allocate our buffer and write the pixels to it. 
00084       //
00085       RImage<float> pixels(image.columns(), image.rows());
00086                 eyefinder->initStream(pixels.width, pixels.height);
00087 
00088       image.quantizeColors ( 256 ); // convert to 256 colors
00089       image.quantize( ); // convert to grayscale
00090 
00091       image.write(0,0, image.columns(), image.rows(), "I", FloatPixel, pixels.array);
00092       //image.display();
00093 
00094       cout << "Image: " << arguments.args[im]
00095            << " - " << pixels.width 
00096            << "x" << pixels.height 
00097            << endl;
00098 
00099       VisualObject faces;
00100 
00101       // start the clock
00102       struct timeval tv_now, last;
00103       double numwindows;
00104       // Search nframes times, using ROI
00105       gettimeofday(&last,0);
00106       numwindows = eyefinder->findEyes(pixels, faces, 1.25, wt_avg);
00107       gettimeofday(&tv_now,0);
00108       printf("search took %g seconds. \n", difftv(tv_now, last));
00109       cout << "numwindows: " << numwindows << endl;
00110       
00111 
00112       if(faces.size() != 0) {
00113         image.strokeWidth(1);
00114         image.fillColor("none" );
00115                                 
00116         list<VisualObject *>::iterator face = faces.begin();
00117         int facenumber=0;
00118         for( ; face != faces.end(); ++face) {
00119           image.strokeColor("white");
00120           FaceObject *fo = static_cast<FaceObject*>(*face);
00121           image.draw( DrawableRectangle( fo->x, fo->y, fo->x+fo->xSize, fo->y+fo->ySize));
00122           image.strokeColor("red");
00123           int eyeSize = static_cast<int>(fo->xSize * 0.1);
00124           //lefteye
00125           image.draw( DrawableRectangle( max(fo->eyes.xLeft-eyeSize, 0),  max(fo->eyes.yLeft-eyeSize,0),
00126                                          min(fo->eyes.xLeft+eyeSize, pixels.width), min(fo->eyes.yLeft+eyeSize, pixels.height)));
00127           //righteye
00128           image.draw( DrawableRectangle( max(fo->eyes.xRight-eyeSize, 0),  max(fo->eyes.yRight-eyeSize,0),min(fo->eyes.xRight+eyeSize, pixels.width), min(fo->eyes.yRight+eyeSize, pixels.height)));
00129           
00130           Image tmpimage = image;
00131           tmpimage.crop(Geometry((int) fo->xSize, (int) fo->ySize , (int) fo->x, (int) fo->y));
00132           tmpimage.scale(Geometry(200,200,0,0));
00133           char outfilename[20];
00134           printf("Processing Face %d\n",facenumber);
00135           sprintf(outfilename,"face%d.jpg",++facenumber);
00136           tmpimage.write(outfilename); 
00137           
00138         }
00139         image.display();
00140       }
00141     }
00142     catch ( ErrorFileOpen &error ) {
00143       cerr << "Error opening file: " 
00144            <<  arguments.args[im]
00145            << ". ImageMagick said: " 
00146            << error.what() 
00147            << endl;
00148     }
00149                 im++;
00150   } 
00151   return 0;
00152 }
00153 

Generated on Mon Nov 8 17:07:41 2004 for MPT by  doxygen 1.3.9.1