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/colortracker/main.cc

Go to the documentation of this file.
00001 /*
00002  * main.cc
00003  *  
00004  * Copyright (c) 2002 Machine Perception Laboratory 
00005  * University of California San Diego.
00006  *
00007  *
00008  * Authors: Ian Fasel, Bret Fortenberry
00009  * Fixes:
00010  * 
00011  * Please do not distribute source or binaries derived from the source
00012  * without explicit permission from UCSD's Machine Perception Laboratory
00013  * http://mplab.ucsd.edu. Library and code may be used for non-commercial
00014  * research, evaluations, or demonstrations. Please provide appropriate
00015  * acknowledgments.
00016  *
00017  */
00018 
00019 #include <iostream.h>
00020 #include <iomanip.h>
00021 #include "MPColorTracker.h"
00022 #include <list>
00023 #include <Magick++.h>
00024 extern "C" {
00025 #include <math.h>
00026 #include <time.h>
00027 #include <string.h>
00028 #include <sys/times.h>
00029 #include <stdlib.h>
00030 }
00031 
00032 #ifndef FALSE
00033 #define FALSE 0
00034 #endif
00035 #ifndef TRUE
00036 #define TRUE 1
00037 #endif
00038 
00039 using namespace Magick;
00040 
00041 class SingleShotColorSearch : public MPColorTracker {
00042 public:
00043   SingleShotColorSearch(char * filename): m_image(filename){
00044         try{
00045           //Image image(filename);
00046           //image.display();
00047           m_pixels = new RImage<RGBTRIPLE>(m_image.columns(), m_image.rows());
00048           m_image.write(0,0, m_image.columns(), m_image.rows(), "RGB", CharPixel, m_pixels->array);
00049         } catch ( ErrorFileOpen &error ) {
00050           cerr << "Error opening file: "
00051           <<  filename
00052           << ". ImageMagick said: "
00053           << error.what()
00054           << endl;
00055           return;
00056         }
00057         
00058         cout << "Image: " << filename
00059                 << " - " << m_pixels->width
00060                 << "x" << m_pixels->height
00061                 << endl;
00062 
00063         InitStreaming();
00064   }
00065   
00066   void Search(int frameCount){
00067                 PutImage(m_pixels->array, m_pixels->width, m_pixels->height, (long)frameCount);
00068                 MPIUpdate();
00069                 m_results = DoSearch();
00070         }
00071 
00072   void displayResults(){
00073                 
00074                 /*if(m_results) {
00075                         Image im2(m_pixels->width, m_pixels->height, "RGB", CharPixel, m_pixels->array);
00076         im2.display();
00077                 }*/
00078                         
00079                 FaceBoxList faces;
00080                 TSquare<double> box;
00081     cout << ".";
00082     if(m_results){
00083       //cout << endl << "Just got a Detect " << GetElapsedTime() << " ms ago" << endl;
00084       GetColorBox(box);
00085       cout << "Bounding Box is [" << box.x << "," << box.y << "; " << box.x+box.size << "," << box.y+box.size << "]" << endl;
00086         Image im2(m_pixels->width, m_pixels->height, "RGB", CharPixel, m_pixels->array);
00087                         im2.strokeColor("white");
00088       im2.strokeWidth(1);
00089       im2.fillColor("none" );
00090       im2.draw( DrawableRectangle(box.x, box.y, box.x+box.size, box.y+box.size) );
00091                         GetLastFaces(faces);
00092       if(faces.size() != 0) {
00093         im2.strokeColor("red");
00094         im2.strokeWidth(1);
00095         im2.fillColor("none" );
00096 
00097         while(!faces.empty( ))
00098         {
00099           Square face = faces.front();  
00100           faces.pop_front();
00101 
00102           im2.draw( DrawableRectangle(face.x, face.y, 
00103                 face.x + face.size, face.y + face.size));
00104 
00105         }
00106                         }                                               
00107       im2.display();
00108     }
00109   }
00110 
00111   ~SingleShotColorSearch(){ delete m_pixels; EndStreaming();};
00112 
00113 private:
00114   RImage<RGBTRIPLE> *m_pixels;
00115   Image m_image;
00116         int m_results;
00117 };
00118 #define MAX_NUM_IMAGES 20
00119 struct arguments {
00120   char *args[MAX_NUM_IMAGES];   // up to xx images can be processed at once
00121 };
00122 
00123 int main (int argc, char **argv)
00124 {  
00125         int i;
00126   struct arguments arguments;
00127   for(i = 0; i<MAX_NUM_IMAGES; i++)
00128     arguments.args[i] = 0;
00129     
00130   // get values from command line
00131   if(argc==2) {
00132         arguments.args[0] = (char *)malloc(256 * sizeof(char));
00133     strcpy(arguments.args[0],argv[1]);
00134         }
00135   else{
00136                 for (i = 0; i < argc-1 && i < MAX_NUM_IMAGES; i++) {
00137                         arguments.args[i] = (char *)malloc(256 * sizeof(char));
00138         strcpy(arguments.args[i],argv[i+1]);
00139                 }
00140   }
00141 
00142         int im = 0;
00143         while (arguments.args[im] && im < MAX_NUM_IMAGES) {
00144         cout << "instantiate SingleShotColorSearch for image " << arguments.args[im] << endl;
00145   
00146         SingleShotColorSearch s(arguments.args[im]);
00147 
00148         s.Search(0);
00149         struct tms tms_buf;
00150         clock_t start_time;
00151         double elapsed_time;
00152         // start the clock
00153     start_time = times(&tms_buf);
00154 
00155         // SEARCH 100 times!
00156         int NSearch = 20;
00157                 int showResults = 0;
00158         for(int i = 1; i< NSearch; i++){
00159         s.Search(i);
00160                         if (showResults > 5) {
00161                 s.displayResults();
00162                                 showResults = 0;
00163                         }
00164                         cout << ".";
00165                         showResults++;
00166         }
00167         cout << endl;
00168         elapsed_time = (double)(times(&tms_buf) - start_time) / 1000;
00169         cout << endl << NSearch/elapsed_time << " Frames per second" << endl;
00170         // stop the clock  
00171         printf("\tThe search took %g seconds. \n", elapsed_time);
00172                 im++;
00173         }
00174   
00175 }
00176 
00177 
00178 
00179 

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