contrib/gel/vsrl/vsrl_stereo_dense_matcher.cxx

Go to the documentation of this file.
00001 #include "vsrl_stereo_dense_matcher.h"
00002 //:
00003 // \file
00004 
00005 #include "vsrl_parameters.h"
00006 #include <vil1/vil1_save.h>
00007 #include <vcl_iostream.h>
00008 
00009 vsrl_stereo_dense_matcher::vsrl_stereo_dense_matcher(const vil1_image &im1, const vil1_image &im2):
00010   vsrl_dense_matcher(im1),
00011   image_correlation_(im1,im2)
00012 {
00013   raster_array_=0;
00014   num_raster_=0;
00015 
00016   correlation_range_= vsrl_parameters::instance()->correlation_range; // probaly 10
00017 }
00018 
00019 vsrl_stereo_dense_matcher::~vsrl_stereo_dense_matcher()
00020 {
00021   if (raster_array_)
00022   {
00023     for (int i=0;i<num_raster_;i++)
00024       delete raster_array_[i];
00025     delete [] raster_array_;
00026   }
00027 }
00028 
00029 
00030 void vsrl_stereo_dense_matcher::execute()
00031 {
00032   // we want to perform the dense matching between the two images
00033 
00034   if (!raster_array_)
00035     // we must perform some initial calculations
00036     this->initial_calculations();
00037 
00038   // start the dynamic program for each raster
00039 
00040   vcl_cout << "Performing dynamic programs\n";
00041 
00042   for (int i=0;i<num_raster_;i++)
00043     evaluate_raster(i);
00044 }
00045 
00046 void vsrl_stereo_dense_matcher::initial_calculations()
00047 {
00048   // we want to perform the dense matching between the two images
00049 
00050   // step 1 - compute the correlations between the two images
00051   //          so that the dynamic programs can perform their calculations efficiently
00052 
00053   vcl_cout << "Performing image correlations\n";
00054 
00055   image_correlation_.set_correlation_range(correlation_range_);
00056 
00057   image_correlation_.initial_calculations();
00058 
00059 
00060   // step 2 - create an array of dynamic programs that process each raster
00061   // and initialize them to zero
00062 
00063   num_raster_ = image_correlation_.get_image1_height();
00064 
00065   typedef vsrl_raster_dp_setup* raster_ptr;
00066   raster_array_ = new raster_ptr[num_raster_];
00067 
00068   for (int i=0;i<num_raster_;i++)
00069     raster_array_[i]=0;
00070 }
00071 
00072 int vsrl_stereo_dense_matcher::get_disparity(int x,int y)
00073 {
00074   int new_x = get_assignment(x,y);
00075 
00076   if (new_x >=0)
00077     return get_assignment(x,y)-x;
00078   else
00079     return 0-1000;
00080 }
00081 
00082 int vsrl_stereo_dense_matcher::get_assignment(int x, int y)
00083 {
00084   // we want to get the assignment of pixel x from raster y
00085 
00086   if (y<0 || y >=num_raster_)
00087     return 0-1;
00088   else
00089   {
00090     if (!(raster_array_[y]))
00091       // we need to perform the dynamic program on the raster
00092       this->evaluate_raster(y);
00093 
00094     return raster_array_[y]->get_assignment(x);
00095   }
00096 }
00097 
00098 
00099 void vsrl_stereo_dense_matcher::evaluate_raster(int i)
00100 {
00101   if (i<0 || i>= num_raster_)
00102     vcl_cout << "Warning tried to evaluate inapropriate raster\n";
00103 
00104   // we want to evaulate the raster i
00105 
00106   vcl_cout << "evaluating raster " << i << vcl_endl;
00107 
00108   // set up the i'th raster array
00109   vsrl_raster_dp_setup *raster = new vsrl_raster_dp_setup(i, &image_correlation_);
00110 
00111   // if the previous or the next raster has been computed,
00112   // we wish to use this information to bias the new raster
00113 
00114   if (i>0)
00115     if (raster_array_[i-1])
00116       raster->set_prior_raster(raster_array_[i-1]);
00117 
00118   if (i<num_raster_-1)
00119     if (raster_array_[i+1])
00120       raster->set_prior_raster(raster_array_[i+1]);
00121 
00122   // set the correlation range for the raster
00123   raster->set_search_range(correlation_range_);
00124 
00125   // performing the dynamic program
00126   raster->execute();
00127 
00128   // keep track of the raster
00129   raster_array_[i]=raster;
00130 }
00131 
00132 
00133 void vsrl_stereo_dense_matcher::write_disparity_image(char *filename)
00134 {
00135   // we want to write a disparity image
00136 
00137   // make a buffer which has the size of image1
00138 
00139   vil1_byte_buffer buffer(image1_);
00140 
00141   for (int x=0;x<buffer.width();x++)
00142     for (int y=0;y<buffer.height();y++)
00143       buffer(x,y)=0;
00144 
00145   // go through each point, get the disparity and save it into the buffer
00146 
00147   for (int y=0;y<buffer.height();y++)
00148      for (int x=0;x<buffer.width();x++)
00149      {
00150        int disparity = this->get_disparity(x,y);
00151        int value = disparity + correlation_range_+1;
00152        if (value < 0)
00153          value = 0;
00154        if (value>2*correlation_range_+1)
00155          value=0;
00156        buffer(x,y)=value;
00157      }
00158 
00159   // save the file, using file name extension to determine type
00160   // vil1_save(buffer, filename, image1_.file_format());
00161   vil1_save(buffer, filename);
00162 }
00163 
00164 // print out the correlation costs for point x,y
00165 
00166 void vsrl_stereo_dense_matcher::print_correlation_cost(int x, int y)
00167 {
00168   vcl_cout << "Correlation costs for pixel " << x << ' ' << y << vcl_endl;
00169 
00170   for (int disp = 0-correlation_range_;disp < correlation_range_;disp++)
00171     vcl_cout << disp << " -> " << image_correlation_.get_correlation(x,y,disp) << vcl_endl;
00172 }

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