Go to the documentation of this file.00001 #include "brct_dense_reconstructor.h"
00002
00003
00004 #include <vcl_iostream.h>
00005 #include <vil1/vil1_save.h>
00006 #include <vsol/vsol_point_2d.h>
00007 #include <vsrl/vsrl_parameters.h>
00008 #include <brip/brip_vil1_float_ops.h>
00009
00010 brct_dense_reconstructor::brct_dense_reconstructor(const vil1_image &im1,
00011 const vil1_image &im2):
00012 vsrl_dense_matcher(brip_vil1_float_ops::convert_to_grey(im1)),
00013 image_correlation_(image1_, brip_vil1_float_ops::convert_to_grey(im2))
00014 {
00015 raster_array_=0;
00016 num_raster_=0;
00017 correlation_range_= vsrl_parameters::instance()->correlation_range;
00018 }
00019
00020 brct_dense_reconstructor::~brct_dense_reconstructor()
00021 {
00022 if (raster_array_)
00023 {
00024 for (int i=0;i<num_raster_;i++)
00025 delete raster_array_[i];
00026 delete [] raster_array_;
00027 }
00028 }
00029
00030
00031 void brct_dense_reconstructor::set_search_range(const int range)
00032 {
00033 vsrl_parameters::instance()->correlation_range = range;
00034 }
00035
00036
00037 void brct_dense_reconstructor::set_correlation_window_radius(const int radius)
00038 {
00039 int w = 2*radius + 1;
00040 vsrl_parameters::instance()->correlation_window_width = w;
00041 vsrl_parameters::instance()->correlation_window_height = w;
00042 }
00043
00044
00045 void brct_dense_reconstructor::set_inner_cost(const double inner_cost)
00046 {
00047 vsrl_parameters::instance()->inner_cost = inner_cost;
00048 }
00049
00050
00051 void brct_dense_reconstructor::set_outer_cost(const double outer_cost)
00052 {
00053 vsrl_parameters::instance()->outer_cost = outer_cost;
00054 }
00055
00056
00057 void brct_dense_reconstructor::set_continuity_cost(const double continuity_cost)
00058 {
00059 vsrl_parameters::instance()->continuity_cost = continuity_cost;
00060 }
00061
00062 void brct_dense_reconstructor::print_params()
00063 {
00064
00065 }
00066
00067 void brct_dense_reconstructor::execute()
00068 {
00069
00070
00071 if (!raster_array_)
00072
00073 this->initial_calculations();
00074
00075
00076
00077 vcl_cout << "Performing dynamic programs\n";
00078
00079 for (int i=0;i<num_raster_;i++)
00080 evaluate_raster(i);
00081 }
00082
00083 void brct_dense_reconstructor::initial_calculations()
00084 {
00085
00086
00087
00088
00089
00090 vcl_cout << "Performing image correlations\n";
00091
00092 image_correlation_.set_correlation_range(correlation_range_);
00093
00094 image_correlation_.initial_calculations();
00095
00096
00097
00098
00099
00100 num_raster_ = image_correlation_.get_image1_height();
00101
00102 typedef vsrl_raster_dp_setup* raster_ptr;
00103 raster_array_ = new raster_ptr[num_raster_];
00104
00105 for (int i=0;i<num_raster_;i++)
00106 raster_array_[i]=0;
00107 }
00108
00109 int brct_dense_reconstructor::get_disparity(int x, int y)
00110 {
00111 int new_x = get_assignment(x,y);
00112
00113 if (new_x >=0)
00114 return get_assignment(x,y)-x;
00115 else
00116 return 0-1000;
00117 }
00118
00119 int brct_dense_reconstructor::get_assignment(int x, int y)
00120 {
00121
00122
00123 if (y<0 || y >=num_raster_)
00124 return 0-1;
00125 else
00126 {
00127 if (!(raster_array_[y]))
00128
00129 this->evaluate_raster(y);
00130
00131 return raster_array_[y]->get_assignment(x);
00132 }
00133 }
00134
00135
00136 void brct_dense_reconstructor::evaluate_raster(const int i)
00137 {
00138 if (i<0 || i>= num_raster_)
00139 vcl_cout << "Warning tried to evaluate inapropriate raster\n";
00140
00141
00142
00143 vcl_cout << "evaluating raster " << i << vcl_endl;
00144
00145
00146 vsrl_raster_dp_setup *raster = new vsrl_raster_dp_setup(i, &image_correlation_);
00147
00148
00149
00150
00151 if (i>0)
00152 if (raster_array_[i-1])
00153 raster->set_prior_raster(raster_array_[i-1]);
00154
00155 if (i<num_raster_-1)
00156 if (raster_array_[i+1])
00157 raster->set_prior_raster(raster_array_[i+1]);
00158
00159
00160 raster->set_search_range(correlation_range_);
00161
00162
00163 raster->execute();
00164
00165
00166 raster_array_[i]=raster;
00167 }
00168
00169
00170 void brct_dense_reconstructor::write_disparity_image(char *filename)
00171 {
00172
00173
00174
00175
00176 vil1_byte_buffer buffer(image1_);
00177
00178 for (int x=0;x<buffer.width();x++)
00179 for (int y=0;y<buffer.height();y++)
00180 buffer(x,y)=0;
00181
00182
00183
00184 for (int y=0;y<buffer.height();y++)
00185 for (int x=0;x<buffer.width();x++)
00186 {
00187 int disparity = this->get_disparity(x,y);
00188 int value = disparity + correlation_range_+1;
00189 if (value < 0)
00190 value = 0;
00191 if (value>2*correlation_range_+1)
00192 value=0;
00193 buffer(x,y)=static_cast<unsigned char>(value);
00194 }
00195
00196
00197
00198 vil1_save(buffer, filename);
00199 }
00200
00201
00202
00203 void brct_dense_reconstructor::print_correlation_cost(const int x, const int y)
00204 {
00205 vcl_cout << "Correlation costs for pixel " << x << ' ' << y << vcl_endl;
00206
00207 for (int disp = 0-correlation_range_;disp < correlation_range_;disp++)
00208 vcl_cout << disp << " -> " << image_correlation_.get_correlation(x,y,disp) << vcl_endl;
00209 }
00210
00211
00212 vcl_vector<vsol_point_2d_sptr> brct_dense_reconstructor::points0(const int i,
00213 const int del)
00214 {
00215 vcl_vector<vsol_point_2d_sptr> points;
00216 int w = image_correlation_.get_image1_width();
00217 int h = image_correlation_.get_image1_height();
00218 if (i<0||i>=h)
00219 return points;
00220 for (int x = 0; x<w; x+=del)
00221 points.push_back(new vsol_point_2d(x, i));
00222 return points;
00223 }
00224
00225
00226 vcl_vector<vsol_point_2d_sptr> brct_dense_reconstructor::points1(const int i,
00227 const int del)
00228 {
00229 vcl_vector<vsol_point_2d_sptr> points;
00230 int w = image_correlation_.get_image1_width();
00231 int h = image_correlation_.get_image1_height();
00232 if (i<0||i>=h)
00233 return points;
00234 for (int x = 0; x<w; x+=del)
00235 {
00236 int xa = this->get_assignment(x, i);
00237 vcl_cout << '(' << x << ' ' << xa << ")\n";
00238 points.push_back(new vsol_point_2d(xa, i));
00239 }
00240 vcl_cout << vcl_flush;
00241 return points;
00242 }
00243
00244 void brct_dense_reconstructor::get_correlation(const int x0, const int y0,
00245 vcl_vector<int>& xpos,
00246 vcl_vector<double>& corr)
00247 {
00248 xpos.clear();
00249 corr.clear();
00250 int range = image_correlation_.get_correlation_range();
00251 int w = image_correlation_.get_image2_width();
00252 for (int x=x0-range; x<=x0+range; ++x)
00253 if (x>=0&&x<w)
00254 {
00255 xpos.push_back(x);
00256 corr.push_back(image_correlation_.get_correlation(x0, y0, x, y0));
00257 }
00258 }