dotmod.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved. 
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
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   // get template data from available modalities
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   // set region relative to the center
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   // add template to template storage
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   //std::cerr << ">> detectTemplates (...)" << std::endl;
00119 
00120   std::vector<QuantizedMap> maps;
00121   const size_t nr_modalities = modalities.size ();
00122 
00123   //std::cerr << "nr_modalities: " << nr_modalities << std::endl;
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   //std::cerr << "1" << std::endl;
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   //std::cerr << "---------------------------------------------------" << std::endl;
00142   //std::cerr << "width:                       " << width << std::endl;
00143   //std::cerr << "height:                      " << height << std::endl;
00144   //std::cerr << "nr_templates:                " << nr_templates << std::endl;
00145   //std::cerr << "nr_template_horizontal_bins: " << nr_template_horizontal_bins << std::endl;
00146   //std::cerr << "nr_template_vertical_bins:   " << nr_template_vertical_bins << std::endl;
00147   //std::cerr << "template_width_:             " << template_width_ << std::endl;
00148   //std::cerr << "template_height_:            " << template_height_ << std::endl;
00149 
00150   //std::cerr << "2" << std::endl;
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       // find templates with response over threshold
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   //std::cerr << "best_response: " << best_response << std::endl;
00199   
00200   //std::cerr << "<< detectTemplates (...)" << std::endl;
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 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:25