Go to the documentation of this file.00001 #include "mfpf_profile_pdf_builder.h"
00002
00003
00004
00005
00006
00007 #include <mfpf/mfpf_profile_pdf.h>
00008 #include <vsl/vsl_binary_loader.h>
00009 #include <vul/vul_string.h>
00010 #include <vgl/vgl_point_2d.h>
00011 #include <vgl/vgl_vector_2d.h>
00012 #include <vcl_cassert.h>
00013 #include <vcl_algorithm.h>
00014 #include <vcl_sstream.h>
00015
00016 #include <mbl/mbl_parse_block.h>
00017 #include <mbl/mbl_read_props.h>
00018 #include <mbl/mbl_data_array_wrapper.h>
00019
00020 #include <vimt/vimt_sample_profile_bilin.h>
00021 #include <vnl/io/vnl_io_vector.h>
00022 #include <vsl/vsl_vector_io.h>
00023
00024 #include <mfpf/mfpf_norm_vec.h>
00025
00026
00027
00028
00029
00030 mfpf_profile_pdf_builder::mfpf_profile_pdf_builder()
00031 {
00032 set_defaults();
00033 }
00034
00035
00036 void mfpf_profile_pdf_builder::set_defaults()
00037 {
00038 step_size_=1.0;
00039 ilo_=-4; ihi_=4;
00040 search_ni_=5;
00041 }
00042
00043
00044
00045
00046
00047 mfpf_profile_pdf_builder::~mfpf_profile_pdf_builder()
00048 {
00049 }
00050
00051
00052 mfpf_point_finder* mfpf_profile_pdf_builder::new_finder() const
00053 {
00054 return new mfpf_profile_pdf();
00055 }
00056
00057 void mfpf_profile_pdf_builder::set(int ilo, int ihi,
00058 const vpdfl_builder_base& builder)
00059 {
00060 ilo_=ilo;
00061 ihi_=ihi;
00062 pdf_builder_ = builder.clone();
00063 }
00064
00065
00066
00067
00068 void mfpf_profile_pdf_builder::set_region_size(double wi, double)
00069 {
00070 wi/=step_size();
00071 ihi_ = vcl_max(1,int(0.99+wi));
00072 ilo_ = -ihi_;
00073 }
00074
00075
00076
00077 void mfpf_profile_pdf_builder::clear(unsigned n_egs)
00078 {
00079 data_.resize(0);
00080 }
00081
00082
00083
00084 void mfpf_profile_pdf_builder::add_example(const vimt_image_2d_of<float>& image,
00085 const vgl_point_2d<double>& p,
00086 const vgl_vector_2d<double>& u)
00087 {
00088 int n=1+ihi_-ilo_;
00089 unsigned np=image.image().nplanes();
00090 vnl_vector<double> v(n*np);
00091 vgl_vector_2d<double> u1=step_size_*u;
00092 const vgl_point_2d<double> p0 = p+ilo_*u1;
00093 vimt_sample_profile_bilin(v,image,p0,u1,n);
00094 mfpf_norm_vec(v);
00095 data_.push_back(v);
00096 }
00097
00098
00099 void mfpf_profile_pdf_builder::build(mfpf_point_finder& pf)
00100 {
00101 assert(pf.is_a()=="mfpf_profile_pdf");
00102 mfpf_profile_pdf& nc = static_cast<mfpf_profile_pdf&>(pf);
00103 nc.set_search_area(search_ni_,0);
00104
00105 vcl_cout<<"Building from "<<data_.size()<<" examples."<<vcl_endl;
00106
00107 vpdfl_pdf_base *pdf = pdf_builder().new_model();
00108 mbl_data_array_wrapper<vnl_vector<double> > data(&data_[0],data_.size());
00109
00110 pdf_builder().build(*pdf,data);
00111
00112 nc.set(ilo_,ihi_,*pdf);
00113 set_base_parameters(nc);
00114
00115
00116 delete pdf;
00117 data_.resize(0);
00118 }
00119
00120
00121
00122
00123
00124
00125 bool mfpf_profile_pdf_builder::set_from_stream(vcl_istream &is)
00126 {
00127
00128 vcl_string s = mbl_parse_block(is);
00129 vcl_istringstream ss(s);
00130 mbl_read_props_type props = mbl_read_props_ws(ss);
00131
00132 set_defaults();
00133
00134
00135 parse_base_props(props);
00136
00137 if (props.find("ilo")!=props.end())
00138 {
00139 ilo_=vul_string_atoi(props["ilo"]);
00140 props.erase("ilo");
00141 }
00142 if (props.find("ihi")!=props.end())
00143 {
00144 ihi_=vul_string_atoi(props["ihi"]);
00145 props.erase("ihi");
00146 }
00147
00148 if (props.find("pdf_builder")!=props.end())
00149 {
00150 vcl_istringstream b_ss(props["pdf_builder"]);
00151 vcl_auto_ptr<vpdfl_builder_base> bb =
00152 vpdfl_builder_base::new_pdf_builder_from_stream(b_ss);
00153 pdf_builder_ = bb->clone();
00154 props.erase("pdf_builder");
00155 }
00156
00157
00158 mbl_read_props_look_for_unused_props(
00159 "mfpf_profile_pdf_builder::set_from_stream", props, mbl_read_props_type());
00160 return true;
00161 }
00162
00163
00164
00165
00166
00167 vcl_string mfpf_profile_pdf_builder::is_a() const
00168 {
00169 return vcl_string("mfpf_profile_pdf_builder");
00170 }
00171
00172
00173 mfpf_point_finder_builder* mfpf_profile_pdf_builder::clone() const
00174 {
00175 return new mfpf_profile_pdf_builder(*this);
00176 }
00177
00178
00179
00180
00181
00182 void mfpf_profile_pdf_builder::print_summary(vcl_ostream& os) const
00183 {
00184 os << "{ size: [" << ilo_ << ',' << ihi_ << ']' <<'\n';
00185 mfpf_point_finder_builder::print_summary(os);
00186 os << " pdf_builder: " << pdf_builder_
00187 << " }";
00188 }
00189
00190
00191 short mfpf_profile_pdf_builder::version_no() const
00192 {
00193 return 1;
00194 }
00195
00196 void mfpf_profile_pdf_builder::b_write(vsl_b_ostream& bfs) const
00197 {
00198 vsl_b_write(bfs,version_no());
00199 mfpf_point_finder_builder::b_write(bfs);
00200 vsl_b_write(bfs,ilo_);
00201 vsl_b_write(bfs,ihi_);
00202 vsl_b_write(bfs,pdf_builder_);
00203 vsl_b_write(bfs,data_);
00204 }
00205
00206
00207
00208
00209
00210 void mfpf_profile_pdf_builder::b_read(vsl_b_istream& bfs)
00211 {
00212 if (!bfs) return;
00213 short version;
00214 vsl_b_read(bfs,version);
00215 switch (version)
00216 {
00217 case (1):
00218 mfpf_point_finder_builder::b_read(bfs);
00219 vsl_b_read(bfs,ilo_);
00220 vsl_b_read(bfs,ihi_);
00221 vsl_b_read(bfs,pdf_builder_);
00222 vsl_b_read(bfs,data_);
00223 break;
00224 default:
00225 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&)\n"
00226 << " Unknown version number "<< version << vcl_endl;
00227 bfs.is().clear(vcl_ios::badbit);
00228 return;
00229 }
00230 }
00231