Go to the documentation of this file.00001 
00002 
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 #include <pcl/recognition/dotmod.h>
00039 
00040 #include <fstream>
00041 
00042 
00044 pcl::DOTMOD::
00045 DOTMOD(size_t template_width,
00046        size_t template_height) :
00047   template_width_ (template_width),
00048   template_height_ (template_height),
00049   templates_ ()
00050 {
00051 }
00052 
00054 pcl::DOTMOD::
00055 ~DOTMOD()
00056 {
00057 }
00058 
00060 size_t 
00061 pcl::DOTMOD::
00062 createAndAddTemplate (const std::vector<pcl::DOTModality*> & modalities,
00063                       const std::vector<pcl::MaskMap*> & masks,
00064                       size_t template_anker_x,
00065                       size_t template_anker_y,
00066                       const pcl::RegionXY & region)
00067 {
00068   DenseQuantizedMultiModTemplate dotmod_template;
00069 
00070   RegionXY actual_template_region;
00071   actual_template_region.x = static_cast<int> (template_anker_x);
00072   actual_template_region.y = static_cast<int> (template_anker_y);
00073   actual_template_region.width = static_cast<int> (template_width_);
00074   actual_template_region.height = static_cast<int> (template_height_);
00075 
00076   
00077   const size_t nr_modalities = modalities.size();
00078   dotmod_template.modalities.resize (nr_modalities);
00079   for (size_t modality_index = 0; modality_index < nr_modalities; ++modality_index)
00080   {
00081     const MaskMap & mask = *(masks[modality_index]);
00082     const QuantizedMap & data = modalities[modality_index]->computeInvariantQuantizedMap (mask, actual_template_region);
00083 
00084     const size_t width = data.getWidth ();
00085     const size_t height = data.getHeight ();
00086 
00087     dotmod_template.modalities[modality_index].features.resize (width*height);
00088 
00089     for (size_t row_index = 0; row_index < height; ++row_index)
00090     {
00091       for (size_t col_index = 0; col_index < width; ++col_index)
00092       {
00093         dotmod_template.modalities[modality_index].features[row_index*width + col_index] = data (col_index, row_index);
00094       }
00095     }
00096   }
00097 
00098   
00099   dotmod_template.region.x = region.x - static_cast<int> (template_anker_x);
00100   dotmod_template.region.y = region.y - static_cast<int> (template_anker_y);
00101   dotmod_template.region.width = region.width;
00102   dotmod_template.region.height = region.height;
00103 
00104   
00105   templates_.push_back(dotmod_template);
00106 
00107   return templates_.size () - 1;
00108 }
00109 
00111 void
00112 pcl::DOTMOD::
00113 detectTemplates (const std::vector<DOTModality*> & modalities, 
00114                  const float template_response_threshold,
00115                  std::vector<DOTMODDetection> & detections,
00116                  const size_t bin_size ) const
00117 {
00118   
00119 
00120   std::vector<QuantizedMap> maps;
00121   const size_t nr_modalities = modalities.size ();
00122 
00123   
00124 
00125   for (size_t modality_index = 0; modality_index < nr_modalities; ++modality_index)
00126   {
00127     QuantizedMap &map = modalities[modality_index]->getDominantQuantizedMap ();
00128     maps.push_back(map);
00129   }
00130 
00131   
00132   
00133   
00134   const size_t width = maps[0].getWidth ();
00135   const size_t height = maps[0].getHeight ();
00136   const size_t nr_templates = templates_.size ();
00137 
00138   const size_t nr_template_horizontal_bins = template_width_ / bin_size;
00139   const size_t nr_template_vertical_bins = template_height_ / bin_size;
00140 
00141   
00142   
00143   
00144   
00145   
00146   
00147   
00148   
00149 
00150   
00151 
00152   float best_response = 0.0f;
00153   for (size_t row_index = 0; row_index < (height - nr_template_vertical_bins); ++row_index)
00154   {
00155     for (size_t col_index = 0; col_index < (width - nr_template_horizontal_bins); ++col_index)
00156     {
00157       std::vector<float> responses (nr_templates, 0.0f);
00158 
00159       for (size_t modality_index = 0; modality_index < nr_modalities; ++modality_index)
00160       {
00161         const QuantizedMap map = maps[modality_index].getSubMap (col_index, row_index, nr_template_horizontal_bins, nr_template_vertical_bins);
00162 
00163         const unsigned char * image_data = map.getData ();
00164         for (size_t template_index = 0; template_index < nr_templates; ++template_index)
00165         {
00166           const unsigned char * template_data = &(templates_[template_index].modalities[modality_index].features[0]);
00167           for (size_t data_index = 0; data_index < (nr_template_horizontal_bins*nr_template_vertical_bins); ++data_index)
00168           {
00169             if ((image_data[data_index] & template_data[data_index]) != 0)
00170               responses[template_index] += 1.0f;
00171           }
00172         }
00173       }
00174 
00175       
00176       const float scaling_factor = 1.0f / float (nr_template_horizontal_bins * nr_template_vertical_bins);
00177       for (size_t template_index = 0; template_index < nr_templates; ++template_index)
00178       {
00179         const float response = responses[template_index] * scaling_factor;
00180 
00181         if (response > template_response_threshold)
00182         {
00183           DOTMODDetection detection;
00184           detection.score = response;
00185           detection.template_id = template_index;
00186           detection.bin_x = col_index;
00187           detection.bin_y = row_index;
00188 
00189           detections.push_back (detection);
00190         }
00191 
00192         if (response > best_response)
00193           best_response = response;
00194       }
00195     }
00196   }
00197 
00198   
00199   
00200   
00201 }
00202 
00204 void
00205 pcl::DOTMOD::
00206 saveTemplates (const char * file_name) const
00207 {
00208   std::ofstream file_stream;
00209   file_stream.open (file_name, std::ofstream::out | std::ofstream::binary);
00210 
00211   serialize (file_stream);
00212 
00213   file_stream.close ();
00214 }
00215 
00217 void
00218 pcl::DOTMOD::
00219 loadTemplates (const char * file_name)
00220 {
00221   std::ifstream file_stream;
00222   file_stream.open (file_name, std::ofstream::in | std::ofstream::binary);
00223 
00224   deserialize (file_stream);
00225 
00226   file_stream.close ();
00227 }
00228 
00230 void
00231 pcl::DOTMOD::
00232 serialize (std::ostream & stream) const
00233 {
00234   const int nr_templates = static_cast<int> (templates_.size ());
00235   write (stream, nr_templates);
00236   for (int template_index = 0; template_index < nr_templates; ++template_index)
00237     templates_[template_index].serialize (stream);
00238 }
00239 
00241 void
00242 pcl::DOTMOD::
00243 deserialize (std::istream & stream)
00244 {
00245   templates_.clear ();
00246 
00247   int nr_templates;
00248   read (stream, nr_templates);
00249   templates_.resize (nr_templates);
00250   for (int template_index = 0; template_index < nr_templates; ++template_index)
00251     templates_[template_index].deserialize (stream);
00252 }