Go to the documentation of this file.00001 #ifndef sdet_texture_classifier_h_
00002 #define sdet_texture_classifier_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <sdet/sdet_texture_classifier_params.h>
00051 #include <brip/brip_filter_bank.h>
00052 #include <vil/vil_image_resource.h>
00053 #include <vil/vil_image_view.h>
00054 #include <vgl/vgl_polygon.h>
00055 #include <vnl/vnl_vector.h>
00056 #include <vnl/vnl_vector_fixed.h>
00057 #include <vbl/vbl_ref_count.h>
00058 #include <vcl_vector.h>
00059 #include <vcl_map.h>
00060 #include <vcl_iosfwd.h>
00061
00062 struct sdet_neighbor
00063 {
00064 sdet_neighbor(vcl_string const& category, vnl_vector<double> const& k_mean)
00065 : cat_(category), k_mean_(k_mean){}
00066 vcl_string cat_;
00067 vnl_vector<double> k_mean_;
00068 };
00069
00070 class sdet_neighbor_less
00071 {
00072 public:
00073 sdet_neighbor_less(vnl_vector<double> const& query): query_(query){}
00074
00075 bool operator()(sdet_neighbor const& na, sdet_neighbor const& nb) const
00076 {
00077 double da = vnl_vector_ssd(na.k_mean_, query_),
00078 db = vnl_vector_ssd(nb.k_mean_, query_);
00079 return da < db;
00080 }
00081 private:
00082 vnl_vector<double> query_;
00083 };
00084
00085 class sdet_texture_classifier : public sdet_texture_classifier_params,
00086 public vbl_ref_count
00087 {
00088 public:
00089
00090 sdet_texture_classifier(sdet_texture_classifier_params const& params);
00091
00092
00093 bool compute_filter_bank(vil_image_view<float> const& img);
00094
00095
00096 unsigned max_filter_radius() const;
00097
00098
00099 brip_filter_bank& filter_responses()
00100 {return filter_responses_;}
00101
00102
00103
00104 bool compute_training_data(vcl_string const& category);
00105
00106 bool compute_training_data(vcl_string const& category,
00107 vgl_polygon<double> const& texture_region);
00108
00109 bool compute_training_data(vcl_string const& category,
00110 vcl_vector<vgl_polygon<double> >const& texture_regions);
00111
00112 bool compute_training_data(vcl_string const& category,
00113 vcl_string const& poly_path = "");
00114
00115
00116 bool compute_textons(vcl_string const& category);
00117
00118
00119
00120
00121
00122 bool compute_textons(vcl_vector<vcl_string> const& image_paths,
00123 vcl_string const& category,
00124 vcl_vector<vcl_string> const& poly_paths=
00125 vcl_vector<vcl_string>());
00126
00127
00128 void compute_category_histograms();
00129
00130
00131 bool save_dictionary(vcl_string const& path) const;
00132
00133 bool load_dictionary(vcl_string const& path);
00134
00135
00136 void set_category_colors(vcl_map< vcl_string, vnl_vector_fixed<float, 3> > const& color_map)
00137 {color_map_ = color_map; color_map_valid_ = true;}
00138
00139 vil_image_view<float> classify_image_blocks(vcl_string const& img_path);
00140
00141
00142 void print_dictionary() const;
00143 void print_distances() const;
00144 void print_color_map() const;
00145 void print_category_histograms() const;
00146 void print_interclass_probs() const;
00147 void print_texton_weights() const;
00148
00149
00150
00151 protected:
00152 sdet_texture_classifier();
00153 vil_image_view<float> scale_image(vil_image_resource_sptr const& resc);
00154 vcl_vector<vnl_vector<double> >
00155 random_centers(vcl_vector<vnl_vector<double> > const& training_data,
00156 unsigned k) const;
00157 void compute_distances();
00158 void compute_interclass_probs();
00159 void compute_texton_weights();
00160
00161 void init_color_map();
00162
00163 void compute_texton_index();
00164
00165 unsigned nearest_texton_index(vnl_vector<double> const& query);
00166
00167
00168 void update_hist(vnl_vector<double> const& f, float weight,
00169 vcl_vector<float>& hist);
00170
00171 vcl_map<vcl_string, float> texture_probabilities(vcl_vector<float> const& hist);
00172
00173 void category_color_mix(vcl_map<vcl_string, float>& probs,
00174 vnl_vector_fixed<float, 3>& color_mix);
00175
00176 void category_quality_color_mix(vcl_map<vcl_string, float>& probs,
00177 vnl_vector_fixed<float, 3>& color_mix);
00178
00179 brip_filter_bank filter_responses_;
00180 vil_image_view<float> laplace_;
00181 vil_image_view<float> gauss_;
00182 vil_image_view<float> frac_counts_;
00183
00184 vcl_map< vcl_string, vcl_vector<vnl_vector<double> > > training_data_;
00185
00186 vcl_map< vcl_string, vcl_vector<vnl_vector<double> > > texton_dictionary_;
00187
00188 vcl_map< vcl_string, vcl_map< vcl_string, double> > dist_;
00189 bool distances_valid_;
00190
00191 vcl_map< vcl_string, vcl_map< vcl_string, double> > inter_prob_;
00192 bool inter_prob_valid_;
00193
00194 vcl_map< vcl_string, vnl_vector_fixed<float, 3> > color_map_;
00195 bool color_map_valid_;
00196 vcl_vector<sdet_neighbor> texton_index_;
00197 bool texton_index_valid_;
00198 vcl_map<vcl_string, vcl_vector<float> > category_histograms_;
00199 vcl_vector<float> texton_weights_;
00200 bool texton_weights_valid_;
00201 };
00202 #include <sdet/sdet_texture_classifier_sptr.h>
00203
00204 void vsl_b_write(vsl_b_ostream & os, sdet_texture_classifier const &tc);
00205
00206
00207 void vsl_b_read(vsl_b_istream & is, sdet_texture_classifier &tc);
00208
00209 void vsl_print_summary(vcl_ostream &os, const sdet_texture_classifier &tc);
00210
00211 void vsl_b_read(vsl_b_istream& is, sdet_texture_classifier* tc);
00212
00213 void vsl_b_write(vsl_b_ostream& os, const sdet_texture_classifier* &tc);
00214
00215 void vsl_print_summary(vcl_ostream& os, const sdet_texture_classifier* &tc);
00216
00217 void vsl_b_read(vsl_b_istream& is, sdet_texture_classifier_sptr& tc);
00218
00219 void vsl_b_write(vsl_b_ostream& os, const sdet_texture_classifier_sptr &tc);
00220
00221 void vsl_print_summary(vcl_ostream& os, const sdet_texture_classifier_sptr &tc);
00222 #endif // sdet_texture_classifier_h_