TextureTracker.cpp
Go to the documentation of this file.
00001 #include <ros/console.h>
00002 #include <blort/Tracker/TextureTracker.h>
00003 #include <blort/TomGine/tgLighting.h>
00004 #include <blort/TomGine/tgMaterial.h>
00005 #include <blort/TomGine/tgError.h>
00006 
00007 
00008 using namespace std;
00009 using namespace Tracking;
00010 using namespace TomGine;
00011 
00012 // *** PRIVATE ***
00013 
00014 // Draw TrackerModel to screen, extract modelview matrix, perform image processing for model
00015 void TextureTracker::model_processing(ModelEntry* modelEntry){
00016         
00017         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00018         
00019         // Render camera image as background
00020         if(modelEntry->model.getTextured()){
00021                 glDisable(GL_DEPTH_TEST);
00022                 glDepthMask(0);
00023                         m_ip->render(m_tex_frame);
00024                 glDepthMask(1);
00025                 glEnable(GL_DEPTH_TEST);
00026         }
00027         
00028         // Render textured model to screen
00029         m_cam_perspective.Activate();
00030         m_lighting.Activate();
00031         modelEntry->pose.Activate();
00032                 modelEntry->model.restoreTexture();
00033                 modelEntry->model.drawPass();
00034         
00035                 // Extract modelview-projection matrix
00036                 mat4 modelview, projection;
00037                 glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
00038                 glGetFloatv(GL_PROJECTION_MATRIX, projection);
00039                 modelEntry->modelviewprojection = projection * modelview;
00040                 
00041                 // pass new modelview matrix to shader
00042                 m_shadeCompare->bind();
00043                 m_shadeCompare->setUniform("modelviewprojection", modelEntry->modelviewprojection, GL_FALSE); // send matrix to shader
00044                 m_shadeCompare->unbind();
00045                 m_shadeConfidenceMM->bind();
00046                 m_shadeConfidenceMM->setUniform("modelviewprojection", modelEntry->modelviewprojection, GL_FALSE); // send matrix to shader
00047                 m_shadeConfidenceMM->unbind();
00048         modelEntry->pose.Deactivate();
00049         m_lighting.Deactivate();
00050                 
00051         // Copy model rendered into image to texture (=reprojection to model)
00052         m_tex_model->copyTexImage2D(params.camPar.width, params.camPar.height);
00053                 
00054         // perform image processing with reprojected image
00055         glDisable(GL_DEPTH_TEST);
00056         glDepthMask(0);
00057         
00058         // Extract edges in various line widths
00059         if(modelEntry->model.getTextured()){
00060                 m_ip->gauss(m_tex_model, m_tex_model_ip[0]);
00061                 m_ip->sobel(m_tex_model_ip[0], m_tex_model_ip[0], params.model_sobel_th, true);
00062         }else{
00063                 m_ip->sobel(m_tex_model, m_tex_model_ip[0], params.model_sobel_th, true);
00064         }
00065 
00066         if(modelEntry->mask_geometry_edges){
00067                 Texture mask;
00068                 this->computeModelEdgeMask(modelEntry, mask);
00069                 m_ip->thinning(m_tex_model_ip[0], m_tex_model_ip[0], &mask);
00070         }else{
00071                 m_ip->thinning(m_tex_model_ip[0], m_tex_model_ip[0]);
00072         }
00073 
00074         for(unsigned i=1; i<params.num_spreadings; i++)
00075                 m_ip->spreading(m_tex_model_ip[i-1], m_tex_model_ip[i]);
00076 
00077         glDepthMask(1);
00078 }
00079 
00080 // Particle filtering
00081 void TextureTracker::particle_filtering(ModelEntry* modelEntry){
00082         int num_particles;
00083         m_cam_perspective.Activate();
00084         
00085         // Calculate Zoom Direction and pass it to the particle filter
00086         TomGine::tgVector3 vCam = m_cam_perspective.GetPos();
00087         TomGine::tgVector3 vObj = TomGine::tgVector3(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z);
00088         modelEntry->vCam2Model = vObj - vCam;
00089         modelEntry->predictor->setCamViewVector(modelEntry->vCam2Model);
00090         
00091         Particle variation = params.variation;
00092         
00093         for(unsigned i=0; i<modelEntry->num_recursions; i++){
00094 //              unsigned i=0;
00095                 // TODO Evaluate if this makes sense (robustness, accuracy)
00096 //              float c_max = modelEntry->distribution.getMaxC();
00097 //              params.m_spreadlvl = (int)floor((params.num_spreadings)*(0.5-c_max));
00098 //              if(params.m_spreadlvl>=params.num_spreadings)
00099 //                      params.m_spreadlvl = params.num_spreadings-1;
00100 //              else if(params.m_spreadlvl<0)
00101 //                      params.m_spreadlvl = 0;
00102 
00103                 // TODO Evaluate if this makes sense (robustness, accuracy)
00104                 int j = (int)params.num_spreadings - (int)i;
00105                 if(j > 1){
00106                         params.m_spreadlvl = j - 1;
00107                 }else{
00108                         params.m_spreadlvl = 1;
00109                 }
00110                 float varred_t = 1.0f;
00111                 float varred_q = 1.0f;
00112                 float varred_z = 1.0f;
00113                 if(modelEntry->num_recursions > 1){
00114                         varred_t = 1.0f - 0.7f * float(i)/(modelEntry->num_recursions - 1);
00115                         varred_q = 1.0f - 0.8f * float(i)/(modelEntry->num_recursions - 1);
00116                         varred_z = 1.0f - 0.8f * float(i)/(modelEntry->num_recursions - 1);
00117                 }
00118                 variation.t = params.variation.t * varred_t;
00119                 variation.q = params.variation.q * varred_q;
00120                 variation.z = params.variation.z * varred_z;
00121                 
00122 //              variation.z = params.variation.z * varred;
00123 //              ROS_DEBUG("%d %f %f %f\n", params.m_spreadlvl, variation.t.x, variation.r.x*180/PI, varred_t);
00124                 
00125 //              if(i == modelEntry->num_recursions - 1)
00126 //                      m_showparticles = true;
00127 //              else
00128 //                      m_showparticles = false;
00129                 
00130                 //BENCE: commented these, they are not really used
00131 //                m_tex_model_ip[params.m_spreadlvl]->bind(0);
00132 //                m_tex_frame_ip[params.m_spreadlvl]->bind(1);
00133 //                m_tex_model->bind(2);
00134 //                m_tex_frame->bind(3);
00135 
00136                 // TODO Evaluate if this makes sense (robustness, accuracy)
00137 //              params.kernel_size = (int)floor(0.5*(params.max_kernel_size)*(1.0-c_max));
00138 //              m_shadeCompare->bind();
00139 //              m_shadeCompare->setUniform("kernelsize", params.kernel_size);
00140 //              m_shadeCompare->unbind();
00141                 
00142                 // update importance weights and confidence levels
00143 //              ROS_DEBUG("Recursion[%d] %d c:%f p: ", i, params.m_spreadlvl, modelEntry->distribution.getParticle(0).c);
00144 //              modelEntry->distribution.getParticle(0).Print();
00145                 modelEntry->distribution.updateLikelihood(modelEntry->model, m_shadeCompare, 1, params.convergence, m_showparticles);
00146                 
00147                 //modelEntry->distribution = d1;
00148 //              ROS_DEBUG("%f\n", ( 1.0 - i * 0.5 / (float)(modelEntry->num_recursions-1)));
00149                 // TODO Evaluate if this makes sense (accuracy)
00150 //              p = (params.variation * ( 1.0 - i * 0.8 / (float)(modelEntry->num_recursions-1)));
00151                 
00152                 // "Attention mechanism" modifies number of particles indirect proportional to the overall confidence
00153                 //num_particles = modelEntry->num_particles * (1.0 - c_max);
00154                 num_particles = modelEntry->num_particles;
00155                 
00156                 // predict movement of object
00157 
00158                 modelEntry->predictor->resample(modelEntry->distribution, num_particles, variation, (i==0));
00159 
00160                 // set timestep to 0.0 for further recursion (within same image)
00161                 modelEntry->predictor->updateTime(0.0);
00162         }
00163         tgCheckError("TextureTracker::particle_filtering");
00164         // weighted mean
00165         modelEntry->pose_prev = modelEntry->pose;
00166         modelEntry->pose = modelEntry->distribution.getMean();
00167 //      modelEntry->pose = modelEntry->distribution.getParticle(0);
00168 }
00169 
00170 
00171 
00172 // *** PUBLIC ***
00173 
00174 TextureTracker::TextureTracker(){
00175         m_lock = false;
00176         m_showparticles = false;
00177         m_showmodel = 0;
00178         m_draw_edges = false;
00179         m_tracker_initialized = false;
00180         m_drawimage = false;
00181 }
00182 
00183 TextureTracker::~TextureTracker(){
00184         delete(m_tex_model);
00185         for(unsigned i=0; i<params.num_spreadings; i++){
00186                 delete(m_tex_model_ip[i]);
00187         }
00188 }
00189 
00190 // Initialise function (must be called before tracking)
00191 bool TextureTracker::initInternal(){    
00192         
00193         int id;
00194 
00195         // Shader
00196         if((id = g_Resources->AddShader("texEdgeTest", "texEdgeTest.vert", "texEdgeTest.frag")) == -1)
00197                 exit(1);
00198         m_shadeTexEdgeTest = g_Resources->GetShader(id);
00199         
00200         if((id = g_Resources->AddShader("texColorTest", "texColorTest.vert", "texColorTest.frag")) == -1)
00201                 exit(1);
00202         m_shadeTexColorTest = g_Resources->GetShader(id);
00203         
00204         if((id = g_Resources->AddShader("mmConfidence", "mmConfidence.vert", "mmConfidence.frag")) == -1)
00205                 exit(1);
00206         m_shadeConfidenceMM = g_Resources->GetShader(id);
00207         
00208         m_shadeCompare = m_shadeTexEdgeTest;
00209         
00210         // Texture
00211         m_tex_model = new Texture();
00212         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00213         
00214         m_tex_model_ip.push_back( new Texture() );
00215         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00216         for(int i=0; i<(int)params.num_spreadings-1; i++){
00217                 m_tex_model_ip.push_back( new Texture() );
00218                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00219         }
00220         
00221         // Load 
00222         float w = (float)params.camPar.width;
00223         float h = (float)params.camPar.height;
00224         
00225         GLfloat offX[9] = { -1.0f/w, 0.0f, 1.0f/w,
00226                                                 -1.0f/w, 0.0f, 1.0f/w,
00227                                                 -1.0f/w, 0.0f, 1.0f/w };
00228         GLfloat offY[9] = {  1.0f/h, 1.0f/h,  1.0f/h,
00229                                                  0.0f,   0.0f,    0.0f,
00230                                                 -1.0f/h,-1.0f/h, -1.0f/h };
00231                         
00232         m_shadeTexEdgeTest->bind();
00233         m_shadeTexEdgeTest->setUniform("tex_model_edge", 0);
00234         m_shadeTexEdgeTest->setUniform("tex_frame_edge", 1);
00235         m_shadeTexEdgeTest->setUniform("fTol", params.edge_tolerance);
00236         m_shadeTexEdgeTest->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00237         m_shadeTexEdgeTest->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00238         m_shadeTexEdgeTest->setUniform("drawcolor", vec4(1.0f,0.0f,0.0f,0.0f));
00239         m_shadeTexEdgeTest->setUniform("kernelsize", (GLint)params.kernel_size);
00240         m_shadeTexEdgeTest->unbind();
00241         
00242         m_shadeTexColorTest->bind();
00243         m_shadeTexColorTest->setUniform("tex_model_color", 2);
00244         m_shadeTexColorTest->setUniform("tex_frame_color", 3);
00245         m_shadeTexColorTest->setUniform("fTol", params.edge_tolerance);
00246         m_shadeTexColorTest->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00247         m_shadeTexColorTest->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00248         m_shadeTexColorTest->setUniform("drawcolor", vec4(0.0f,0.0f,1.0f,0.0f));
00249         m_shadeTexColorTest->setUniform("kernelsize", (GLint)params.kernel_size);
00250         m_shadeTexColorTest->unbind();
00251         
00252         m_shadeConfidenceMM->bind();
00253         m_shadeConfidenceMM->setUniform("tex_model_color", 2);
00254         m_shadeConfidenceMM->setUniform("tex_frame_color", 3);
00255         m_shadeConfidenceMM->setUniform("fTol", params.edge_tolerance);
00256         m_shadeConfidenceMM->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00257         m_shadeConfidenceMM->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00258         m_shadeConfidenceMM->setUniform("drawcolor", vec4(0.0f,0.0f,1.0f,0.0f));
00259         m_shadeConfidenceMM->setUniform("kernelsize", (GLint)params.kernel_size);
00260         m_shadeConfidenceMM->unbind();
00261         
00262         tgLight light;
00263         tgVector3 cam_f = m_cam_perspective.GetF();
00264         light.ambient = vec4(0.4f,0.4f,0.4f,1.0f);
00265         light.diffuse = vec4(1.0f,1.0f,1.0f,1.0f);
00266         light.specular = vec4(1.0f,1.0f,1.0f,1.0f);
00267         light.position = vec4(-cam_f.x, -cam_f.y, -cam_f.z, 1.0f);
00268         m_lighting.ApplyLight(light,0);
00269         
00270         return true;
00271 }
00272 
00273 float TextureTracker::evaluateParticle(ModelEntry* modelEntry)
00274 {
00275         return evaluateParticle(modelEntry, m_shadeCompare);
00276 }
00277 
00278 // evaluate single particle
00279 float TextureTracker::evaluateParticle(ModelEntry* modelEntry, Shader* shader){
00280         unsigned int queryMatches;
00281         unsigned int queryEdges;
00282         int v, d;
00283         float c;
00284         
00285         glGenQueriesARB(1, &queryMatches);
00286         glGenQueriesARB(1, &queryEdges);
00287 
00288         m_cam_perspective.Activate();
00289         //glViewport(0,0,256,256);
00290         glColorMask(0,0,0,0); glDepthMask(0);
00291         glColor3f(1.0,1.0,1.0);
00292 
00293         m_tex_model_ip[params.m_spreadlvl]->bind(0);
00294         m_tex_frame_ip[params.m_spreadlvl]->bind(1);    
00295         m_tex_model->bind(2);
00296         m_tex_frame->bind(3);
00297         
00298         // Draw particles and count pixels
00299         shader->bind();
00300         shader->setUniform("analyze", false);
00301     
00302         modelEntry->pose.Activate();
00303         
00304                 // Draw all model edge pixels
00305                 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queryEdges);
00306                 shader->setUniform("compare", false);
00307                 if(m_showparticles)
00308                         glColorMask(1,1,1,1);
00309                 modelEntry->model.drawTexturedFaces();
00310                 modelEntry->model.drawUntexturedFaces();
00311                 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
00312                 
00313                 glColorMask(0,0,0,0);
00314                 
00315                 // Draw matching model edge pixels using a shader
00316                 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queryMatches);
00317                 shader->setUniform("compare", true);
00318                 shader->setUniform("textured", true);
00319                 modelEntry->model.drawTexturedFaces();
00320                 shader->setUniform("textured", false);
00321                 modelEntry->model.drawUntexturedFaces();
00322                 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
00323         
00324         modelEntry->pose.Deactivate();
00325 
00326         shader->unbind();
00327 
00328         glGetQueryObjectivARB(queryMatches, GL_QUERY_RESULT_ARB, &d);
00329         glGetQueryObjectivARB(queryEdges, GL_QUERY_RESULT_ARB, &v);
00330         glColorMask(1,1,1,1); glDepthMask(1);
00331         glDeleteQueriesARB(1, &queryMatches);
00332         glDeleteQueriesARB(1, &queryEdges);
00333         
00334         c = modelEntry->distribution.confidenceFunction(d,v);
00335         
00336 
00337         
00338         return c;
00339 }
00340 
00341 // Process camera image (gauss, sobel, thinning, spreading, rendering)
00342 void TextureTracker::image_processing(unsigned char* image, GLenum format){
00343         
00344         // Load camera image to texture
00345         m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00346         
00347         // Preprocessing for camera image
00348         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00349         glDisable(GL_DEPTH_TEST);
00350         glDepthMask(0);
00351         glColor3f(1.0,1.0,1.0);
00352         
00353         m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00354         
00355         m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00356         m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00357         m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00358         for(unsigned i=1; i<params.num_spreadings; i++)
00359                 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00360         
00361         glDepthMask(1);
00362 }
00363 
00364 // Process camera image (gauss, sobel, thinning, spreading, rendering)
00365 void TextureTracker::image_processing(unsigned char* image, const TomGine::tgModel &m, const tgPose &p, GLenum format){
00366         
00367         // Load camera image to texture
00368         m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00369         
00370         // Preprocessing for camera image
00371         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00372         glDisable(GL_DEPTH_TEST);
00373         glDepthMask(0);
00374         glColor3f(1.0,1.0,1.0);
00375         
00376         m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00377 
00378         // Draw model (i.e. a virutal occluder)
00379         glDepthMask(1);
00380         glEnable(GL_DEPTH_TEST);
00381         drawModel(m,p);
00382         glDisable(GL_DEPTH_TEST);
00383         glDepthMask(0);
00384         m_tex_frame->copyTexImage2D(params.camPar.width, params.camPar.height);
00385 
00386         m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00387         m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00388         m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00389         for(unsigned i=1; i<params.num_spreadings; i++)
00390                 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00391         
00392         glDepthMask(1);
00393 }
00394 
00395 void TextureTracker::image_processing(unsigned char* image, int model_id, const tgPose &p, GLenum format){
00396         
00397         // Load camera image to texture
00398         m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00399         
00400         // Preprocessing for camera image
00401         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00402         glDisable(GL_DEPTH_TEST);
00403         glDepthMask(0);
00404         glColor3f(1.0,1.0,1.0);
00405         
00406         m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00407         
00408 //      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00409 
00410         // Draw model (i.e. a virutal occluder)
00411         ModelEntryList::iterator it = m_modellist.begin();
00412         while(it != m_modellist.end()){
00413                 if(model_id==(*it)->id){
00414                         m_cam_perspective.Activate();
00415                         glClear(GL_DEPTH_BUFFER_BIT);
00416                         m_lighting.Activate();
00417                         glEnable(GL_DEPTH_TEST);
00418                         glDepthMask(1);
00419                         p.Activate();
00420                         
00421                         //(*it)->model.restoreTexture();
00422                         (*it)->model.drawPass();
00423                         
00424                         p.Deactivate();
00425                         glDepthMask(0);
00426                         glDisable(GL_DEPTH_TEST);
00427                         m_lighting.Deactivate();
00428                         glClear(GL_DEPTH_BUFFER_BIT);
00429                 }
00430                 ++it;
00431         }
00432         m_tex_frame->copyTexImage2D(params.camPar.width, params.camPar.height);
00433 
00434         m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00435         m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00436         m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00437         for(unsigned i=1; i<params.num_spreadings; i++)
00438                 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00439         
00440         glDepthMask(1);
00441 }
00442 
00443 bool TextureTracker::track()
00444 {
00445   if(!m_tracker_initialized)
00446   {
00447     ROS_ERROR("[TextureTracker::track()] Error tracker not initialised!\n");
00448                 return false;
00449         }
00450         
00451         // Track models
00452         for(unsigned i=0; i<m_modellist.size(); i++){
00453                 track(m_modellist[i]);
00454         }
00455         
00456         //BENCE: this part never runs
00457         // Track hypothesis
00458 //      ModelEntryList::iterator m1 = m_hypotheses.begin();
00459 //      while(m1 < m_hypotheses.end()){
00460 //              track((*m1));
00461 //              if((*m1)->num_convergence++>params.hypotheses_trials){
00462 //                      if((*m1)->past_confidences.size() < params.hypotheses_trials){
00463 //
00464 //                              (*m1)->past_confidences.push_back((*m1)->distribution.getMaxC());
00465 //                              m_modellist[(*m1)->hypothesis_id]->past_confidences.push_back(m_modellist[(*m1)->hypothesis_id]->distribution.getMaxC());
00466 //                      }else{
00467 //
00468 //                              // Evaluate mean confidence of hypothesis
00469 //                              float c_hyp = 0.0;
00470 //                              int s = (*m1)->past_confidences.size();
00471 //                              for(int j=0; j<s; j++){
00472 //                                      c_hyp += (*m1)->past_confidences[j];
00473 //                              }
00474 //                              if(s>0)
00475 //                                      c_hyp = c_hyp / (float)s;
00476 //
00477 //                              // Evaluate mean confidence of model, the hypothesis belongs to
00478 //                              float c_model = 0.0;
00479 //                              s = m_modellist[(*m1)->hypothesis_id]->past_confidences.size();
00480 //                              for(int j=0; j<s; j++){
00481 //                                      c_model += m_modellist[(*m1)->hypothesis_id]->past_confidences[j];
00482 //                              }
00483 //                              if(s>0)
00484 //                                      c_model = c_model / (float)s;
00485 //
00486 //                              // Compare confidence of model to the hypothesis
00487 //                              if(c_model >= c_hyp){
00488 //                                      // if model is more confident, delete hypothesis
00489 //                                      delete(*m1);
00490 //                                      m_hypotheses.erase(m1);
00491 //                              }else{
00492 //                                      // if hypothesis is more confident, delete model and replace modellist-entry with hypothesis
00493 //                                      delete(m_modellist[(*m1)->hypothesis_id]);
00494 //                                      (*m1)->id = (*m1)->hypothesis_id;
00495 //                                      m_modellist[(*m1)->hypothesis_id] = (*m1);
00496 //                                      m_hypotheses.erase(m1);
00497 //                              }
00498 //                      }
00499 //              }
00500 //              m1++;
00501 //      }
00502         tgCheckError("TextureTracker::track()");
00503         return true;
00504 }
00505 
00506 bool TextureTracker::track(ModelEntry *modelEntry)
00507 {
00508 
00509         // Process model (texture reprojection, edge detection)
00510   model_processing(modelEntry);
00511         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00512 
00513         if(m_drawimage)
00514                 drawImage(NULL);
00515 
00516         // Apply particle filtering
00517         if(!modelEntry->lock){
00518                 particle_filtering(modelEntry);
00519                 if(!m_cam_perspective.GetFrustum()->PointInFrustum(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z))
00520                         reset();
00521         }else{
00522                 modelEntry->pose.c = evaluateParticle(modelEntry, m_shadeCompare);
00523         }
00524 
00525         modelEntry->confidence_edge = evaluateParticle(modelEntry, m_shadeTexEdgeTest);
00526         modelEntry->confidence_color = evaluateParticle(modelEntry, m_shadeTexColorTest);    
00527         
00528         modelEntry->filter_pose();
00529   modelEntry->evaluate_states(params.variation, params.num_recursions,
00530                               params.c_th_base, params.c_th_min, params.c_th_fair,
00531                               params.c_mv_not, params.c_mv_slow, params.c_th_lost);
00532 
00533   m_ftime = (float)m_timer.Update();
00534         return true;
00535 }
00536 
00537 bool TextureTracker::track(int id)
00538 {
00539   if(!m_tracker_initialized)
00540   {
00541     ROS_ERROR("[TextureTracker::track(int)] Error tracker not initialised!\n");
00542                 return false;
00543         }
00544         
00545         ModelEntryList::iterator it = m_modellist.begin();
00546   while(it != m_modellist.end())
00547   {
00548     if(id==(*it)->id)
00549     {
00550                         track(m_modellist[id]);
00551                         tgCheckError("TextureTracker::track(int)");
00552                         return true;
00553                 }
00554                 ++it;
00555         }
00556         
00557         return false;
00558 }
00559 
00560 // grabs texture from camera image and attaches it to faces of model
00561 void TextureTracker::textureFromImage(bool use_num_pixels){
00562         std::vector<tgVertex> vertices;
00563         
00564         Parameter tmpParams = params;
00565         
00566         params.num_particles = 500;
00567         params.num_recursions = 10;
00568         params.m_spreadlvl = 0;
00569         params.variation = params.variation * 0.1;
00570         setKernelSize(0);
00571 
00572         track();
00573 
00574         params = tmpParams;
00575         setKernelSize(params.kernel_size);
00576         
00577         for(unsigned i=0; i<m_modellist.size(); i++){
00578 
00579                 m_cam_perspective.Activate();
00580                 vector<unsigned> faceUpdateList = m_modellist[i]->model.getFaceUpdateList(m_modellist[i]->pose, 
00581                                         vec3(m_modellist[i]->vCam2Model.x, m_modellist[i]->vCam2Model.y, m_modellist[i]->vCam2Model.z),
00582                                         params.minTexGrabAngle,
00583                                         use_num_pixels);
00584 
00585                 if(!faceUpdateList.empty()){
00586                         vertices.clear();
00587                         m_modellist[i]->model.textureFromImage( m_tex_frame,
00588                                                                 params.camPar.width, params.camPar.height,
00589                                                                 m_modellist[i]->pose,
00590                                                                 vec3(m_modellist[i]->vCam2Model.x, m_modellist[i]->vCam2Model.y, m_modellist[i]->vCam2Model.z),
00591                                                                 params.minTexGrabAngle,
00592                                                                 faceUpdateList,
00593                                                                 vertices,
00594                                                                 &m_cam_perspective);
00595                 }
00596                 faceUpdateList.clear();
00597         }
00598 }
00599 
00600 // grabs texture from camera image and attaches it to faces of model
00601 void TextureTracker::textureFromImage(int id, const TomGine::tgPose &pose, bool use_num_pixels){
00602         std::vector<tgVertex> vertices;
00603         
00604         ModelEntry* modelEntry = 0;
00605         ModelEntryList::iterator it = m_modellist.begin();
00606         while(it != m_modellist.end()){
00607                 if(id==(*it)->id){
00608                         modelEntry = (*it);
00609                 }
00610                 ++it;
00611         }
00612         
00613         modelEntry->pose = pose;
00614         
00615         m_cam_perspective.Activate();
00616         vector<unsigned> faceUpdateList = modelEntry->model.getFaceUpdateList(modelEntry->pose, 
00617                                 vec3(modelEntry->vCam2Model.x, modelEntry->vCam2Model.y, modelEntry->vCam2Model.z),
00618                                 params.minTexGrabAngle,
00619                                 use_num_pixels);
00620 
00621         if(!faceUpdateList.empty()){
00622                 vertices.clear();
00623 
00624                 modelEntry->model.textureFromImage(m_tex_frame,
00625                                                    params.camPar.width, params.camPar.height,
00626                                                    modelEntry->pose,
00627                                                    vec3(modelEntry->vCam2Model.x, modelEntry->vCam2Model.y, modelEntry->vCam2Model.z),
00628                                                    params.minTexGrabAngle,
00629                                                    faceUpdateList,
00630                                                    vertices,
00631                                                    &m_cam_perspective);
00632         }
00633         faceUpdateList.clear();
00634 }
00635 
00636 void TextureTracker::untextureModels(){
00637         for(unsigned i=0; i<m_modellist.size(); i++){
00638                 m_modellist[i]->model.releasePassList();        
00639         }
00640 }
00641 
00642 // Draw result of texture tracking (particle with maximum likelihood)
00643 void TextureTracker::drawResult(float linewidth){
00644                 
00645         m_cam_perspective.Activate();
00646         m_lighting.Activate();
00647 
00648         glEnable(GL_DEPTH_TEST);
00649         glClear(GL_DEPTH_BUFFER_BIT);
00650         
00651         for(unsigned i=0; i<m_modellist.size(); i++){
00652                 drawModelEntry(m_modellist[i], linewidth);
00653         }
00654         
00655 //      for(int i=0; i<m_hypotheses.size(); i++){
00656 //              drawModelEntry(m_hypotheses[i]);
00657 //      }
00658         
00659         m_lighting.Deactivate();
00660         tgCheckError("TextureTracker::drawResult B");
00661 }
00662 
00663 void TextureTracker::drawModelEntry(ModelEntry* modelEntry, float linewidth){
00664 
00665                 modelEntry->pose.Activate();
00666                 
00667                 switch(m_showmodel){
00668                         case 0:
00669                                 modelEntry->model.restoreTexture();
00670                                 modelEntry->model.drawPass(false);
00671                                 break;
00672                         case 1:
00673                                 m_lighting.Deactivate();
00674                                 //glColorMask(0,0,0,0);
00675                                 //modelEntry->model.drawFaces();
00676                                 //glColorMask(1,1,1,1);
00677                                 if(linewidth<1.0f)
00678                                         linewidth = 1.0f;
00679                                 glLineWidth(linewidth);
00680                                 modelEntry->model.drawEdges();
00681                                 glColor3f(1.0,1.0,1.0);
00682                                 break;
00683                         case 2:
00684                                 m_tex_model_ip[params.m_spreadlvl]->bind(0);
00685                                 m_tex_frame_ip[params.m_spreadlvl]->bind(1);    
00686                                 m_tex_model->bind(2);
00687                                 m_tex_frame->bind(3);
00688                                 m_shadeCompare->bind();
00689                                 m_shadeCompare->setUniform("analyze", true);
00690                                 m_shadeCompare->setUniform("compare", true);
00691                                 m_shadeCompare->setUniform("textured", true);
00692                                 modelEntry->model.drawTexturedFaces();
00693                                 m_shadeCompare->setUniform("textured", false);
00694                                 modelEntry->model.drawUntexturedFaces();
00695                                 m_shadeCompare->unbind();
00696                                 break;
00697                         case 3:
00698                                 break;
00699                         default:
00700                                 m_showmodel = 0;
00701                                 break;                  
00702                 }
00703                                 
00704 //              m_modellist[i]->model.drawCoordinates();
00705 //              m_modellist[i]->model.drawNormals();
00706                 modelEntry->pose.Deactivate();
00707 }
00708 
00709 void TextureTracker::drawTrackerModel(int id, const TomGine::tgPose &p, float linewidth)
00710 {
00711         m_cam_perspective.Activate();
00712         ModelEntryList::iterator it = m_modellist.begin();
00713         while(it != m_modellist.end()){
00714                 if(id==(*it)->id){
00715                         ModelEntry* modelEntry = (*it);
00716                         p.Activate();
00717                                 switch(m_showmodel){
00718                                         case 0:
00719                                                 modelEntry->model.restoreTexture();
00720                                                 modelEntry->model.drawPass(false);
00721                                                 break;
00722                                         case 1:
00723                                                 m_lighting.Deactivate();
00724                                                 //glColorMask(0,0,0,0);
00725                                                 //modelEntry->model.drawFaces();
00726                                                 //glColorMask(1,1,1,1);
00727                                                 if(linewidth<1.0f)
00728                                                         linewidth = 1.0f;
00729                                                 glLineWidth(linewidth);
00730                                                 modelEntry->model.drawEdges();
00731                                                 glColor3f(1.0,1.0,1.0);
00732                                                 break;
00733                                         case 2:
00734                                         case 3:
00735                                                 break;
00736                                         default:
00737                                                 m_showmodel = 0;
00738                                                 break;                  
00739                                 }
00740                         p.Deactivate();
00741                 }
00742                 ++it;
00743         }
00744 }
00745 
00746 void TextureTracker::evaluatePDF(int id,
00747                                  float x_min, float y_min,
00748                                  float x_max, float y_max,
00749                                  int res,
00750                                  const char* meshfile, const char* xfile)
00751 {
00752         ModelEntryList::iterator it = m_modellist.begin();
00753         while(it != m_modellist.end()){
00754                 if(id==(*it)->id){
00755                         vector<float> pdf;
00756                         pdf = getPDFxy((*it), x_min, y_min, x_max, y_max, res);
00757                         savePDF(pdf, x_min, y_min, x_max, y_max, res, meshfile, xfile);                 
00758                         return;
00759                 }
00760                 ++it;
00761         }
00762 }
00763 
00764 // Plots pdf in x-y plane (with z and rotational DOF locked)
00765 vector<float> TextureTracker::getPDFxy(ModelEntry* modelEntry,
00766                                        float x_min, float y_min,
00767                                        float x_max, float y_max,
00768                                        int res)
00769 {
00770         int i = 0;
00771   ROS_DEBUG("Evaluating PDF constrained to x,y movement only");
00772         float x_step = (x_max-x_min) / res;
00773         float y_step = (y_max-y_min) / res;
00774         float scale = 0.1f;
00775         
00776         x_min = (modelEntry->pose.t.x += x_min);
00777         y_min = (modelEntry->pose.t.y += y_min);
00778         
00779         vector<float> vPDF;
00780         vPDF.assign(res*res, 0.0);
00781         
00782         TomGine::tgFrustum* frustum = m_cam_perspective.GetFrustum();
00783         
00784         
00785         float p;
00786         i=0;
00787         modelEntry->pose.t.y = y_min;
00788         for(int n=0; n<res; n++){
00789                 modelEntry->pose.t.x = x_min;
00790                 for(int m=0; m<res; m++){
00791                         if(frustum->PointInFrustum(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z)){
00792                                 p = evaluateParticle(modelEntry, m_shadeTexEdgeTest) * scale;
00793                         }else{
00794                                 p = 0.0;
00795                         }
00796                         vPDF[i] = p;
00797                         
00798                         i++;
00799                         modelEntry->pose.t.x += x_step;
00800                 }
00801                 modelEntry->pose.t.y += y_step;
00802         }
00803         
00804 //      glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00805 //      glEnable(GL_BLEND);
00806 //      glBegin(GL_QUADS);
00807 //              glColor4f(1.0,0.0,0.0,0.5);
00808 //              glVertex3f(x_min, y_min, 0.0);
00809 //              glVertex3f(x_min+x_step*res, y_min, 0.0);
00810 //              glVertex3f(x_min+x_step*res, y_min+y_step*res, 0.0);
00811 //              glVertex3f(x_min, y_min+y_step*res, 0.0);
00812 //      glEnd();
00813 //      glDisable(GL_BLEND);
00814 //      
00815 //      swap();
00816 //      usleep(3000000);
00817         
00818         return vPDF;
00819 }
00820 
00821 // draws a rectangular terrain using a heightmap
00822 void TextureTracker::savePDF(vector<float> vPDFMap,
00823                              float x_min, float y_min,
00824                              float x_max, float y_max,
00825                              unsigned res,
00826                              const char* meshfile, const char* xfile)
00827 {
00828         unsigned i,d,x,y;
00829         
00830         float x_step = (x_max-x_min) / res;
00831         float y_step = (y_max-y_min) / res;
00832         
00833         vector<vec3> vertexlist;
00834         vector<vec3> normallist;
00835         vector<vec2> texcoordlist;
00836         vector<unsigned int> indexlist;
00837         
00838         for(y=0; y<res; y++){
00839                 for(x=0; x<res; x++){
00840                         vec3 v[5], n;
00841                         vec2 tc;
00842                         d=0;
00843                         
00844                         v[0].x = x_min + float(x)*x_step;
00845                         v[0].y = y_min + float(y)*y_step;
00846                         v[0].z = vPDFMap[y*res+x];
00847                         
00848                         if(x<res-1){
00849                         v[1].x = x_min + float(x+1)*x_step;
00850                         v[1].y = y_min + float(y)*y_step;
00851                         v[1].z = vPDFMap[y*res+(x+1)];
00852                         n += (v[1]-v[0]);
00853                         d++;
00854                         }
00855                         
00856                         if(y<res-1){
00857                         v[2].x = x_min + float(x)*x_step;
00858                         v[2].y = y_min + float(y+1)*y_step;
00859                         v[2].z = vPDFMap[(y+1)*res+x];
00860                         n += (v[2]-v[0]);
00861                         d++;
00862                         }
00863                         
00864                         if(x>0){
00865                         v[3].x = x_min + float(x-1)*x_step;
00866                         v[3].y = y_min + float(y)*y_step;
00867                         v[3].z = vPDFMap[y*res+(x-1)];
00868                         n += (v[3]-v[0]);
00869                         d++;
00870                         }
00871                         
00872                         if(y>0){
00873                         v[4].x = x_min + float(x)*x_step;
00874                         v[4].y = y_min + float(y-1)*y_step;
00875                         v[4].z = vPDFMap[(y-1)*res+x];
00876                         n += (v[4]-v[0]);
00877                         d++;
00878                         }
00879                         
00880                         n = n * (1.0f/float(d));
00881                         
00882                         tc.x = float(x)/res;
00883                         tc.y = float(y)/res;
00884                         
00885                         vertexlist.push_back(v[0]);
00886                         normallist.push_back(n);
00887                         texcoordlist.push_back(tc);
00888                         
00889                         if(x<res-1 && y<res-1){
00890                                 indexlist.push_back(y*res+x);
00891                                 indexlist.push_back(y*res+(x+1));
00892                                 indexlist.push_back((y+1)*res+(x+1));
00893                                 
00894                                 indexlist.push_back(y*res+x);
00895                                 indexlist.push_back((y+1)*res+(x+1));
00896                                 indexlist.push_back((y+1)*res+x);
00897                         }
00898                 }
00899         }
00900         /*
00901         glClear(GL_DEPTH_BUFFER_BIT);
00902         m_lighting.Activate();
00903         
00904         glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &vertexlist[0]);
00905         glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, &normallist[0]);
00906         glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, &texcoordlist[0]);
00907         
00908         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00909         glDrawElements(GL_TRIANGLES, 6 * (xres-1) * (yres-1), GL_UNSIGNED_INT, &indexlist[0]);
00910         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00911         
00912         glDisableClientState(GL_VERTEX_ARRAY);
00913         glDisableClientState(GL_NORMAL_ARRAY);
00914         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00915         
00916         m_lighting.Deactivate();
00917         */
00918         if(meshfile){
00919                 FILE* fd = fopen(meshfile,"w");
00920                 fprintf(fd, "ply\nformat ascii 1.0\n");
00921                 fprintf(fd, "element vertex %d\n", res*res);
00922                 fprintf(fd, "property float x\n");
00923                 fprintf(fd, "property float y\n");
00924                 fprintf(fd, "property float z\n");
00925                 fprintf(fd, "property float nx\n");
00926                 fprintf(fd, "property float ny\n");
00927                 fprintf(fd, "element face %d\n", (res-1)*(res-1)*2);
00928                 fprintf(fd, "property list uchar uint vertex_indices\n");
00929                 fprintf(fd, "end_header\n");
00930                 
00931                 for(i=0; i<vertexlist.size(); i++){
00932                         vec3 v = vertexlist[i];
00933                         vec3 n = normallist[i];
00934                         fprintf(fd, "%f %f %f %f %f\n", v.x, v.y, v.z, n.x, n.y);
00935                 }
00936                 
00937                 for(i=0; i<indexlist.size(); i+=3){
00938                         fprintf(fd, "3 %d %d %d\n", indexlist[i], indexlist[i+1], indexlist[i+2]);
00939                 }
00940                 
00941     ROS_DEBUG("  output written to '%s'", meshfile);
00942                 
00943                 fclose(fd);
00944         }
00945         
00946         if(xfile){
00947                 FILE* fd2 = fopen(xfile,"w");
00948                 y = res>>1;
00949                 for(x=0; x<res; x++){
00950                         fprintf(fd2, "%f\n", vPDFMap[y*res+x]);
00951                 }
00952     ROS_DEBUG("  output written to '%s'", xfile);
00953                 fclose(fd2);
00954         }
00955 }
00956 
00957 // get m_tex_model
00958 cv::Mat TextureTracker::getModelTexture()
00959 {
00960     return m_tex_model->toCvMat();
00961 }


blort
Author(s): Michael Zillich, Thomas Mörwald, Johann Prankl, Andreas Richtsfeld, Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:26