Skip to content

Commit 126207e

Browse files
committed
Add comments and cleaning the code
1 parent 612c43f commit 126207e

File tree

1 file changed

+44
-57
lines changed

1 file changed

+44
-57
lines changed

sources/link_particules.cpp

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020

2121

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>
2828
#include "point.hpp"
2929
#include <opencv2/core/core.hpp>
3030
#include <opencv2/highgui/highgui.hpp>
@@ -48,6 +48,17 @@ using namespace cv;
4848
// **********************************************/
4949

5050

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+
5162
double cost_function(double * current, double * expected)
5263
{
5364
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)
7384
else return false;
7485
}
7586

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-
9687

9788

9889

@@ -104,53 +95,50 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
10495
/*///////////////////////////////STRATEGY//////////////////////////////
10596
if 0 : neirest neighbor
10697
if 1 : linear prediction
107-
if 2 : quadratic predicition
98+
if 2 : quadratic predicition (not implementated… (Yet?))
10899
*/
109-
//test(0);
110100

111101

112102
double expected_position[2];
113103
unsigned int frame=0;
114-
//initialisation
104+
//initialisation: will assign every point in frame 0 to a new track
115105
for (unsigned int i=0;i<points[0].size();i++)
116106
{
117107
if (points[0].at(i).area()>=size_min && points[0].at(i).area()<=size_max)
118108
{
119109
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;
121110
points[0].at(i).track_index=i;
122111
}
123112
}
124113

125114

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
128117
{
129118

130-
if (mode_low_ram) ia >> points[i+1];
119+
if (mode_low_ram) ia >> points[i+1]; //Backup the archive
131120

132121
if (i%100==0)
133122
{
134123
cout<<"\r"<<i<<"/"<<NB_frames;
135124
fflush(stdout);
136125
}
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…)
138127
{
139128
// 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.
141130
{
142131
//Linking of particules already linked in the precedent frame
143-
//TO change in further developments
144132
if (strategy==1)
145133
{
146-
if (tracks[j].X.size()>1)
134+
if (tracks[j].X.size()>1) //linear interpolation
147135
{
148136
int size=tracks[j].X.size();
149137
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;
150138
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;
151139

152140
}
153-
else
141+
else //track to short to compute the interpolation (2 points needed)
154142
{
155143
expected_position[0]=tracks[j].X.back()+flow_x;
156144
expected_position[1]=tracks[j].Y.back()+flow_y;
@@ -165,17 +153,16 @@ void link_particules(vector<Points> * & points,vector<Track> & tracks,double sea
165153

166154

167155

168-
Points * candidate=NULL;
156+
Points * candidate=NULL; //Windows compatibility…
169157
double candidate_cost=search_radius*2;
170158
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
174161
{
175162
double x_current=points[i+1].at(k).center_position()[0];
176163
double y_current=points[i+1].at(k).center_position()[1];
177164

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
179166
{
180167
//if (cost_function(points[i+1].at(k).center_position(),expected_position)<=candidate_cost)
181168
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
188175
}// end if in area
189176
}//end if not already allocated
190177
}//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
200188
} //end of if on particles who are between N and N-gap
201189
} //end loop on tracks of frame N
202-
190+
203191

204192

205193
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
236224
}// end if in area
237225
}//end if not already allocated
238226
}//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
240228
{
241229
tracks.push_back(Track(points[i].at(j).center_position()[0],points[i].at(j).center_position()[1],i,points[i].at(j).area()));
242230
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
245233
}
246234
} //not already attributed
247235
} //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
250238
} //loop on frames
251-
252239
} //end of function
253240

254241

0 commit comments

Comments
 (0)