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 #include "Component/ModelManager.H"
00037 #include "Media/FrameSeries.H"
00038 #include "Transport/FrameInfo.H"
00039 #include "Image/Image.H"
00040 #include "Image/ImageSet.H"
00041 #include "Image/ShapeOps.H"
00042 #include "Image/CutPaste.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/FilterOps.H"
00045 #include "Image/ColorOps.H"
00046 #include "Image/Transforms.H"
00047 #include "Image/MathOps.H"
00048 #include "Image/MatrixOps.H"
00049 #include "Image/Kernels.H"
00050 #include "Image/fancynorm.H"
00051 #include "Image/Layout.H"
00052 #include "Util/log.H"
00053 #include <math.h>
00054 #include <stdlib.h>
00055 
00056 struct ObjState
00057 {
00058   float pos[3];
00059   float side[3];
00060   float ori[3];
00061   float color[3];
00062   float lik;
00063 };
00064 
00065 Image<float> thresh(const Image<float>& src)
00066 {
00067 
00068   Image<float> result = src;
00069   Image<float>::const_iterator sptr = src.begin();
00070   Image<float>::iterator dptr = result.beginw();
00071   int size = src.getSize();
00072 
00073   float min, max;
00074   getMinMax(src, min, max);
00075 
00076   
00077   for (int i = 0; i < size; i ++)
00078   {
00079     if (*sptr/max > 0.10)
00080       *dptr++ = 1.0;
00081     else
00082       *dptr++ = 0;
00083     *sptr++;
00084   }
00085 
00086 
00087   return result;
00088 }
00089 
00090 Image<float> edgeDist(const Image<float>& priorMag, const Image<float>& priorOri,
00091     const Image<float>& dataMag, const Image<float>& dataOri)
00092 {
00093 
00094   Image<float> dataChamfer = chamfer34(thresh(dataMag));
00095   Image<float> priorThresh = thresh(priorMag);
00096 
00097   return dataChamfer*priorThresh;
00098 }
00099 
00100 
00101 
00102 Image<PixRGB<byte> > drawObject(ObjState &objState)
00103 {
00104   Image<PixRGB<byte> > objImg(256,256,ZEROS);
00105   drawPatch(objImg, Point2D<int>((int)objState.pos[0], (int)objState.pos[1]), 40, PixRGB<byte>(255,0,0));
00106 
00107   return objImg;
00108 }
00109 
00110 Image<PixRGB<byte> > drawWorld()
00111 {
00112   Image<PixRGB<byte> > objImg(256,256,ZEROS);
00113 
00114   int x = randomUpToIncluding(256);
00115   int y = randomUpToIncluding(256);
00116   LINFO("Object at %ix%i", x, y);
00117   int size = 40;
00118   PixRGB<byte> color = PixRGB<byte>(255,0,0);
00119 
00120   drawPatch(objImg, Point2D<int>(x,y), size, color);
00121 
00122   
00123   
00124   
00125 
00126   
00127 
00128   return objImg;
00129 }
00130 
00131 Image<float> likelyhood(const Image<PixRGB<byte> > &world, const Image<PixRGB<byte> > &model)
00132 {
00133 
00134   Image<float> worldMag, worldOri;
00135   Image<byte> worldLum = luminance(world);
00136   gradientSobel(worldLum, worldMag, worldOri, 3);
00137 
00138   Image<float> modelMag, modelOri;
00139   Image<byte> modelLum = luminance(model);
00140   gradientSobel(modelLum, modelMag, modelOri, 3);
00141 
00142   
00143   Image<float> diffImg = edgeDist(modelMag, modelOri, worldMag, worldOri);
00144 
00145   return diffImg;
00146 
00147 }
00148 
00149 void normalize(std::vector<ObjState> &particles)
00150 {
00151 
00152   float sum = 0;
00153   for (uint i=0; i<particles.size(); i++)
00154     sum += particles[i].lik;
00155 
00156   for (uint i=0; i<particles.size(); i++)
00157      particles[i].lik /= sum;
00158 }
00159 
00160 int resample(std::vector<ObjState> &particles)
00161 {
00162 
00163   float max = particles[0].lik;
00164   int maxi = 0;
00165   for (uint i=1; i<particles.size(); i++)
00166   {
00167     if (particles[i].lik > max)
00168     {
00169       maxi = i;
00170       max = particles[i].lik;
00171     }
00172 
00173   }
00174 
00175   return maxi;
00176 
00177 }
00178 
00179 Image<float> getLikImg(std::vector<ObjState> &particles)
00180 {
00181 
00182   Image<float> retImg(256,256,ZEROS);
00183 
00184   for (uint i=0; i<particles.size(); i++)
00185     retImg.setVal((int)particles[i].pos[0], (int)particles[i].pos[1],particles[i].lik);
00186 
00187   inplaceNormalize(retImg, 0.0F, 255.0F);
00188 
00189   return retImg;
00190 }
00191 
00192 
00193 int main(const int argc, const char **argv)
00194 {
00195   MYLOGVERB = LOG_INFO;
00196   ModelManager *mgr = new ModelManager("Test ObjRec");
00197 
00198   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00199   mgr->addSubComponent(ofs);
00200 
00201   mgr->exportOptions(MC_RECURSE);
00202 
00203   if (mgr->parseCommandLine(
00204         (const int)argc, (const char**)argv, "", 0, 0) == false)
00205     return 1;
00206   mgr->start();
00207 
00208 
00209   int run = 1;
00210   Image<PixRGB<byte> > worldImg = drawWorld();
00211 
00212   
00213   std::vector<ObjState> particles(100);
00214   for(uint i=0; i<particles.size(); i++)
00215   {
00216     particles[i].pos[0] = randomUpToIncluding(255);
00217     particles[i].pos[1] = randomUpToIncluding(255);
00218     particles[i].lik = 1/(float)particles.size();
00219   }
00220   while(run)
00221   {
00222 
00223     LINFO("Move particles");
00224     for(uint i=0; i<particles.size(); i++)
00225     {
00226       particles[i].pos[0] += randomUpToIncluding(6)-3;
00227       particles[i].pos[1] += randomUpToIncluding(6)-3;
00228 
00229       
00230       if (particles[i].pos[0] < 0) particles[i].pos[0] = 0;
00231       if (particles[i].pos[0] > 255) particles[i].pos[0] = 255;
00232 
00233       if (particles[i].pos[1] < 0) particles[i].pos[1] = 0;
00234       if (particles[i].pos[1] > 255) particles[i].pos[1] = 255;
00235     }
00236 
00237 
00238     LINFO("Evaluate importance weight");
00239     
00240     for(uint i=0; i<particles.size(); i++)
00241     {
00242       Image<PixRGB<byte> > modelImg = drawObject(particles[i]);
00243       Image<float> diffImg = likelyhood(worldImg, modelImg);
00244       float diff = sum(diffImg);
00245       float lik = 1/diff;
00246       particles[i].lik *= lik;
00247     }
00248 
00249     
00250     normalize(particles);
00251 
00252 
00253     float sum = 0;
00254     for(uint i=0; i<particles.size(); i++)
00255       sum += (particles[i].lik)*(particles[i].lik);
00256 
00257     LINFO("Effective N: %f ", 1/sum);
00258     for(uint i=0; i<particles.size(); i++)
00259       printf("%0.2f ", particles[i].lik);
00260     printf("\n");
00261 
00262     
00263     LINFO("resample particles");
00264     int maxParticle = resample(particles);
00265 
00266     LINFO("Show results");
00267     Image<float> likImg = getLikImg(particles);
00268 
00269     
00270 
00271     Image<PixRGB<byte> > probImg = drawObject(particles[maxParticle]);
00272 
00273     Layout<PixRGB<byte> > outDisp;
00274     outDisp = vcat(outDisp, hcat(worldImg, toRGB(Image<byte>(likImg))));
00275     outDisp = vcat(outDisp, hcat(worldImg, probImg));
00276 
00277     ofs->writeRgbLayout(outDisp, "Result", FrameInfo("Result", SRC_POS));
00278     getchar();
00279   }
00280 
00281   
00282 
00283 
00284   
00285   
00286   
00287 
00288   
00289 
00290   
00291   
00292   
00293 
00294   
00295   
00296 
00297   
00298   
00299   
00300   
00301 
00302 
00303   
00304   
00305   
00306   
00307 
00308   
00309 
00310   
00311   
00312   
00313   
00314   
00315 
00316   
00317 
00318   
00319 
00320   
00321   
00322   
00323 
00324 
00325   
00326 
00327   
00328   
00329   
00330 
00331   exit(0);
00332 
00333 }
00334 
00335