19
19
20
20
21
21
22
- #include < iostream>
23
- #include < cmath>
24
- #include < fstream>
25
- #include < iomanip>
26
- #include < stdlib.h>
27
- #include < vector>
22
+ #include < iostream>
23
+ #include < cmath>
24
+ #include < fstream>
25
+ #include < iomanip>
26
+ #include < stdlib.h>
27
+ #include < vector>
28
28
#include " point.hpp"
29
29
#include < opencv2/core/core.hpp>
30
30
#include < opencv2/highgui/highgui.hpp>
@@ -48,6 +48,17 @@ using namespace cv;
48
48
// **********************************************/
49
49
50
50
51
+ /* *************************************************
52
+ Cost functions compute the cost to link to different particles.
53
+ This function can be computed from different factors: the distance,
54
+ the size, the form factor etc…
55
+ Currently, the first version computes the cost from the distance only,
56
+ the second includes the size…
57
+ Finally these functions will be merged in order to have the same prototypes,
58
+ thus it will be easier to make this function evolve…
59
+
60
+ ****************************************************/
61
+
51
62
double cost_function (double * current, double * expected)
52
63
{
53
64
return (sqrt ((current[0 ]-expected[0 ])*(current[0 ]-expected[0 ])+(current[1 ]-expected[1 ])*(current[1 ]-expected[1 ])));
@@ -73,26 +84,6 @@ bool in_area(double * current, double * expected, double radius)
73
84
else return false ;
74
85
}
75
86
76
- /*
77
- double cost_function(int * current, double * expected)
78
- {
79
- return sqrt((current[0]-expected[0])*(current[0]-expected[0])+(current[1]-expected[1])*(current[1]-expected[1]));
80
- }
81
- */
82
-
83
- // /***********************************************
84
- // ************IN AREA****************************
85
- // **********************************************/
86
- /*
87
- bool in_area(int * current, double * expected, double radius)
88
- {
89
- if (sqrt((current[0]-expected[0])*(current[0]-expected[0])+(current[1]-expected[1])*(current[1]-expected[1]))<radius) return true;
90
- else return false;
91
-
92
- }
93
- */
94
-
95
-
96
87
97
88
98
89
@@ -104,53 +95,50 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
104
95
/* ///////////////////////////////STRATEGY//////////////////////////////
105
96
if 0 : neirest neighbor
106
97
if 1 : linear prediction
107
- if 2 : quadratic predicition
98
+ if 2 : quadratic predicition (not implementated… (Yet?))
108
99
*/
109
- // test(0);
110
100
111
101
112
102
double expected_position[2 ];
113
103
unsigned int frame=0 ;
114
- // initialisation
104
+ // initialisation: will assign every point in frame 0 to a new track
115
105
for (unsigned int i=0 ;i<points[0 ].size ();i++)
116
106
{
117
107
if (points[0 ].at (i).area ()>=size_min && points[0 ].at (i).area ()<=size_max)
118
108
{
119
109
tracks.push_back (Track (points[0 ].at (i).center_position ()[0 ],points[0 ].at (i).center_position ()[1 ],0 ,points[0 ].at (i).area ()));
120
- // cout<<points[0].at(i).area()<<endl;
121
110
points[0 ].at (i).track_index =i;
122
111
}
123
112
}
124
113
125
114
126
- // calcul
127
- for (unsigned int i=0 ;i<NB_frames-1 ;i++) // expected_position[0]=points[i][j].center[0];
115
+
116
+ for (unsigned int i=0 ;i<NB_frames-1 ;i++) // loop on all frames of the movie
128
117
{
129
118
130
- if (mode_low_ram) ia >> points[i+1 ];
119
+ if (mode_low_ram) ia >> points[i+1 ]; // Backup the archive
131
120
132
121
if (i%100 ==0 )
133
122
{
134
123
cout<<" \r " <<i<<" /" <<NB_frames;
135
124
fflush (stdout);
136
125
}
137
- for (unsigned int j=0 ;j<tracks.size ();j++) // loop on particule of frame N
126
+ for (unsigned int j=0 ;j<tracks.size ();j++) // loop on ALL tracks (not so efficient…)
138
127
{
139
128
// test(j);
140
- if (tracks[j].Frame .back ()<=i && tracks[j].Frame .back ()>=i-gap)
129
+ if (tracks[j].Frame .back ()<=i && tracks[j].Frame .back ()>=i-gap) // test if track is still open.
141
130
{
142
131
// Linking of particules already linked in the precedent frame
143
- // TO change in further developments
144
132
if (strategy==1 )
145
133
{
146
- if (tracks[j].X .size ()>1 )
134
+ if (tracks[j].X .size ()>1 ) // linear interpolation
147
135
{
148
136
int size=tracks[j].X .size ();
149
137
expected_position[0 ]=(tracks[j].X .at (size-1 )-tracks[j].X .at (size-2 ))/((tracks[j].Frame .at (size-1 )-tracks[j].Frame .at (size-2 )))+tracks[j].X .at (size-1 )+flow_x;
150
138
expected_position[1 ]=(tracks[j].Y .at (size-1 )-tracks[j].Y .at (size-2 ))/((tracks[j].Frame .at (size-1 )-tracks[j].Frame .at (size-2 )))+tracks[j].Y .at (size-1 )+flow_y;
151
139
152
140
}
153
- else
141
+ else // track to short to compute the interpolation (2 points needed)
154
142
{
155
143
expected_position[0 ]=tracks[j].X .back ()+flow_x;
156
144
expected_position[1 ]=tracks[j].Y .back ()+flow_y;
@@ -165,17 +153,16 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
165
153
166
154
167
155
168
- Points * candidate=NULL ;
156
+ Points * candidate=NULL ; // Windows compatibility…
169
157
double candidate_cost=search_radius*2 ;
170
158
for (unsigned int k=0 ;k<points[i+1 ].size ();k++) // loop in particules in next frame
171
- {
172
-
173
- if (points[i+1 ].at (k).track_index ==-1 && points[i+1 ].at (k).area ()>=size_min && points[i+1 ].at (k).area ()<=size_max)
159
+ {
160
+ if (points[i+1 ].at (k).track_index ==-1 && points[i+1 ].at (k).area ()>=size_min && points[i+1 ].at (k).area ()<=size_max) // test if particle is not attributed (track_index=-1) and its size
174
161
{
175
162
double x_current=points[i+1 ].at (k).center_position ()[0 ];
176
163
double y_current=points[i+1 ].at (k).center_position ()[1 ];
177
164
178
- if (in_area (points[i+1 ].at (k).center_position (),expected_position,search_radius)) // put the cost function here ?
165
+ if (in_area (points[i+1 ].at (k).center_position (),expected_position,search_radius)) // check if the particle is close enought to the expected position
179
166
{
180
167
// if (cost_function(points[i+1].at(k).center_position(),expected_position)<=candidate_cost)
181
168
if (cost_function (expected_position,points[i+1 ].at (k),tracks[j].size_P .back ())<=candidate_cost)
@@ -188,18 +175,19 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
188
175
}// end if in area
189
176
}// end if not already allocated
190
177
}// end loop particules in next frame
191
- if (candidate_cost<search_radius+1 ){
192
- tracks[j].X .push_back (candidate->center_position ()[0 ]);
193
- tracks[j].Y .push_back (candidate->center_position ()[1 ]);
194
- tracks[j].Frame .push_back (i+1 );
195
- tracks[j].size_P .push_back (candidate->area ());
196
- candidate->track_index =j;
197
- // cout<<"Track "<<j<<" found a match at position "<<candidate->center[0]<<" "<<candidate->center[1]<<endl;
198
- } // end of if there is a match
199
-
178
+ if (candidate_cost<search_radius+1 ) // a match is found:
179
+ {
180
+ // put the particle at the end of the track
181
+ tracks[j].X .push_back (candidate->center_position ()[0 ]);
182
+ tracks[j].Y .push_back (candidate->center_position ()[1 ]);
183
+ tracks[j].Frame .push_back (i+1 );
184
+ tracks[j].size_P .push_back (candidate->area ());
185
+ // give the number of the track to the particle (then it will not be attributed a second time…)
186
+ candidate->track_index =j;
187
+ } // end of if there is a match
200
188
} // end of if on particles who are between N and N-gap
201
189
} // end loop on tracks of frame N
202
-
190
+
203
191
204
192
205
193
for (unsigned int j=0 ;j<points[i].size ();j++) // loop on particules not being attributed on a tracks previously
@@ -236,7 +224,7 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
236
224
}// end if in area
237
225
}// end if not already allocated
238
226
}// end loop particules in next frame
239
- if (candidate_cost<search_radius+1 )
227
+ if (candidate_cost<search_radius+1 ) // put the two particle in a new track
240
228
{
241
229
tracks.push_back (Track (points[i].at (j).center_position ()[0 ],points[i].at (j).center_position ()[1 ],i,points[i].at (j).area ()));
242
230
tracks.back ().add_point (candidate->center_position ()[0 ],candidate->center_position ()[1 ],i+1 ,candidate->area ());
@@ -245,10 +233,9 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
245
233
}
246
234
} // not already attributed
247
235
} // not previously attributed
248
- vector<Points> tamp_vec;
249
- points[i].swap (tamp_vec);
236
+ vector<Points> tamp_vec;
237
+ points[i].swap (tamp_vec); // free memory
250
238
} // loop on frames
251
-
252
239
} // end of function
253
240
254
241
0 commit comments