00001 // This is mul/fhs/fhs_searcher.h 00002 #ifndef fhs_searcher_h_ 00003 #define fhs_searcher_h_ 00004 //: 00005 // \file 00006 // \author Tim Cootes 00007 // \brief Use F&H's DP style algorithm to search for global solutions 00008 00009 #include <fhs/fhs_arc.h> 00010 #include <vimt/vimt_image_2d_of.h> 00011 00012 //: Use F&H's DP style algorithm to search for global solutions to model match 00013 // Model consists of a set of features, together with a tree of neighbour 00014 // relationships of the form pos(j) = pos(i) + (N(mx,var_x),N(my,var_y)) 00015 // where N(m,var) is a gaussian with mean m and variance var. 00016 // 00017 // The aim is to find a set of points {p(i)} which minimise 00018 // sum_i F_i(p(i)) + sum_k shape_cost(arc(k)) 00019 // where k indexes the set of arcs defining neighbour relationships, 00020 // and shape_cost(arc) = dx*dx/arc.var_x + dy*dy.var_y (dx=p(arc_j).x()-p(arc_i).x()-arc.mx) 00021 // This is achieved using a combination of a quadratic distance function 00022 // applied to each feature response image F(i), and a dynamic programming 00023 // approach to combining the data. 00024 // 00025 // Algorithm based on papers by Felzenszwalb and Huttenlocher on 00026 // Pictoral Structure Matching. 00027 class fhs_searcher 00028 { 00029 private: 00030 //: Arcs defining neighbour relationships between features 00031 // Ordered so that parents precede children 00032 vcl_vector<fhs_arc> arc_; 00033 00034 //: arc_to_j_[j] gives index of arc ending at given j 00035 vcl_vector<unsigned> arc_to_j_; 00036 00037 //: children_[i] gives list of child nodes of node i in tree 00038 vcl_vector<vcl_vector<unsigned> > children_; 00039 00040 //: Workspace for accumulated sum of responses 00041 vcl_vector<vimt_image_2d_of<float> > sum_im_; 00042 00043 //: Workspace for sum of responses, transformed by distance function 00044 vcl_vector<vimt_image_2d_of<float> > dist_im_; 00045 00046 //: pos_[i](x,y,0),pos_[i](x,y,1) is position of best response for (x,y) 00047 // Result is in image co-ordinates. 00048 vcl_vector<vimt_image_2d_of<int> > pos_im_; 00049 00050 //: Combine responses for image im_index, given supplied feature_response for that node 00051 void combine_responses(unsigned im_index, 00052 const vimt_image_2d_of<float>& feature_response); 00053 00054 public: 00055 //: Default constructor 00056 fhs_searcher(); 00057 00058 //: Set tree defining relationships between features 00059 // Input arcs define neighbour relationships in any order. 00060 // root_node defines which feature to be used as the root 00061 void set_tree(const vcl_vector<fhs_arc>& arcs, unsigned root_node); 00062 00063 //: Index of root node (set by last call to set_tree() 00064 unsigned root_node() const; 00065 00066 //: Number of points represented 00067 unsigned n_points() const { return arc_.size()+1; } 00068 00069 //: Perform global search 00070 // Images of feature response supplied. The transformation 00071 // (world2im()) for each image can be used to indicate regions 00072 // which don't necessarily overlap. However, effective displacements 00073 // are assumed to be in pixel sized steps. 00074 // 00075 // After calling search(), results can be obtained using 00076 // points() and best_points() etc 00077 void search(const vcl_vector<vimt_image_2d_of<float> >& feature_response); 00078 00079 //: Compute optimal position of all points given position of root 00080 // Assumes search() has been called first 00081 void points_from_root(const vgl_point_2d<double>& root_pt, 00082 vcl_vector<vgl_point_2d<double> >& pts) const; 00083 00084 //: Compute optimal position of all points 00085 // Assumes search() has been called first 00086 // Returns cost at optimal position 00087 double best_points(vcl_vector<vgl_point_2d<double> >& pts) const; 00088 00089 //: Return final total cost image for root 00090 const vimt_image_2d_of<float>& root_cost_image() const; 00091 }; 00092 00093 #endif // fhs_searcher_h_
1.4.4