00001 #ifndef Impala_Core_Tracking_SearcherAroundLastpos_h
00002 #define Impala_Core_Tracking_SearcherAroundLastpos_h
00003
00004 #include "Core/Tracking/SearcherBase.h"
00005
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Tracking
00011 {
00012
00013 class SearcherAroundLastPos : public SearcherBase
00014 {
00015 public:
00016 SearcherAroundLastPos()
00017 {
00018 mSampleSize = 300;
00019 mPosSigma = 20;
00020 }
00021
00022 void SearchObject(Array::Array2dVec3Real64& image)
00023 {
00024 int i;
00025 double score, bestScore = 1e100;
00026 Point bestPos;
00027 Array::Array2dVec3Real64* temp = new Array::Array2dVec3Real64(mLastPos.size.x, mLastPos.size.y, 0, 0);
00028 for(i=0 ; i<mSampleSize ; i++)
00029 {
00030 double x,y,sqr;
00031
00032 do
00033 {
00034 do
00035 {
00036 x = ((double)rand() / (double)RAND_MAX) * 2 - 1;
00037 y = ((double)rand() / (double)RAND_MAX) * 2 - 1;
00038 sqr = x*x + y*y;
00039 }
00040 while(sqr >=1.0 || sqr == 0);
00041 double fac = sqrt(-2.0*log(sqr)/sqr);
00042
00043 x = (fac * x * mPosSigma) + mLastPos.translation.x;
00044 y = (fac * y * mPosSigma) + mLastPos.translation.y;
00045 }
00046 while(x<0 || y<0 || x+mLastPos.size.x >= image.CW() || y+mLastPos.size.y >= image.CH());
00047
00048
00049
00050
00051
00052 Array::Pattern::PatSet(temp,&image, (int)x, (int)y, mLastPos.size.x, mLastPos.size.y, 0, 0);
00053
00054
00055 score = mObject->Score(*temp,mLastPos);
00056
00057
00058 if(score < bestScore)
00059 {
00060 bestPos.x = x;
00061 bestPos.y = y;
00062 bestScore = score;
00063 }
00064 }
00065
00066
00067 int x,y;
00068 for(x=bestPos.x-3 ; x<=bestPos.x+3 ; x++)
00069 {
00070 for(y=bestPos.y-3 ; y<=bestPos.y+3 ; y++)
00071 {
00072 if(x>=0 && y>=0 && x+mLastPos.size.x < image.CW() && y+mLastPos.size.y < image.CH())
00073 {
00074 Array::Pattern::PatSet(temp,&image, (int)x, (int)y, mLastPos.size.x, mLastPos.size.y, 0, 0);
00075 score = mObject->Score(*temp,mLastPos);
00076 if(score < bestScore)
00077 {
00078 bestPos.x = x;
00079 bestPos.y = y;
00080 bestScore = score;
00081 }
00082 }
00083 }
00084 }
00085 delete temp;
00086
00087
00088 mSpeed.translation.x = bestPos.x - mLastPos.translation.x;
00089 mSpeed.translation.y = bestPos.y - mLastPos.translation.y;
00090 mLastPos.translation = bestPos;
00091 }
00092
00093 void Reset(Array::Array2dVec3Real64& image, ObjectRepresentation& object, const Position& pos)
00094 {
00095 SearcherBase::Reset(image, object, pos);
00096 }
00097
00098 private:
00099 int mSampleSize;
00100 double mPosSigma;
00101 Position mSpeed;
00102 };
00103
00104 }
00105 }
00106 }
00107
00108 #endif //Impala_Core_Tracking_SearcherAroundLastpos_h