$search
00001 /* 00002 * Copyright (c) 2009, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include "bosch_object_segmentation_gui/utils.h" 00031 #include <math.h> 00032 00033 namespace bosch_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 //if(i%2) f = 1 - f; 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 // 0: v, n, m); 00143 r = v; 00144 g = t; 00145 b = p; 00146 break; 00147 case 1: 00148 // 1: n, v, m); 00149 r = q; 00150 g = v; 00151 b = p; 00152 break; 00153 case 2: 00154 // 2: (m, v, n) 00155 r = p; 00156 g = v; 00157 b = t; 00158 break; 00159 case 3: 00160 // 3: (m, n, v); 00161 r = p; 00162 g = q; 00163 b = v; 00164 break; 00165 case 4: 00166 // 4: (n, m, v) 00167 r = t; 00168 g = p; 00169 b = v; 00170 break; 00171 case 5: 00172 // 5: (v, m, n); 00173 r = v; 00174 g = p; 00175 b = q; 00176 break; 00177 } 00178 } 00179 }