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 #ifndef ROBOTS_BEOBOT2_NAVIGATION_FOENAVIGATION_POPULATIONHEADINGMAP_C_DEFINED
00038 #define ROBOTS_BEOBOT2_NAVIGATION_FOENAVIGATION_POPULATIONHEADINGMAP_C_DEFINED
00039
00040 #define PHM_NUM_DIRECTIONS 4
00041 #define PHM_FOE_STEP 16
00042 #define PHM_NUM_MSTD_POPULATION_NEURONS 20
00043 #define PHM_NUM_MSTD_INPUT_NEURONS 30
00044
00045 #define PHM_MU .20F
00046
00047 #include "Robots/Beobot2/Navigation/FOE_Navigation/PopulationHeadingMap.H"
00048
00049 #include "Image/CutPaste.H"
00050 #include "Image/DrawOps.H"
00051 #include "Image/MatrixOps.H"
00052 #include "Image/LinearAlgebra.H"
00053 #include "Image/ShapeOps.H"
00054
00055 #include "Util/Timer.H"
00056
00057 #include <stdio.h>
00058
00059
00060 PopulationHeadingMap::PopulationHeadingMap(float focalLength):
00061 itsFocalLength(focalLength)
00062 {
00063
00064 }
00065
00066
00067 void PopulationHeadingMap::setFocalLength(float focalLength)
00068 {
00069 itsFocalLength = focalLength;
00070 }
00071
00072
00073 PopulationHeadingMap::~PopulationHeadingMap()
00074 { }
00075
00076
00077
00078
00079 void PopulationHeadingMap::initialize(Dims dims)
00080 {
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 }
00093
00094
00095
00096 void PopulationHeadingMap::getFOE(Image<byte> stim1, Image<byte> stim2)
00097 {
00098 itsCurrentImage = stim1;
00099 getFOE(stim2);
00100 }
00101
00102
00103
00104
00105 void PopulationHeadingMap::getFOE(Image<byte> stim)
00106 {
00107
00108
00109
00110 if(!itsCurrentImage.initialized())
00111 {
00112 itsCurrentImage = stim;
00113 itsPreviousImage = stim;
00114 LINFO("NOT ENOUGH INPUT YET");
00115 return;
00116 }
00117
00118 LINFO("Focal Length: %f", itsFocalLength);
00119
00120 itsPreviousImage = itsCurrentImage;
00121 itsCurrentImage = stim;
00122
00123
00124
00125 itsOpticalFlow =
00126 getCleanOpticFlow(Image<byte>(itsPreviousImage.getDims(),ZEROS));
00127
00128 itsOpticalFlowImage =
00129 drawOpticFlow(itsPreviousImage, itsOpticalFlow);
00130
00131
00132
00133 if(itsWin.is_invalid())
00134 itsWin.reset(new XWinManaged(itsCurrentImage.getDims(),
00135 0, 0, "Pop Heading Map"));
00136 itsWin->setDims(itsCurrentImage.getDims());
00137 itsWin->drawImage(itsOpticalFlowImage,0,0);
00138
00139 LINFO("got the optic flow");
00140 Raster::waitForKey();
00141
00142
00143
00144
00145
00146 computePopulationHeading();
00147
00148
00149
00150 }
00151
00152 void PopulationHeadingMap::computePopulationHeading()
00153 {
00154 itsHeadingDirectionMap =
00155 Image<float>(itsCurrentImage.getWidth() /PHM_FOE_STEP,
00156 itsCurrentImage.getHeight()/PHM_FOE_STEP, ZEROS);
00157
00158 uint border = 2;
00159 uint hdw = itsHeadingDirectionMap.getWidth();
00160 uint hdh = itsHeadingDirectionMap.getHeight();
00161
00162
00163 uint m = PHM_NUM_MSTD_INPUT_NEURONS;
00164
00165
00166 uint n = PHM_NUM_MSTD_POPULATION_NEURONS;
00167
00168
00169 for(uint jx = border; jx < hdw - border; jx++)
00170 {
00171 for(uint jy = border; jy < hdh - border; jy++)
00172 {
00173 float U = 0.0;
00174
00175
00176
00177
00178 std::vector<uint> lind = pickRandomLocations(m);
00179 std::vector<Point2D<float> > points = getLocations(lind);
00180 std::vector<Point2D<float> > flows = getFlowVectors(lind);
00181
00182
00183
00184
00185 Image<float> C = computeC(points, jx, jy);
00186
00187
00188
00189
00190
00191 Image<float> c_perp = computeOrthogonalComplement(C);
00192
00193
00194
00195
00196
00197
00198 for(uint l = 0; l < n; l++)
00199 {
00200
00201 uint nndims = c_perp.getWidth();
00202
00203
00204 uint rvindex = uint(nndims * float(rand())/(RAND_MAX + 1.0F));
00205 Image<double> c_perp_l =
00206 crop(c_perp, Point2D<int>(rvindex, 0), Dims(1,2*m));
00207
00208 Image<double>::iterator cpli = c_perp_l.beginw();
00209
00210
00211 double total = 0.0;
00212 for(uint i = 0; i < m; i++)
00213 {
00214
00215 double dx = flows[i].i;
00216 double dy = flows[i].j;
00217
00218 total += dx*(*cpli++);
00219 total += dy*(*cpli++);
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 }
00230
00231
00232
00233
00234 double ul = sigmoid(PHM_MU - total) + sigmoid(PHM_MU + total) -1.0;
00235
00236
00237
00238 U += ul;
00239 }
00240 LINFO("j[%3d %3d]: U: %f",jx,jy, U/n);
00241
00242
00243
00244 itsHeadingDirectionMap.setVal(jx,jy, U/n);
00245 }
00246 }
00247
00248 if(itsWin.is_invalid())
00249 itsWin.reset(new XWinManaged(itsCurrentImage.getDims(), 0, 0, "Pop Heading Map"));
00250 itsWin->setDims(itsCurrentImage.getDims());
00251 itsWin->drawImage(zoomXY(itsHeadingDirectionMap, PHM_FOE_STEP),0,0);
00252 LINFO("map");
00253 Raster::waitForKey();
00254
00255 }
00256
00257
00258 double PopulationHeadingMap::sigmoid(double x)
00259 {
00260 return 1.0F/(1.0F + pow(M_E,-x));
00261 }
00262
00263
00264 std::vector<uint> PopulationHeadingMap::pickRandomLocations(uint m)
00265 {
00266 std::vector<uint> locIndexes(m);
00267
00268 std::vector<Point2D<float> > flowLocations =
00269 itsOpticalFlow->getFlowLocations();
00270 uint numFlows = flowLocations.size();
00271
00272
00273 std::vector<bool> picked(numFlows);
00274 for(uint i = 0; i < numFlows; i++) picked[i] = false;
00275
00276
00277 for(uint i = 0; i < m; i++)
00278 {
00279 uint rindex =
00280 uint(numFlows * float(rand())/(RAND_MAX + 1.0F));
00281
00282 while(picked[rindex])
00283 rindex = uint(numFlows * float(rand())/(RAND_MAX + 1.0F));
00284
00285 locIndexes[i] = rindex;
00286 picked[rindex] = true;
00287 }
00288
00289 return locIndexes;
00290 }
00291
00292
00293 std::vector<Point2D<float> >
00294 PopulationHeadingMap::getLocations(std::vector<uint> lind)
00295 {
00296 uint m = lind.size();
00297 std::vector<Point2D<float> > locs(m);
00298
00299 std::vector<Point2D<float> > flowLocations =
00300 itsOpticalFlow->getFlowLocations();
00301
00302
00303
00304
00305 for(uint i = 0; i < m; i++)
00306 {
00307 locs[i] = flowLocations[lind[i]];
00308
00309
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 return locs;
00321 }
00322
00323
00324 std::vector<Point2D<float> >
00325 PopulationHeadingMap::getFlowVectors(std::vector<uint> lind)
00326 {
00327 uint m = lind.size();
00328 std::vector<Point2D<float> > flows(m);
00329
00330 std::vector<rutz::shared_ptr<FlowVector> > fv =
00331 itsOpticalFlow->getFlowVectors();
00332
00333
00334 for(uint i = 0; i < m; i++)
00335 {
00336 flows[i] =
00337 Point2D<float>
00338 (fv[lind[i]]->p2.i - fv[lind[i]]->p1.i,
00339 fv[lind[i]]->p2.j - fv[lind[i]]->p1.j);
00340 }
00341
00342 return flows;
00343 }
00344
00345
00346 Image<double>
00347 PopulationHeadingMap::computeC
00348 (std::vector<Point2D<float> > points, uint jx, uint jy)
00349 {
00350
00351 float f = itsFocalLength;
00352
00353
00354 int w = itsCurrentImage.getWidth();
00355 int h = itsCurrentImage.getHeight();
00356
00357
00358 uint m = points.size();
00359 Image<double> C(m+3, 2*m, ZEROS);
00360
00361 double Tx = double(jx)*PHM_FOE_STEP - double(w/2);
00362 double Ty = double(jy)*PHM_FOE_STEP - double(h/2);
00363 double Tz = f;
00364
00365 double wh = w/2;
00366 double hh = h/2;
00367
00368
00369 for(uint i = 0; i < m; i++)
00370 {
00371
00372 double x = points[i].i - wh;
00373 double y = points[i].j - hh;
00374
00375
00376
00377
00378 C.setVal(i, i*2, -f*Tx + x*Tz);
00379 C.setVal(i, i*2+1, -f*Ty + y*Tz);
00380
00381
00382 C.setVal(m, i*2, x*y/f );
00383 C.setVal(m+1, i*2, -f-(x*x/f) );
00384 C.setVal(m+2, i*2, y );
00385 C.setVal(m, i*2+1, f + (y*y/f));
00386 C.setVal(m+1, i*2+1, -x*y/f );
00387 C.setVal(m+2, i*2+1, -x );
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 return C;
00401 }
00402
00403
00404 Image<double>
00405 PopulationHeadingMap::computeOrthogonalComplement(Image<double> C)
00406 {
00407
00408 Image<double> U;
00409 Image<double> S;
00410 Image<double> V;
00411 svd(C, U, S, V, SVD_LAPACK, SVD_FULL);
00412
00413
00414
00415
00416
00417
00418
00419 uint m = C.getHeight();
00420 uint n = C.getWidth();
00421 uint nindex = n;
00422 for(uint i = 0; i < n; i++)
00423 {
00424 double val = S.getVal(i,i);
00425
00426
00427
00428 if(val < 0.00000001) nindex = i;
00429 }
00430
00431 uint nspacedims = m - nindex;
00432
00433
00434
00435 Image<double> Cperp =
00436 crop(U, Point2D<int>(nindex,0),Dims(nspacedims,m));
00437
00438
00439
00440
00441
00442
00443
00444 return Cperp;
00445 }
00446
00447
00448 void PopulationHeadingMap::print(Image<double> img, const std::string& name)
00449 {
00450 LINFO("%s: %d by %d", name.c_str(), img.getWidth(), img.getHeight());
00451 for(int j = 0; j < img.getHeight(); j++)
00452 {
00453 for(int i = 0; i < img.getWidth(); i++)
00454 {
00455 printf("%13.5f ", img.getVal(i,j));
00456 }
00457 printf("\n");
00458 }
00459 }
00460
00461
00462
00463
00464
00465
00466 #endif // ROBOTS_BEOBOT2_NAVIGATION_FOENAVIGATION_POPULATIONHEADINGMAP_C_DEFINED