00001 /*!@file Neuro/EnvObjDetection.C */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Neuro/EnvObjDetection.C $ 00035 // $Id: EnvObjDetection.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #include "Image/OpenCVUtil.H" // must be first to avoid conflicting defs of int64, uint64 00039 00040 #include "Neuro/EnvObjDetection.H" 00041 00042 #include "Image/CutPaste.H" 00043 #include "Image/DrawOps.H" 00044 #include "Image/Image.H" 00045 #include "Image/Pixels.H" 00046 #include "GUI/DebugWin.H" 00047 #include "Component/ModelOptionDef.H" 00048 #include "Neuro/NeuroOpts.H" 00049 00050 static const ModelOptionDef OPT_CascadeFilePath = 00051 { MODOPT_ARG_STRING, "Cascade file path", &MOC_ITC, OPTEXP_CORE, 00052 "Name of directory containing the description of a trained cascade classifier." 00053 "Used in making faces salient or any other object. ", 00054 "cascade-file", '\0', "<filename>.xml", 00055 "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml"}; 00056 00057 00058 // ###################################################################### 00059 EnvObjDetection::EnvObjDetection(OptionManager& mgr) 00060 : 00061 EnvSegmenter(mgr, "Embeddable Object detection", 00062 "EnvObjDetection"), 00063 itsCascadeFile(&OPT_CascadeFilePath, this) 00064 #ifdef HAVE_OPENCV 00065 , 00066 itsStorage(cvCreateMemStorage(0)) 00067 #endif 00068 { 00069 #ifndef HAVE_OPENCV 00070 LFATAL("OpenCV must be installed in order to use this function"); 00071 #else 00072 ASSERT(itsStorage != 0); 00073 00074 itsCascade = (CvHaarClassifierCascade*)cvLoad( itsCascadeFile.getVal().c_str(), 0, 0, 0 ); 00075 if( !itsCascade ) 00076 LFATAL("ERROR: Could not load classifier cascade (%s)\n", itsCascadeFile.getVal().c_str() ); 00077 00078 #endif 00079 } 00080 00081 // ###################################################################### 00082 EnvObjDetection::~EnvObjDetection() 00083 { 00084 #ifndef HAVE_OPENCV 00085 LERROR("OpenCV must be installed in order to use this function"); 00086 #else 00087 cvReleaseMemStorage(&itsStorage); 00088 #endif 00089 } 00090 00091 // ###################################################################### 00092 Rectangle EnvObjDetection::getFoa(const Image<PixRGB<byte> >& rgbin, 00093 const Point2D<int>& center, 00094 Image<byte>* foamask, 00095 Image<PixRGB<byte> >* segmentdisp) const 00096 { 00097 #ifndef HAVE_OPENCV 00098 LFATAL("OpenCV must be installed in order to use this function"); 00099 return Rectangle(); 00100 #else 00101 00102 Image<byte> lum = luminance(rgbin); 00103 00104 double scale = 1.3; 00105 IplImage* small_img = cvCreateImage( 00106 cvSize( cvRound (lum.getWidth()/scale), cvRound (lum.getHeight()/scale)), 00107 8, 1 ); 00108 00109 cvResize( img2ipl(lum), small_img, CV_INTER_LINEAR ); 00110 cvEqualizeHist( small_img, small_img ); 00111 cvClearMemStorage( itsStorage ); 00112 00113 Rectangle result; 00114 if( itsCascade ) 00115 { 00116 double t = (double)cvGetTickCount(); 00117 CvSeq* objects = cvHaarDetectObjects( small_img, itsCascade, itsStorage, 00118 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, 00119 cvSize(30, 30) ); 00120 t = (double)cvGetTickCount() - t; 00121 LDEBUG( "detection time = %gms objects=%i\n", 00122 t/((double)cvGetTickFrequency()*1000.), objects->total ); 00123 00124 if (objects->total > 0) 00125 { 00126 int i = 0; 00127 CvRect* r = (CvRect*)cvGetSeqElem( objects, i ); 00128 00129 result = Rectangle(Point2D<int>((int)(r->x*scale), (int)(r->y*scale)), 00130 Dims((int)(r->width*scale), (int)(r->height*scale))); 00131 } 00132 00133 } 00134 cvReleaseImage( &small_img ); 00135 00136 return result.getOverlap(rgbin.getBounds()); 00137 #endif 00138 } 00139 00140 // ###################################################################### 00141 /* So things look consistent in everyone's emacs... */ 00142 /* Local Variables: */ 00143 /* mode: c++ */ 00144 /* indent-tabs-mode: nil */ 00145 /* End: */