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

mac/build/mpisearch.framework/Versions/Current/Headers/faceboxlist.h

Go to the documentation of this file.
00001 /* 
00002  *  FACEBOXLIST.h
00003  *
00004  *  Created by Ian Fasel on Feb 02, 2003.
00005  *  Fixes: 
00006  * 
00007  *  Copyright (c) 2003 Machine Perception Laboratory 
00008  *  University of California San Diego.
00009  * 
00010  * Please read the disclaimer and notes about redistribution 
00011  * at the end of this file.
00012  *  
00013  */
00014 
00015 #ifndef _FACEBOXLIST_H_
00016 #define _FACEBOXLIST_H_
00017 
00018 #include "square.h"
00019 #include <algorithm>
00020 #include <list>
00021 #include <math.h>
00022 #include <iostream>
00023 
00024 using namespace std;
00025 
00026 class avg_object {
00027  public:
00028   float  l, r, t, b; // left, right, top, bottom
00029   int  nL, nR, nT, nB, nScale; // number of contributing boxes
00030   float  scale;
00031   bool normalized;
00032 
00033   avg_object();
00034   avg_object(const avg_object &v);
00035   ~avg_object();
00036   avg_object operator+(const avg_object& v) const;
00037   avg_object & operator+=(const avg_object &v);
00038   void normalize();
00039   void unnormalize();  
00040 };
00041 
00042 template< class T >
00043 class ObjectList{
00044   typedef typename list<TSquare< T > >::iterator TSquare_iter;
00045  public:
00046   ObjectList() {};
00047   ObjectList(ObjectList &thelist):objects(thelist.objects){};
00048   ~ObjectList() {};
00049 
00050   void simplify(float tol);
00051   inline int size() const {return objects.size();}
00052   inline bool empty() const {return objects.empty(); }
00053   inline void clear() {objects.clear();}
00054   inline void sort() {objects.sort();}
00055   inline void erase(TSquare_iter &object){objects.erase(object);}
00056   inline void push_back(const TSquare< T >  &object){objects.push_back(object);}
00057   inline void push_front(const TSquare< T >  &object){objects.push_front(object);}
00058   inline void pop_front(){objects.pop_front();}
00059   inline TSquare_iter begin() { return objects.begin(); }
00060   inline TSquare_iter end() { return objects.end(); }
00061   inline TSquare< T >  front() const { return objects.front(); }
00062   inline TSquare< T >  back() const { return objects.back(); }
00063 
00064   list< TSquare< T > > objects;
00065   float  overlap(avg_object &a, avg_object &b);
00066 };
00067 
00068 typedef ObjectList<int> FaceBoxList;
00069 
00070 #ifdef WIN32
00071 #include <windows.h>
00072 #else
00073 template <class T, class U>
00074 static inline T max(T x, U y){ return static_cast<T>(x > y ? x : y);}
00075 template <class T, class U>
00076 static inline T min(T x, U y){ return static_cast<T>(x < y ? x : y);}
00077 #endif
00078 
00079 template< class T >
00080 float ObjectList<T>::overlap(avg_object &a, avg_object &b) {
00081   float  x = max(0,min(a.r,b.r)-max(a.l,b.l));
00082   float  y = max(0,min(a.b,b.b)-max(a.t,b.t));
00083   float  intersection = x*y;
00084   float  total = (a.r-a.l)*(a.b-a.t) + (b.r-b.l)*(b.b-b.t);
00085   float  unin = total-intersection; // note: 'union' is reserved
00086   float  p = intersection/unin;
00087   return(p);
00088 }
00089 
00090 template< class T >
00091 void ObjectList<T>::simplify (float tol)
00092 {
00093   list<avg_object>* origObjects;
00094   avg_object tmp;
00095 
00096   if(objects.size() > 1){
00097     int n = 0;
00098     //
00099     // First, put objects into convenient form
00100     //
00101     TSquare<T> f;
00102     origObjects = new list<avg_object>;
00103     while(objects.size() > 0) {
00104       f = objects.front();
00105       //cout << "Simplifying from (x,y,scale,size): (" << f.x << ", " << f.y << ", " << f.scale<<", " << f.size << ")" << endl;
00106       tmp.l = f.x;
00107       tmp.r = f.x + f.size;
00108       tmp.t = f.y;
00109       tmp.b = f.y + f.size;
00110       //tmp.n = 1;
00111       tmp.scale = f.scale;
00112       origObjects->push_back(tmp);  // append the copy
00113       objects.pop_front();
00114     }
00115 
00116     //
00117     // Now, iterate through boxes and combine those that overlap with first
00118     // box.  Make a new list containing originals and combined boxes.  Now
00119     // repeat, using this new list in place of the original, until all boxes
00120     // have been checked with all other boxes.
00121     //
00122     list<avg_object> *newObjects;
00123     list<avg_object> *overlappingObjects;
00124     //float  collapsed;
00125     for(int i = 0; i < origObjects->size(); i++){
00126       newObjects = new list<avg_object>;
00127       overlappingObjects = new list<avg_object>;
00128 
00129       //
00130       // store the first object in the overlapping list.
00131       //
00132       overlappingObjects->push_back(origObjects->front());
00133       origObjects->pop_front();
00134       //
00135       //        Loop through all boxes in list, and add them either to the new list,
00136       //  or the overlapping boxes list.
00137       //
00138       while ( origObjects->size() > 0 ) {
00139         float  o = overlap(overlappingObjects->front(),origObjects->front());
00140         if(o > tol)
00141           overlappingObjects->push_back(origObjects->front());
00142         else
00143           newObjects->push_back(origObjects->front());
00144         origObjects->pop_front();
00145       }
00146 
00147 
00148       //
00149       // Now average all the overlapping boxes, and add them to the new list.
00150       //
00151       list<avg_object>::iterator over_iter = overlappingObjects->begin(),
00152         over_iter_end = overlappingObjects->end();
00153       tmp = *over_iter;
00154       for ( over_iter++; over_iter != over_iter_end; over_iter++){
00155         tmp += *over_iter;
00156       }
00157       tmp.normalize();
00158       //cout << "tmp after normallizing: (" << tmp.t << ","<< tmp.b << ","<< tmp.l << ","<< tmp.r << ")"<<endl;
00159       newObjects->push_back(tmp);
00160 
00161       //
00162       //  Make the new list pose as the original list, and free memory so we
00163       //  can make a new new list and overlapping boxes list.
00164       //
00165       delete(origObjects);        // get rid of the old original list
00166       origObjects = newObjects; // make the new list pose as the original
00167       delete(overlappingObjects); // get rid of the overlapping list
00168     }
00169 
00170     // finally, write them back into object
00171     while( !newObjects->empty() ) {
00172       tmp = *(newObjects->begin());
00173       TSquare< T > square (static_cast<int>(tmp.r-tmp.l), static_cast<int>(tmp.l), static_cast<int>(tmp.t), static_cast<int>(floor(tmp.scale+.5)));
00174       objects.push_front(square);
00175       f = objects.front();
00176       newObjects->pop_front ( );
00177     }
00178     delete newObjects;
00179   }
00180 }
00181 
00182 
00183 
00184 #endif
00185 
00186 /*
00187  * 
00188  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00189  * 
00190  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00191  *    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.
00192  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00193  * 
00194  * 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.
00195  * 
00196  */

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