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 #include "object_segmentation_gui/utils.h"
00031 #include <math.h>
00032 
00033 namespace object_segmentation_gui {
00034   
00035   void transformRgb(float val, float &r, float &g, float &b){
00036     union{int intp; float floatp; } a;
00037     a.floatp = val;
00038     int rgb = *reinterpret_cast<int*>(&a.intp);
00039   
00040     r = ((rgb >> 16) & 0xff) / 255.0f;
00041     g = ((rgb >> 8) & 0xff) / 255.0f;
00042     b = (rgb & 0xff) / 255.0f; 
00043   }
00044 
00045   float getRGB( float r, float g, float b){
00046     union{ int intp; float floatp; } a;
00047     int res = (int(r*255) << 16) | (int(g*255) << 8) | int(b*255);
00048     a.intp=res;
00049     float rgb = *(&a.floatp);
00050     return rgb;
00051   }
00052 
00053   template<class T>
00054   void swap( T &x, T &y) {
00055     T tmp=x;
00056     x=y;
00057     y=tmp;
00058   }
00059 
00060   template void swap( float &x, float &y);
00061   template  void swap( int &x, int &y);
00062   
00063   template<class T>
00064   T square( T x_c) {
00065     
00066     return x_c * x_c;
00067   }
00068 
00069   template float square( float x_c);
00070   template int  square( int x_c);
00071   
00072   template<class T>
00073   T dist( T x_c, T y_c, T x, T y) {
00074     
00075     T dist  = sqrt( square(x_c-x) + square(y_c-y));
00076     return dist;
00077   }
00078 
00079   template float dist( float x_c, float y_c, float x, float y);
00080   template int dist( int x_c, int y_c, int x, int y);
00081  
00082   void RGBToHSV(float r, float g, float b,
00083                 float &h, float &s, float &v) 
00084   {  
00085   
00086     float maxC = b;
00087     if (maxC < g) maxC = g;
00088     if (maxC < r) maxC = r;
00089     float minC = b;
00090     if (minC > g) minC = g;
00091     if (minC > r) minC = r;
00092     
00093     float delta = maxC - minC;
00094     
00095     v = maxC;
00096     s = 0;
00097     h = 0;
00098     
00099     if (delta == 0) {
00100       return;
00101     } else {
00102       s = delta / maxC;
00103       float dR = 60*(maxC - r)/delta + 180;
00104       float dG = 60*(maxC - g)/delta + 180;
00105       float dB = 60*(maxC - b)/delta + 180;
00106       if (r == maxC)
00107         h = dB - dG;
00108       else if (g == maxC)
00109         h = 120 + dR - dB;
00110       else
00111         h = 240 + dG - dR;
00112     }
00113     
00114     if (h<0)
00115       h+=360;
00116     if (h>=360)
00117       h-=360;
00118   }
00119 
00120   void HSVToRGB(float h, float s, float v,
00121                 float &r, float &g, float &b) 
00122   {  
00123     if(s==0){
00124       r = v;
00125       g = v;
00126       b = v;
00127       return;
00128     }
00129     
00130     
00131     float h_tmp = h/60.0f;
00132     int i = floor(h_tmp);
00133     float f = h_tmp - i;
00134     
00135     float p = v * (1-s);
00136     float q = v * (1-s*f);
00137     float t = v * (1-s * (1-f));
00138     
00139 
00140     switch(i){
00141     case 0:
00142       
00143       r = v;
00144       g = t;
00145       b = p;
00146       break;
00147     case 1:
00148       
00149       r = q;
00150       g = v;
00151       b = p;
00152       break;
00153     case 2:
00154       
00155       r = p;
00156       g = v;
00157       b = t;
00158       break;
00159     case 3:
00160       
00161       r = p;
00162       g = q;
00163       b = v;
00164       break;
00165     case 4:
00166       
00167       r = t;
00168       g = p;
00169       b = v;
00170       break;
00171     case 5:
00172       
00173       r = v;
00174       g = p;
00175       b = q;
00176       break;
00177     }
00178   }
00179 }