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

FindFacesInGWorld.mm

Go to the documentation of this file.
00001 
00002 //
00003 //  FindFacesInGWorld.mm
00004 //      Contains:       Implementation file for the FindFacesInGWorld class
00005 //
00006 //      Written by:    Ian Fasel -- added Face Detector (MPISearch) support
00007 //
00008 //      Copyright:      ©  2003 Machine Perception Laboratory 
00009 //                      University of California San Diego.
00010 //
00011 //      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 //
00013 //    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00014 //    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.
00015 //    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00016 //
00017 // 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.
00018 // 
00020 
00021 #import "FindFacesInGWorld.h"
00022 
00023 static RImage<float> grayImg;
00024 
00025 static inline double difftv(const struct timeval &t1, const struct timeval &t0)
00026 { return( (t1.tv_sec -t0.tv_sec )*1.0 +(t1.tv_usec-t0.tv_usec)*1e-6); }
00027 
00028 static inline void square(unsigned char *data, Square it, int w){
00029     typedef struct{ unsigned char a, r, g, b;} argbPixel;
00030     unsigned char val;
00031     argbPixel *pixeldata = (argbPixel *)data;
00032     for(int k=0; k<3;++k){
00033         val = ((k % 2) ? 0 : 255);
00034         int top = it.y+k;
00035         int bot = it.y+it.size-k;
00036         int left = it.x+k;
00037         int right = it.x+it.size-k;
00038         argbPixel* p = pixeldata + top*w+left;
00039         for(int i=left; i<right; ++i){
00040             p->r = val; p->g=val; p->b=val; p++;
00041         }
00042         p = pixeldata + bot*w+left;
00043         for(int i=left; i<right; ++i){
00044             p->r = val; p->g=val; p->b=val; p++;
00045         }
00046         p = pixeldata + top*w+left;
00047         for(int i=top; i<bot; ++i){
00048             p->r = val; p->g=val; p->b=val; p+=w;
00049         }
00050         p = pixeldata + top*w+right-1;
00051         for(int i=top; i<bot; ++i){
00052             p->r = val; p->g=val; p->b=val; p+=w;
00053         }
00054     }
00055 }
00056 
00057 
00058 @implementation FindFacesInGWorld
00059 - (FindFacesInGWorld *)init
00060 {
00061     mpi = new MPISearchFaceDetector;
00062     initialized = false;
00063     subsampling = 2;
00064     return [super init];
00065 }
00066 
00067 - (void)InitializeStreamWithWidth:(int)w Height:(int)h
00068 {
00069     //NSLog(@"w = %d, h = %d", w, h);
00070     mpi->initStream(w/subsampling, h/subsampling);
00071     grayImg.setSize(w/subsampling, h/subsampling);
00072     initialized = true;
00073 }
00074 
00075 - (void)FindFaces:(GWorldPtr)gWorldPtr
00076 {
00077     PixMapHandle        pixMapHandle = NULL;
00078     Ptr                 pixBaseAddr = nil;
00079 
00080     //struct timeval tv_now, last, tv_a, tv_b;
00081     //double totalTime, msgTime;
00082     //gettimeofday(&last,0);
00083 
00084     NSAssert(gWorldPtr != nil, @"nil gWorldPtr");
00085 
00086     // Lock the pixels
00087     pixMapHandle = GetGWorldPixMap(gWorldPtr);
00088     if (pixMapHandle)
00089     {
00090         Rect    portRect;
00091         int     portWidth, portHeight;
00092 
00093         NSAssert(LockPixels(pixMapHandle) != false, @"LockPixels returns false");
00094     
00095         GetPortBounds(gWorldPtr, &portRect);
00096         portWidth = (portRect.right - portRect.left);
00097         portHeight = (portRect.bottom - portRect.top);
00098 
00099         if(!initialized)
00100             [self InitializeStreamWithWidth:portWidth Height:portHeight];
00101         float   *theData = grayImg.array;
00102         int     pixmapRowBytes;
00103         int     srcRowByte,srcRowIndex;    
00104         pixBaseAddr = (unsigned char *)GetPixBaseAddr(pixMapHandle);
00105         int sm1 = (subsampling - 1)*4, sm2 = subsampling * 4;
00106         if (pixBaseAddr)
00107         {
00108             pixmapRowBytes = GetPixRowBytes(pixMapHandle);
00109             //float one_over_765 = 1.0f/(3.0f * 255.0f);
00110             for (srcRowIndex=0; srcRowIndex< portHeight; srcRowIndex+=subsampling)
00111             {
00112                 float *dst = theData + (srcRowIndex/subsampling) * (portWidth/subsampling);
00113                 unsigned char *src = pixBaseAddr + srcRowIndex * pixmapRowBytes;
00114                 //unsigned char a,r,g,b,r2,g2,b2,r3,g3,b3;
00115                 for (srcRowByte = 0; srcRowByte < portWidth; srcRowByte+=(subsampling*3))
00116                 {
00117                     *dst++ = (float)(src[1] + src[2] + src[3]);
00118                     src+=sm2;
00119                     *dst++ = (float)(src[1] + src[2] + src[3]);
00120                     src+=sm2;
00121                     *dst++ = (float)(src[1] + src[2] + src[3]);
00122                     src+=sm2;
00123                 }
00124             }
00125         }
00126         FaceBoxList faces;
00127         mpi->search(grayImg, faces);
00128         list<Square>::iterator it = faces.begin();
00129         faces.simplify(0.1f);
00130         if(faces.size() != 0) {
00131             while(!faces.empty( )){
00132                 Square face = faces.front(); 
00133                 faces.pop_front(); 
00134                 face.x*=subsampling; 
00135                 face.y*=subsampling; 
00136                 face.size*=subsampling; 
00137                 //NSLog(@"really at (%d, %d, %d)", face.x, face.y, face.size);
00138                 square(pixBaseAddr, face, pixmapRowBytes/4);
00139             }
00140         }
00141     }
00142 
00143     NSAssert(pixMapHandle != NULL, @"null pixMapHandle");
00144     NSAssert(pixBaseAddr != nil, @"nil pixBaseAddr");
00145 
00146     if (pixMapHandle)
00147     {
00148         UnlockPixels(pixMapHandle);
00149     }
00150     //gettimeofday(&tv_now,0);
00151     //totalTime = difftv(tv_now, last);
00152     //NSLog(@"\tThe search took %g seconds.", totalTime);
00153 
00154 }
00155 @end
00156 

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