EnvObjDetection.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Image/OpenCVUtil.H"
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,
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
00142
00143
00144
00145