icub-basic-demos
pf3dTracker.hpp
1 
7 //good combinations:
8 // _nParticles | stDev | inside_outside | nPixels
9 // 800 100 1.5 50
10 //with 800 particles and 30fps the 100% of the processor of CORTEX1 is used.
11 //could try to halve the number of pixels used for the circles.
12 
13 #ifndef _PF3DTRACKER_
14 #define _PF3DTRACKER_
15 
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19 
20 #include <yarp/os/BufferedPort.h>
21 #include <yarp/os/LogStream.h>
22 #include <yarp/os/Network.h>
23 #include <yarp/os/RFModule.h>
24 #include <yarp/os/Stamp.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/sig/Image.h>
27 #include <yarp/sig/ImageDraw.h>
28 #include <yarp/sig/ImageFile.h>
29 #include <yarp/sig/Vector.h>
30 
31 
32 #ifdef _CH_
33 #pragma package <opencv>
34 #endif
35 #ifndef _EiC
36 #include <opencv2/opencv.hpp>
37 #include <opencv2/highgui/highgui.hpp>
38 #include <opencv2/core/types_c.h>
39 #endif
40 
41 #include <iCub/pf3dTrackerSupport.hpp>
42 
43 //for tracking in the iCub: 1000 particles and an stDev of 80 work well with slow movements of the ball. the localization is quite stable. the shape model has a 20% difference wrt the real radius.
44 //#define _nParticles 5000
45 //#define stDev 100 80
46 //150 for the demo.
47 //#define stDev 150
48 
49 //#define NEWPOLICY 0
50 //1: only transform the color of the pixels that you need
51 //0: transform the whole image.
52 
53 #define nPixels 50
54 
55 #define YBins 4
56 #define UBins 8
57 #define VBins 8
58 
59 //should be 1.5 or 1.0 !!! ???
60 //#define inside_outside_difference_weight 1.5
61 //if I set it to 1.5, when the ball goes towards the camera, the tracker lags behind.
62 //this happens because the particles with "ball color" on both sides of the contour
63 //are still quite likely. the opposite is not true: when the ball goes away from the
64 // camera, the tracker follows it quite readily.
65 
66 class PF3DTracker : public yarp::os::RFModule
67 {
68 
69 private:
70 
71 int _numParticlesReceived;
72 
73 //parameters set during initialization.
74 std::string _inputVideoPortName;
75 yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > _inputVideoPort;
76 std::string _outputVideoPortName;
77 yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > _outputVideoPort;
78 std::string _outputDataPortName;
79 yarp::os::BufferedPort<yarp::os::Bottle> _outputDataPort;
80 std::string _inputParticlePortName;
81 yarp::os::BufferedPort<yarp::os::Bottle> _inputParticlePort;
82 std::string _outputParticlePortName;
83 yarp::os::BufferedPort<yarp::os::Bottle> _outputParticlePort;
84 std::string _outputAttentionPortName;
85 yarp::os::BufferedPort<yarp::sig::Vector> _outputAttentionPort;
86 std::string _outputUVDataPortName;
87 yarp::os::BufferedPort<yarp::os::Bottle> _outputUVDataPort;
88 bool supplyUVdata;
89 
90 std::string _projectionModel;
91 float _perspectiveFx;
92 float _perspectiveFy;
93 float _perspectiveCx;
94 float _perspectiveCy;
95 int _calibrationImageWidth;
96 int _calibrationImageHeight;
97 float _likelihoodThreshold;
98 
99 int _seeingObject; //0 means false, 1 means true.
100 int _circleVisualizationMode;
101 std::string _initializationMethod;
102 std::string _trackedObjectType;
103 bool _saveImagesWithOpencv;
104 std::string _saveImagesWithOpencvDir;
105 double _initialX;
106 double _initialY;
107 double _initialZ;
108 double _attentionOutput;
109 double _attentionOutputMax;
110 double _attentionOutputDecrease;
111 CvRNG rngState; //something needed by the random number generator
112 bool _doneInitializing;
113 
114 Lut* _lut;
115 CvMat* _A;
116 int _nParticles;
117 float _accelStDev;
118 float _inside_outside_difference_weight;
119 int _colorTransfPolicy;
120 
121 //float _modelHistogram[YBins][UBins][VBins]; //data
122 CvMatND* _modelHistogramMat; //OpenCV Matrix
123 CvMatND* _innerHistogramMat;//[YBins][UBins][VBins];
124 CvMatND* _outerHistogramMat;//[YBins][UBins][VBins];
125 
126 CvMat* _model3dPointsMat; //shape model
127 CvMat* _visualization3dPointsMat; //visualization model for the sphere (when _circleVisualizationMode==1). should have less points, but it was easier to make it like this.
128 
129 //not sure which of these are really used.
130 CvMat* _rzMat; //this is used during some operations... it is defined here to avoid repeated instantiations
131 CvMat* _ryMat; //this is used during some operations... it is defined here to avoid repeated instantiations
132 CvMat* _points2Mat; //this is used during some operations... it is defined here to avoid repeated instantiations
133 CvMat* _p2Mat1; //pointer to the first row of _points2Mat
134 CvMat* _p2Mat3; //pointer to the third row of _points2Mat
135 CvMat* _tempMat; //this is used during some operations... it is defined here to avoid repeated instantiations
136 CvMat* _tempMat1; //pointer to the first row of _tempMat
137 CvMat* _tempMat2; //pointer to the first row of _tempMat
138 CvMat* _tempMat3; //pointer to the first row of _tempMat
139 CvMat* _drawingMat; //this is used during some operations... it is defined here to avoid repeated instantiations
140 CvMat* _projectionMat; //this is used during some operations... it is defined here to avoid repeated instantiations
141 CvMat* _xyzMat1; //this is used during some operations... it is defined here to avoid repeated instantiations
142 CvMat* _xyzMat2; //this is used during some operations... it is defined here to avoid repeated instantiations
143 CvMat* _xyzMat3; //this is used during some operations... it is defined here to avoid repeated instantiations
144 CvMat* _particles1; //this is used during some operations... it is defined here to avoid repeated instantiations
145 CvMat* _particles2; //this is used during some operations... it is defined here to avoid repeated instantiations
146 CvMat* _particles3; //this is used during some operations... it is defined here to avoid repeated instantiations
147 CvMat* _particles4; //this is used during some operations... it is defined here to avoid repeated instantiations
148 CvMat* _particles5; //this is used during some operations... it is defined here to avoid repeated instantiations
149 CvMat* _particles6; //this is used during some operations... it is defined here to avoid repeated instantiations
150 CvMat* _particles7; //this is used during some operations... it is defined here to avoid repeated instantiations
151 CvMat* _particles1to6;
152 CvMat* _newParticles1to6;
153 CvMat* _uv;
154 
155 //resampling-related stuff
156 CvMat* _nChildren;
157 CvMat* _label;
158 CvMat* _u;
159 CvMat* _ramp;
160 
161 //new resampling-related stuff
162 CvMat* _cumWeight;
163 
164 //variables
165 CvMat* _particles;
166 CvMat* _newParticles;
167 CvMat* _noise;
168 CvMat* _noise1; //lines from 0 to 2
169 CvMat* _noise2; //lines from 3 to 5
170 
171 yarp::os::Stamp _yarpTimestamp;
172 yarp::sig::ImageOf<yarp::sig::PixelRgb> *_yarpImage;
173 IplImage *_rawImage;
174 IplImage* _transformedImage;//_yuvBinsImage[image_width][image_height][3];
175 double _initialTime;
176 double _finalTime;
177 
178 int _frameCounter;
179 int _framesNotTracking; //number of frames for which the likelihood has been under the threshold. after 20 such frames the tracker is restarted.
180 int downsampler;
181 float _lastU;
182 float _lastV;
183 bool _firstFrame; //when processing the first frame do not write the fps values, as it's wrong.
184 
186 //MEMBERS THAT "WORK":/
188 bool testOpenCv();
189 bool readModelHistogram(CvMatND* histogram,const char fileName[]);
190 bool readInitialmodel3dPoints(CvMat* points,std::string fileName);
191 bool readMotionModelMatrix(CvMat* points, std::string fileName);
192 bool computeTemplateHistogram(std::string imageFileName,std::string dataFileName); //I checked the output, it seems to work, but it seems as if in the old version the normalization didn't take effect.
193 bool computeHistogram(CvMat* uv, IplImage* transformedImage, CvMatND* innerHistogramMat, float &usedInnerPoints, CvMatND* outerHistogramMat, float &usedOuterPoints);
194 bool computeHistogramFromRgbImage(CvMat* uv, IplImage *image, CvMatND* innerHistogramMat, float &usedInnerPoints, CvMatND* outerHistogramMat, float &usedOuterPoints);
195 bool calculateLikelihood(CvMatND* templateHistogramMat, CvMatND* innerHistogramMat, CvMatND* outerHistogramMat, float inside_outside, float &likelihood);
196 bool place3dPointsPerspective(CvMat* points, float x, float y, float z);
197 int perspective_projection(CvMat* xyz, float fx, float fy, float cx, float cy, CvMat* uv);
198 void drawContourPerspectiveYARP(CvMat* model3dPointsMat,float x, float y, float z, yarp::sig::ImageOf<yarp::sig::PixelRgb> *image,float _perspectiveFx,float _perspectiveFy ,float _perspectiveCx,float _perspectiveCy ,int R, int G, int B, float &meanU, float &meanV);
199 void drawSampledLinesPerspectiveYARP(CvMat* model3dPointsMat,float x, float y, float z, yarp::sig::ImageOf<yarp::sig::PixelRgb> *image,float _perspectiveFx,float _perspectiveFy ,float _perspectiveCx,float _perspectiveCy ,int R, int G, int B, float &meanU, float &meanV);
200 bool evaluateHypothesisPerspective(CvMat* model3dPointsMat, float x, float y, float z, CvMatND* modelHistogramMat, IplImage* transformedImage, float fx, float fy, float u0, float v0, float, float &likelihood);
201 
203 //MEMBERS THAT SHOULD BE CHANGED AND CHECKED:/
205 bool evaluateHypothesisPerspectiveFromRgbImage(CvMat* model3dPoints,float x, float y, float z, CvMatND* modelHistogramMat, IplImage *image, float fx, float fy, float u0, float v0, float inside_outside, float &likelihood);
206 
207 bool systematicR(CvMat* inState, CvMat* weights, CvMat* outState);
208 bool systematic_resampling(CvMat* oldParticlesState, CvMat* oldParticlesWeights, CvMat* newParticlesState, CvMat* cumWeight);
209 
210 public:
211 
212 PF3DTracker(); //constructor
213 ~PF3DTracker(); //destructor
214 
215 virtual bool configure(yarp::os::ResourceFinder &rf); //member to set the object up.
216 virtual bool close(); //member to close the object.
217 virtual bool interruptModule(); //member to close the object.
218 virtual bool updateModule(); //member that is repeatedly called by YARP, to give this object a chance to do something.
219 virtual double getPeriod();
220 
221 };
222 
223 #endif /* _PF3DTRACKER_ */
Copyright: (C) 2009 RobotCub Consortium Authors: Matteo Taiana, Ugo Pattacini CopyPolicy: Released un...