icub-basic-demos
pf3dBottomup.hpp
1 /*
2  * A bottom-up approach for generating particles for the "pf3dTracker" module
3  *
4  * Copyright (C) 2010 RobotCub Consortium
5  *
6  * Author: Martim Brandao
7  * Note: Should you use or reference my work on your own research, please let me know (mbrandao _AT_ isr.ist.utl.pt)
8  *
9  * Image sequence as input, N particles (3D position of balls) as output.
10  *
11  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
12  *
13  */
14 
15 #include <iostream>
16 #include <string>
17 #include <sstream>
18 #include <yarp/os/all.h>
19 #include <yarp/sig/all.h>
20 #include <iCub/ScaleSpace.h>
21 
22 #ifdef _CH_
23 #pragma package <opencv>
24 #endif
25 #ifndef _EiC
26 #include <opencv2/opencv.hpp>
27 #include <opencv2/core/types_c.h>
28 #include <opencv2/imgproc/imgproc_c.h>
29 #endif
30 
31 // my definitions
32 #define PI 3.1415926535897932384626433832795
33 #define SCATTER(s) ( (s*(double)rand()/(double)RAND_MAX)-0.5*s )
34 
35 using namespace std;
36 using namespace yarp::os;
37 using namespace yarp::sig;
38 
39 // module
40 class pf3dBottomup : public RFModule
41 {
42 
43 private:
44 
45 
46 // my definitions
47 typedef struct MbMoments
48 {
49  double m00, m10, m01;
50  double m[12];
51 }MbMoments;
52 
53 typedef struct CameraModel
54 {
55  double fx;
56  double fy;
57  double cx;
58  double cy;
59  double fov;
60  double aspect;
61  double znear;
62  double zfar;
63 }CameraModel;
64 
65 typedef struct ObjectModel
66 {
67 /*
68  static const int QUANT_A=12; //this should be made variable... (dynamic init.)
69  static const int NUM_MOM=12; //this should be made variable... (dynamic init.)
70  static const int NUM_POSES=1008; //this should be made variable... (dynamic init.)
71  double data[NUM_POSES][7+NUM_MOM];
72  double moment_variance[NUM_MOM];
73  double prob[NUM_POSES];
74  double prob_acum[NUM_POSES];
75  float prob_particles[NUM_POSES];
76 */
77  double raio_esfera;
78  CvHistogram *hist;
79  CvMat *particles;
80 }ObjectModel;
81 
82 
83 // parameters set during initialization.
84 string _inputVideoPortName;
85 BufferedPort<ImageOf<PixelRgb> > _inputVideoPort;
86 string _outputParticlePortName;
87 BufferedPort<Bottle> _outputParticlePort;
88 
89 double _perspectiveFx;
90 double _perspectiveFy;
91 double _perspectiveCx;
92 double _perspectiveCy;
93 int _calibrationImageWidth;
94 int _calibrationImageHeight;
95 bool _doneInitializing;
96 
97 int _nParticles;
98 
99 ImageOf<PixelRgb> *_yarpImage;
100 
101 
102 // my parameters
103 int _maskVmin, _maskVmax, _maskSmin, _blur;
104 int _scaleSpaceLevels;
105 double _scaleSpaceScales[3];
106 
107 
108 // global instances
109 CameraModel _camera;
110 ObjectModel _object_model;
111 
112 ScaleSpace ss;
113 IplImage *image, *infloat, *hsv, *hue, *sat, *val, *mask, *backproject, *backprojectmask2;
114 
115 
116 void calc_hist_from_model_2D(string file, CvHistogram **objhist, int _vmin, int _vmax);
117 void normalize_to_global_max(IplImage *img);
118 void scale_space_segmentation(IplImage *img, ScaleSpace *ss, IplImage *result);
119 int object_localization_simple(IplImage *segm, ObjectModel *model, CameraModel *camera);
120 
121 
122 public:
123 
124 pf3dBottomup(); //constructor
125 ~pf3dBottomup(); //destructor
126 
127 virtual bool configure(ResourceFinder &rf); //member to set the object up.
128 virtual bool close(); //member to close the object.
129 virtual bool interruptModule(); //member to close the object.
130 virtual bool updateModule(); //member that is repeatedly called by YARP
131 
132 };
133 
134 
135 // changed functions
136 void cvFloodFill2( CvArr* arr, CvPoint seed_point, CvScalar newVal, CvScalar lo_diff, CvScalar up_diff, CvConnectedComp* comp, int flags, CvArr* maskarr );
137 
138 
Implements a Gaussian Scale Space for floating point images.