OpticalFlow.C
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Robots/Beobot2/Navigation/FOE_Navigation/OpticalFlow.H"
00039
00040
00041 OpticalFlow::OpticalFlow
00042 (std::vector<rutz::shared_ptr<FlowVector> > flowVectors,
00043 Dims dims):
00044 itsFlowVectors(flowVectors),
00045 itsImageDims(dims)
00046 {
00047 itsFlowFieldComputed = false;
00048 itsFlowVectorsComputed = true;
00049
00050 computeFlowLocations();
00051 itsFlowLocationsComputed = true;
00052 }
00053
00054
00055 OpticalFlow::OpticalFlow
00056 (lobot::triple<Image<float>,Image<float>,Image<float> > flowField):
00057 itsFlowField(flowField)
00058 {
00059 itsFlowFieldComputed = true;
00060 itsFlowVectorsComputed = false;
00061 itsFlowLocationsComputed = false;
00062
00063 itsImageDims = itsFlowField.first.getDims();
00064 }
00065
00066
00067 OpticalFlow::~OpticalFlow()
00068 { }
00069
00070
00071 void OpticalFlow::computeFlowLocations()
00072 {
00073
00074 if(!itsFlowVectorsComputed)
00075 {
00076 computeFlowVectors();
00077 }
00078
00079 itsFlowLocations.clear();
00080 for(uint i = 0; i < itsFlowVectors.size(); i++)
00081 {
00082 itsFlowLocations.push_back(itsFlowVectors[i]->p1);
00083 }
00084 itsFlowLocationsComputed = true;
00085 }
00086
00087
00088 std::vector<Point2D<float> > OpticalFlow::getFlowLocations()
00089 {
00090
00091 if(!itsFlowLocationsComputed)
00092 {
00093 computeFlowLocations();
00094 }
00095
00096 return itsFlowLocations;
00097 }
00098
00099
00100 std::vector<rutz::shared_ptr<FlowVector> > OpticalFlow::getFlowVectors()
00101 {
00102
00103 if(!itsFlowVectorsComputed)
00104 {
00105 computeFlowVectors();
00106 }
00107
00108 return itsFlowVectors;
00109 }
00110
00111
00112 Dims OpticalFlow::getImageDims()
00113 {
00114 return itsImageDims;
00115 }
00116
00117
00118 void OpticalFlow::computeFlowVectors()
00119 {
00120 LFATAL("NEED DEBUGGING TO CONFIRM THAT IT WORKS - then delete this line");
00121
00122 itsFlowVectors.clear();
00123
00124
00125 Image<float> dir = itsFlowField.first;
00126 Image<float> len = itsFlowField.second;
00127 Image<float> val = itsFlowField.third;
00128 Image<float>::iterator dptr = dir.beginw(), stop = dir.endw();
00129 Image<float>::iterator lptr = len.beginw();
00130 Image<float>::iterator vptr = val.beginw();
00131
00132
00133 uint height = dir.getHeight();
00134 uint i = 0; uint j = 0;
00135 while(dptr != stop)
00136 {
00137 float fval = *vptr++;
00138
00139 if(fval > 0.0)
00140 {
00141 rutz::shared_ptr<FlowVector>
00142 (new FlowVector
00143 (Point2D<float>(i,j), *dptr++, *lptr++, fval));
00144 }
00145
00146 if(j >= height){ j = 0; i++; }
00147 }
00148 itsImageDims = dir.getDims();
00149
00150 itsFlowVectorsComputed = true;
00151 }
00152
00153
00154 void OpticalFlow::computeFlowField()
00155 {
00156 LFATAL("NEED DEBUGGING TO CONFIRM THAT IT WORKS - then delete this line");
00157
00158
00159 Image<float> dir(itsImageDims, ZEROS);
00160 Image<float> len(itsImageDims, ZEROS);
00161 Image<float> val(itsImageDims, ZEROS);
00162
00163
00164
00165 for(uint v = 0; v < itsFlowVectors.size(); v++)
00166 {
00167 Point2D<float> pt = itsFlowVectors[v]->p1;
00168 uint i = pt.i;
00169 uint j = pt.j;
00170 float ang = itsFlowVectors[v]->angle;
00171 float mag = itsFlowVectors[v]->mag;
00172 float fval = itsFlowVectors[v]->val;
00173
00174 if(fval > 0.0)
00175 {
00176 dir.setVal(i,j, ang );
00177 len.setVal(i,j, mag );
00178 val.setVal(i,j, fval);
00179 }
00180 }
00181 itsImageDims = dir.getDims();
00182
00183 itsFlowFieldComputed = true;
00184 }
00185
00186
00187
00188 lobot::triple<Image<float>,Image<float>,Image<float> >
00189 OpticalFlow::getFlowField()
00190 {
00191
00192 if(!itsFlowFieldComputed)
00193 {
00194 computeFlowField();
00195 }
00196
00197 return itsFlowField;
00198 }
00199
00200
00201 Image<float> OpticalFlow::getDirectionField()
00202 {
00203
00204 if(!itsFlowFieldComputed)
00205 {
00206 computeFlowField();
00207 }
00208
00209 return itsFlowField.first;
00210 }
00211
00212
00213 Image<float> OpticalFlow::getVectorLengthField()
00214 {
00215
00216 if(!itsFlowFieldComputed)
00217 {
00218 computeFlowField();
00219 }
00220
00221 return itsFlowField.second;
00222 }
00223
00224
00225 Image<float> OpticalFlow::getFlowStrengthField()
00226 {
00227
00228 if(!itsFlowFieldComputed)
00229 {
00230 computeFlowField();
00231 }
00232
00233 return itsFlowField.third;
00234 }
00235
00236
00237
00238
00239
00240