opencv on mbed

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers detection_based_tracker.hpp Source File

detection_based_tracker.hpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
00016 // Third party copyrights are property of their respective owners.
00017 //
00018 // Redistribution and use in source and binary forms, with or without modification,
00019 // are permitted provided that the following conditions are met:
00020 //
00021 //   * Redistribution's of source code must retain the above copyright notice,
00022 //     this list of conditions and the following disclaimer.
00023 //
00024 //   * Redistribution's in binary form must reproduce the above copyright notice,
00025 //     this list of conditions and the following disclaimer in the documentation
00026 //     and/or other materials provided with the distribution.
00027 //
00028 //   * The name of the copyright holders may not be used to endorse or promote products
00029 //     derived from this software without specific prior written permission.
00030 //
00031 // This software is provided by the copyright holders and contributors "as is" and
00032 // any express or implied warranties, including, but not limited to, the implied
00033 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00034 // In no event shall the Intel Corporation or contributors be liable for any direct,
00035 // indirect, incidental, special, exemplary, or consequential damages
00036 // (including, but not limited to, procurement of substitute goods or services;
00037 // loss of use, data, or profits; or business interruption) however caused
00038 // and on any theory of liability, whether in contract, strict liability,
00039 // or tort (including negligence or otherwise) arising in any way out of
00040 // the use of this software, even if advised of the possibility of such damage.
00041 //
00042 //M*/
00043 
00044 #ifndef __OPENCV_OBJDETECT_DBT_HPP__
00045 #define __OPENCV_OBJDETECT_DBT_HPP__
00046 
00047 // After this condition removal update blacklist for bindings: modules/python/common.cmake
00048 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(__ANDROID__) || \
00049   (defined(__cplusplus) &&  __cplusplus > 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1700)
00050 
00051 #include <vector>
00052 
00053 namespace cv
00054 {
00055 
00056 //! @addtogroup objdetect
00057 //! @{
00058 
00059 class CV_EXPORTS DetectionBasedTracker
00060 {
00061     public:
00062         struct Parameters
00063         {
00064             int maxTrackLifetime;
00065             int minDetectionPeriod; //the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0
00066 
00067             Parameters();
00068         };
00069 
00070         class IDetector
00071         {
00072             public:
00073                 IDetector():
00074                     minObjSize(96, 96),
00075                     maxObjSize(INT_MAX, INT_MAX),
00076                     minNeighbours(2),
00077                     scaleFactor(1.1f)
00078                 {}
00079 
00080                 virtual void detect(const cv::Mat& image, std::vector<cv::Rect>& objects) = 0;
00081 
00082                 void setMinObjectSize(const cv::Size& min)
00083                 {
00084                     minObjSize = min;
00085                 }
00086                 void setMaxObjectSize(const cv::Size& max)
00087                 {
00088                     maxObjSize = max;
00089                 }
00090                 cv::Size getMinObjectSize() const
00091                 {
00092                     return minObjSize;
00093                 }
00094                 cv::Size getMaxObjectSize() const
00095                 {
00096                     return maxObjSize;
00097                 }
00098                 float getScaleFactor()
00099                 {
00100                     return scaleFactor;
00101                 }
00102                 void setScaleFactor(float value)
00103                 {
00104                     scaleFactor = value;
00105                 }
00106                 int getMinNeighbours()
00107                 {
00108                     return minNeighbours;
00109                 }
00110                 void setMinNeighbours(int value)
00111                 {
00112                     minNeighbours = value;
00113                 }
00114                 virtual ~IDetector() {}
00115 
00116             protected:
00117                 cv::Size minObjSize;
00118                 cv::Size maxObjSize;
00119                 int minNeighbours;
00120                 float scaleFactor;
00121         };
00122 
00123         DetectionBasedTracker(cv::Ptr<IDetector>  mainDetector, cv::Ptr<IDetector>  trackingDetector, const Parameters& params);
00124         virtual ~DetectionBasedTracker();
00125 
00126         virtual bool run();
00127         virtual void stop();
00128         virtual void resetTracking();
00129 
00130         virtual void process(const cv::Mat& imageGray);
00131 
00132         bool setParameters(const Parameters& params);
00133         const Parameters& getParameters() const;
00134 
00135 
00136         typedef std::pair<cv::Rect, int> Object;
00137         virtual void getObjects(std::vector<cv::Rect>& result) const;
00138         virtual void getObjects(std::vector<Object>& result) const;
00139 
00140         enum ObjectStatus
00141         {
00142             DETECTED_NOT_SHOWN_YET,
00143             DETECTED,
00144             DETECTED_TEMPORARY_LOST,
00145             WRONG_OBJECT
00146         };
00147         struct ExtObject
00148         {
00149             int id;
00150             cv::Rect location;
00151             ObjectStatus status;
00152             ExtObject(int _id, cv::Rect _location, ObjectStatus _status)
00153                 :id(_id), location(_location), status(_status)
00154             {
00155             }
00156         };
00157         virtual void getObjects(std::vector<ExtObject>& result) const;
00158 
00159 
00160         virtual int addObject(const cv::Rect& location); //returns id of the new object
00161 
00162     protected:
00163         class SeparateDetectionWork;
00164         cv::Ptr<SeparateDetectionWork>  separateDetectionWork;
00165         friend void* workcycleObjectDetectorFunction(void* p);
00166 
00167         struct InnerParameters
00168         {
00169             int numLastPositionsToTrack;
00170             int numStepsToWaitBeforeFirstShow;
00171             int numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown;
00172             int numStepsToShowWithoutDetecting;
00173 
00174             float coeffTrackingWindowSize;
00175             float coeffObjectSizeToTrack;
00176             float coeffObjectSpeedUsingInPrediction;
00177 
00178             InnerParameters();
00179         };
00180         Parameters parameters;
00181         InnerParameters innerParameters;
00182 
00183         struct TrackedObject
00184         {
00185             typedef std::vector<cv::Rect> PositionsVector;
00186 
00187             PositionsVector lastPositions;
00188 
00189             int numDetectedFrames;
00190             int numFramesNotDetected;
00191             int id;
00192 
00193             TrackedObject(const cv::Rect& rect):numDetectedFrames(1), numFramesNotDetected(0)
00194             {
00195                 lastPositions.push_back(rect);
00196                 id=getNextId();
00197             };
00198 
00199             static int getNextId()
00200             {
00201                 static int _id=0;
00202                 return _id++;
00203             }
00204         };
00205 
00206         int numTrackedSteps;
00207         std::vector<TrackedObject> trackedObjects;
00208 
00209         std::vector<float> weightsPositionsSmoothing;
00210         std::vector<float> weightsSizesSmoothing;
00211 
00212         cv::Ptr<IDetector>  cascadeForTracking;
00213 
00214         void updateTrackedObjects(const std::vector<cv::Rect>& detectedObjects);
00215         cv::Rect calcTrackedObjectPositionToShow(int i) const;
00216         cv::Rect calcTrackedObjectPositionToShow(int i, ObjectStatus& status) const;
00217         void detectInRegion(const cv::Mat& img, const cv::Rect& r, std::vector<cv::Rect>& detectedObjectsInRegions);
00218 };
00219 
00220 //! @} objdetect
00221 
00222 } //end of cv namespace
00223 #endif
00224 
00225 #endif
00226