00001 #include "vsrl_stereo_dense_matcher.h"
00002
00003
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;
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
00033
00034 if (!raster_array_)
00035
00036 this->initial_calculations();
00037
00038
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
00049
00050
00051
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
00061
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
00085
00086 if (y<0 || y >=num_raster_)
00087 return 0-1;
00088 else
00089 {
00090 if (!(raster_array_[y]))
00091
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
00105
00106 vcl_cout << "evaluating raster " << i << vcl_endl;
00107
00108
00109 vsrl_raster_dp_setup *raster = new vsrl_raster_dp_setup(i, &image_correlation_);
00110
00111
00112
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
00123 raster->set_search_range(correlation_range_);
00124
00125
00126 raster->execute();
00127
00128
00129 raster_array_[i]=raster;
00130 }
00131
00132
00133 void vsrl_stereo_dense_matcher::write_disparity_image(char *filename)
00134 {
00135
00136
00137
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
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
00160
00161 vil1_save(buffer, filename);
00162 }
00163
00164
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 }