contrib/gel/vsrl/vsrl_saliency_diffusion.cxx

Go to the documentation of this file.
00001 // This is gel/vsrl/vsrl_saliency_diffusion.cxx
00002 #include "vsrl_saliency_diffusion.h"
00003 #include <vcl_iostream.h>
00004 
00005 vsrl_saliency_diffusion::vsrl_saliency_diffusion(vsrl_dense_matcher *matcher):
00006   vsrl_diffusion(matcher), saliency_(0)
00007 {
00008   disparity_matrix_->fill(0.0);
00009 }
00010 
00011 vsrl_saliency_diffusion::~vsrl_saliency_diffusion()
00012 {
00013 }
00014 
00015 void vsrl_saliency_diffusion::set_initial_disparity(vsrl_diffusion *step_diff)
00016 {
00017   // we want to set the initial disparity
00018 
00019   for (int x=0;x<get_width();x++)
00020   {
00021     for (int y=0;y<get_height();y++)
00022     {
00023       double dd = step_diff->get_disparity(x,y);
00024       (*disparity_matrix_)(x,y)=dd;
00025     }
00026   }
00027 }
00028 
00029 void vsrl_saliency_diffusion::set_saliency(vsrl_token_saliency *saliency)
00030 {
00031   // set the object that knows whether or not a pixel is salient
00032   saliency_ = saliency;
00033 }
00034 
00035 void vsrl_saliency_diffusion::diffuse_disparity(int num_iter)
00036 {
00037   // OK we want to use diffusion so that disparity
00038   // will propogate from stong regions of salienct
00039   // to regions of low disparity
00040 
00041   vcl_cout << "Starting to diffuse\n";
00042 
00043   vnl_matrix<double> mat1= (*disparity_matrix_);
00044   vnl_matrix<double> mat2= mat1;
00045 
00046   vnl_matrix<double> *mstar1 = (&mat1);
00047   vnl_matrix<double> *mstar2 = (&mat2);
00048   vnl_matrix<double> *hold;
00049 
00050   // OK start to diffuse
00051 
00052    for (int dif_num=0;dif_num<num_iter;dif_num++)
00053    {
00054      vcl_cout << "Saliency Diffusion Iteration " << dif_num << vcl_endl
00055               << " disparity for pixel 700,353 is " << (*mstar1)(700,353) << vcl_endl;
00056 
00057      // make an image of the current disparity
00058 
00059      write_image("/projects/IUP2/peter_tu/tmp/fig",dif_num,mstar1);
00060 
00061      // start to diffuse
00062 
00063      int dif_range = 5; // The range that we are will to diffuse over
00064 
00065      for (int x=dif_range;x<get_width() -dif_range;x++)
00066      {
00067        for (int y=dif_range;y<get_height() - dif_range;y++)
00068        {
00069          // only process non salient pixels
00070 
00071          if (!(saliency_->get_saliency(x,y)))
00072          {
00073            // get the average value of mat1(x,y)'s neighborhood
00074            int N=0;
00075            double sum=0;
00076 
00077            for (int i=x-dif_range;i<x+dif_range+1;i++)
00078            {
00079              for (int j=y-dif_range;j<y+dif_range+1;j++)
00080              {
00081                double val = (*mstar1)(i,j);
00082                if (val!=1000)
00083                {
00084                  // this pixel has some information to offer so diffuse naturally
00085                  sum+=val;
00086                  N++;
00087                }
00088              }
00089            }
00090            if (N!=0)
00091            {
00092              // we have some information: store the diffused value
00093              (*mstar2)(x,y)=sum/N;
00094            }
00095          }
00096        }
00097      }
00098 
00099      // OK Swap mstar1 and mstar2
00100      hold=mstar1;
00101      mstar1=mstar2;
00102      mstar2=hold;
00103    }
00104 
00105    // get rid of the pockets of unitialized data
00106    for (int x=0;x<get_width();x++)
00107    {
00108      for (int y=0;y<get_height();y++)
00109      {
00110        double val = (*mstar1)(x,y);
00111        if (val==1000)
00112          (*mstar1)(x,y)=0;
00113      }
00114    }
00115 
00116    // copy the new results
00117    (*disparity_matrix_)=(*mstar1);
00118 
00119    vcl_cout << "Finished the diffusion\n";
00120 }
00121 
00122 void vsrl_saliency_diffusion::execute(int num_iter)
00123 {
00124   // execute the disparity stuff
00125 
00126   // modify the initial disparity_matrix using the results of
00127   // the saliency object
00128 
00129   if (saliency_)
00130     consider_saliency();
00131 
00132   // run a diffusion algorithm
00133   diffuse_disparity(num_iter);
00134 }
00135 
00136 void vsrl_saliency_diffusion::consider_saliency()
00137 {
00138   // each pixel is marked if it is not salient
00139 
00140   for (int x=0;x<get_width();x++)
00141     for (int y=0;y<get_height();y++)
00142       if (!(saliency_->get_saliency(x,y)))
00143         // this pixel is not salient so mark it with 1000 disparity
00144         (*disparity_matrix_)(x,y)=1000; // this value will not be used for diffusion
00145 }

Generated on Mon Mar 8 05:24:39 2010 for contrib/gel/vsrl by  doxygen 1.5.1