dominant_color.cpp
Go to the documentation of this file.
00001 
00060 #include <cob_3d_mapping_common/dominant_color.h>
00061 
00062 namespace cob_3d_mapping
00063 {
00064   void
00065   DominantColor::addColor (uint8_t r, uint8_t g, uint8_t b, int weight)
00066   {
00067     /*if(id_==0) std::cout << "rgb_in" << (int)r << "," << (int)g << "," << (int)b << "\tweight:" << weight << std::endl;
00068      int h,s,v;
00069      rgb2hsv(r,g,b,h,s,v);
00070      if(id_==0) std::cout << "hsv_in" << (int)h << "," << (int)s << "," << (int)v << std::endl;*/
00071     for (int i = 0; i < weight; i++)
00072     {
00073       /*int pos = incrBin(h);
00074        sat_values_[pos] += s;
00075        sum_sat_ += s;
00076        sum_val_ += v;
00077        ++sum_colors_;*/
00078       sum_r_ += r;
00079       sum_g_ += g;
00080       sum_b_ += b;
00081       ++sum_colors_;
00082     }
00083   }
00084 
00085   void
00086   DominantColor::getColor (uint8_t& r, uint8_t& g, uint8_t& b) const
00087   {
00088     if (!sum_colors_)
00089     {
00090       r = 0;
00091       g = 0;
00092       b = 0;
00093       return;
00094     }
00095     /*int pos = getMaxBin();
00096      if(id_==0) std::cout << "hsv_out" << round(pos*bin_size+bin_center) << "," << sat_values_[pos]/hue_histogram_[pos] << "," << sum_val_/sum_colors_ << std::endl;
00097      hsv2rgb( round(pos*bin_size+bin_center), sat_values_[pos]/hue_histogram_[pos], sum_val_/sum_colors_, r,g,b );
00098      if(id_==0) std::cout << "rgb_out" << (int)r << "," << (int)g << "," << (int)b << std::endl;*/
00099     r = sum_r_ / sum_colors_;
00100     g = sum_g_ / sum_colors_;
00101     b = sum_b_ / sum_colors_;
00102   }
00103 
00104   int
00105   DominantColor::incrBin (int h)
00106   {
00107     int bin = h * inv_bin_size;
00108     hue_histogram_[bin] += 1;
00109     return bin;
00110   }
00111 
00112   int
00113   DominantColor::getMaxBin () const
00114   {
00115     int max_bin = 0, max_size = 0;
00116     for (int i = 0; i < HIST_SIZE; ++i)
00117     {
00118       if (hue_histogram_[i] >= max_size)
00119       {
00120         max_size = hue_histogram_[i];
00121         max_bin = i;
00122       }
00123     }
00124     //std::cout<<"max_(bin/size): "<<max_bin<<"/"<<max_size<<std::endl;
00125     return max_bin;
00126   }
00127 
00128   void
00129   DominantColor::rgb2hsv (uint8_t r, uint8_t g, uint8_t b, int& h, int& s, int& v) const
00130   {
00131     int rgb_min = min3 (r, g, b);
00132     int rgb_max = max3 (r, g, b);
00133     int delta = rgb_max - rgb_min;
00134     v = rgb_max;
00135     if (v == 0)
00136     {
00137       s = h = 0;
00138       return;
00139     }
00140     s = round (float (delta) / float (v) * 100.0f);
00141     v /= 2.55f;
00142     float h_tmp;
00143     if (s == 0)
00144     {
00145       h = 0;
00146       return;
00147     }
00148     if ((int)r == rgb_max)
00149       h_tmp = (float (g - b)) / (float (delta));
00150     else if ((int)g == rgb_max)
00151       h_tmp = 2.0f + (float (b - r)) / (float (delta));
00152     else
00153       h_tmp = 4.0f + (float (r - g)) / (float (delta));
00154 
00155     h = h_tmp * 60.0f;
00156     if (h < 0)
00157       h += 360;
00158   }
00159 
00160   void
00161   DominantColor::hsv2rgb (int h, int s, int v, uint8_t& r, uint8_t& g, uint8_t& b) const
00162   {
00163     if (s == 0)
00164     {
00165       r = g = b = v * 2.55f;
00166       return;
00167     }
00168 
00169     float hh = h / 60.0f; // sector 0..5
00170     int i = floor (hh);
00171     float f = hh - i;
00172     v = round (v * 2.55f);
00173     int p = v * (100 - s) * 0.01f;
00174     int q = v * (100 - s * f) * 0.01f;
00175     int t = v * (100 - s * (1.0f - f)) * 0.01f;
00176 
00177     switch (i)
00178     {
00179       case 0:
00180       {
00181         r = v;
00182         g = t;
00183         b = p;
00184         break;
00185       }
00186       case 1:
00187       {
00188         r = q;
00189         g = v;
00190         b = p;
00191         break;
00192       }
00193       case 2:
00194       {
00195         r = p;
00196         g = v;
00197         b = t;
00198         break;
00199       }
00200       case 3:
00201       {
00202         r = p;
00203         g = q;
00204         b = v;
00205         break;
00206       }
00207       case 4:
00208       {
00209         r = t;
00210         g = p;
00211         b = v;
00212         break;
00213       }
00214       default: // case 5:
00215       {
00216         r = v;
00217         g = p;
00218         b = q;
00219         break;
00220       }
00221     }
00222   }
00223 
00224   //inline void setID(unsigned int id) {id_=id;}
00225 
00226   void
00227   DominantColor::reset ()
00228   {
00229     sum_colors_ = 0; //sum_sat_ = sum_val_ = 0;
00230     sum_r_ = sum_b_ = sum_g_ = 0;
00231     hue_histogram_.clear ();
00232     hue_histogram_.resize (HIST_SIZE, 0);
00233     //sat_values_.clear();
00234     //sat_values_.resize(HIST_SIZE,0);
00235   }
00236 }


cob_3d_mapping_common
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:02:19