opencv_blobtrack.h

00001 /* -- 2007-05-07 -- 
00002  *  ardev - an augmented reality library for robot developers
00003  *  Copyright 2005-2007 - Toby Collett (ardev _at_ plan9.net.nz)
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00018  *
00019  */
00020 #ifndef OPENCV_BLOBTRACK_H
00021 #define OPENCV_BLOBTRACK_H
00022 
00023 #include <ardev/ardevconfig.h>
00024 
00025 #ifdef HAVE_OPENCV
00026 
00027 #include <ardev/ardev.h>
00028 #include <ardev/debug.h>
00029 #include <map>
00030 
00031 #include <opencv/cv.h>
00032 
00033 struct intlt_bt
00034 {
00035         public: bool operator () (const int & lhs, const int &rhs)
00036         {
00037                 return lhs < rhs;
00038         };
00039 };
00040 
00041 typedef struct BlobPair
00042 {
00043         int Hue1_Min;
00044         int Hue1_Max;
00045         int Hue2_Min;
00046         int Hue2_Max;
00047         double Height;
00048         
00049         ARPosition pos; 
00050 } BlobPair_t;
00051 
00055 class OpenCVBlobTrackPreProcess : public FrameProcessObject
00056 {
00057         public:
00059                 OpenCVBlobTrackPreProcess(CameraObject & Camera, double BlobMinSize = 200, double BlobMaxSize = 10000, bool debug = false);
00060         
00062                 ~OpenCVBlobTrackPreProcess();
00063                 
00065                 void ProcessFrame(const ARImage & frame);
00066                 
00072                 int AddBlobs(BlobPair_t & blob);
00074                 void RemoveBlobs(int id);
00075         
00077                 bool GetMarkerPos(int id, ARPosition &result);
00078         
00079         private:
00080                 unsigned int lastFrameWidth;            //< last width of a frame, used to scale calibration data
00081                 unsigned int lastFrameHeight;           //< last height of a frame, used to scale calibration data
00082                 ARCamera camera;                                //< the camera
00083                 double MinSize;                                 //< Min size of blob
00084                 double MaxSize;                                 //< Max size of blob
00085                 bool Debug;                                     //< Set wether the debug windows are enabled
00086                 int PatNumber;
00087                 
00088                 std::map<int,BlobPair,intlt_bt> Markers; //< A map of the marker Heights
00089 
00090                 // find center of a blob                
00091                 ARPoint GetBlobCenter(int HueMin, int HueMax, IplImage * im, CvSize size);
00092                 
00094                 ARPosition GetPosition(ARPoint P1, ARPoint P2, double Height);
00095                 
00097                 void DropLowSat(IplImage * h_img, IplImage * s_img, int sat_thresh, int NewHue, CvSize size);
00098 };
00099 
00102 class OpenCVBlobTrackPosition : public PositionObject
00103 {
00104         public:
00105                 OpenCVBlobTrackPosition(OpenCVBlobTrackPreProcess & pre, const BlobPair_t & blob) : Pre(pre), Blob(blob)
00106                 {
00107                         Elapsed = 0;
00108                         MarkerID = 0;
00109                         memset(&lastGoodPosition,0,sizeof(lastGoodPosition));
00110                 }; 
00111                 
00112                 ~OpenCVBlobTrackPosition()
00113                 {
00114                 }; 
00115         
00117                 int Initialise(bool Active = true)
00118                 {
00119                         dbg_print(ARDBG_VERBOSE,"Adding new blob pair to opencv tracker: %d %d %d %d\n",Blob.Hue1_Min, Blob.Hue1_Max,Blob.Hue2_Min,Blob.Hue2_Max);
00120                         MarkerID = Pre.AddBlobs(Blob);
00121                         dbg_print(ARDBG_VERBOSE,"Blob was assigned Marker ID: %d\n",MarkerID);
00122                         return 0;
00123                 };
00124 
00126                 void Terminate()
00127                 {
00128                         Pre.RemoveBlobs(MarkerID);
00129                 };
00130         
00132                 virtual ARPosition GetPosition();
00133 
00135                 virtual bool Present();
00136         
00137         protected:
00138                 OpenCVBlobTrackPreProcess & Pre;
00139                 unsigned int MarkerID;
00140                 ARPosition lastGoodPosition;
00141                 StopWatch Timer;
00142                 double Elapsed;
00143                 BlobPair_t Blob;
00144 };
00145 
00146 #endif
00147 #endif

SourceForge.net Logo Generated on Sat May 12 15:25:43 2007 for ardev by doxygen 1.5.1