00001 /* --------------------------------------------------------------------------- 00002 Phission : 00003 Realtime Vision Processing System 00004 00005 Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu) 00006 University of Massachusetts at Lowell, 00007 Laboratory for Artificial Intelligence and Robotics 00008 00009 This file is part of Phission. 00010 00011 ---------------------------------------------------------------------------*/ 00012 #ifdef HAVE_CONFIG_H 00013 #include <phissionconfig.h> 00014 #endif 00015 00016 #include <hsvsegment_Filter.h> 00017 #include <phbase.h> 00018 00019 /* ---------------------------------------------------------------------- */ 00020 hsvsegment_Filter::hsvsegment_Filter(int h, int s, int v, 00021 int lh, int ls, int lv, 00022 int uh, int us, int uv) : 00023 phFilter("hsvsegment_Filter") 00024 00025 { 00026 this->m_format = phImageHSV24; 00027 set(h,s,v,lh,ls,lv,uh,us,uv); 00028 } 00029 00030 /* ---------------------------------------------------------------------- */ 00031 hsvsegment_Filter::~hsvsegment_Filter() 00032 { 00033 } 00034 00035 /* ------------------------------------------------------------------------ */ 00036 phFilter *hsvsegment_Filter::cloneFilter() 00037 { 00038 return (phFilter *)new hsvsegment_Filter(this->m_h,this->m_s,this->m_v, 00039 this->m_lh,this->m_ls,this->m_lv, 00040 this->m_uh,this->m_us,this->m_uv ); 00041 } 00042 00043 /* ---------------------------------------------------------------------- */ 00044 int hsvsegment_Filter::set(int h, int s, int v, 00045 int lh, int ls, int lv, 00046 int uh, int us, int uv) 00047 { 00048 00049 this->m_h = h; 00050 this->m_s = s; 00051 this->m_v = v; 00052 this->m_lh = lh; 00053 this->m_ls = ls; 00054 this->m_lv = lv; 00055 this->m_uh = uh; 00056 this->m_us = us; 00057 this->m_uv = uv; 00058 00059 return phSUCCESS; 00060 } 00061 00062 /* ---------------------------------------------------------------------- */ 00063 int hsvsegment_Filter::filter() 00064 { 00065 phFUNCTION("hsvsegment_Filter::filter") 00066 00067 unsigned int i = 0; 00068 00069 int32_t x = 0; 00070 int32_t y = 0; 00071 int32_t h_step = 16; 00072 int32_t s_step = 40; 00073 int32_t v_step = 30; 00074 00075 uint8_t hi = 0; 00076 uint8_t si = 0; 00077 uint8_t vi = 0; 00078 uint8_t h = 0; 00079 uint8_t s = 0; 00080 uint8_t v = 0; 00081 uint8_t mid_s = (this->m_ls + this->m_us) / 2; 00082 uint8_t mid_v = (this->m_lv + this->m_uv) / 2; 00083 00084 uint8_t *imgptr = Image; 00085 uint32_t limit = width * height; 00086 00087 phImageFormatIndecies(this->format,&hi,&si,&vi,NULL); 00088 00089 /* Begin filter */ 00090 00091 if ((!(this->format & phImageHSV24)) || (imgptr == NULL)) 00092 return phSUCCESS; 00093 00094 for (i = 0; i < limit; i++, imgptr += depth) 00095 { 00096 h = imgptr[hi]; 00097 s = imgptr[si]; 00098 v = imgptr[vi]; 00099 00100 if (this->m_s) 00101 { 00102 if (s < this->m_ls) h = v = s = 0; 00103 else if (s > this->m_us) h = v = s = 0; 00104 } 00105 if (this->m_v) 00106 { 00107 if (v < this->m_lv) h = s = v = 0; 00108 else if (v > this->m_uv) h = s = v = 0; 00109 } 00110 00111 x = 0; 00112 if ((this->m_h) && (s > 0) && (v > 0)) 00113 { 00114 while (x < 275) 00115 { 00116 if ((h >= (x - (h_step / 2))) && (h <= (x + (h_step / 2)))) 00117 { 00118 h = x; 00119 y = 0; 00120 while ((y - (s_step / 2)) < 255) 00121 { 00122 if ((s >= (y-(s_step / 2))) && (s <= (y+(s_step / 2)))) 00123 s = y; 00124 y += s_step; 00125 } 00126 y = 0; 00127 while ((y - (v_step / 2)) < 255) 00128 { 00129 if ((v >= (y-(v_step / 2))) && (v <= (y+(v_step / 2)))) 00130 v = y; 00131 y += v_step; 00132 } 00133 } 00134 x += 20; 00135 } 00136 #if 0 00137 if (((h >= 225) && (h < 254)) && (1)) 00138 { 00139 h = 235; 00140 s = 255; 00141 v = 255; 00142 } 00143 else if (((h >= 1) && (h <= 16)) && (1)) 00144 { 00145 h = 15; 00146 s = 255; 00147 v = 255; 00148 } 00149 else if (((h >= 17) && (h <= 29)) && (1)) 00150 { 00151 h = 29; 00152 s = 255; 00153 v = 255; 00154 } 00155 else if (((h >= 30) && (h <= 44)) && (1)) 00156 { 00157 h = 43; 00158 s = 255; 00159 v = 255; 00160 } 00161 else if (((h >= 45) && (h <= 60)) && (1)) 00162 { 00163 h = 60; 00164 s = 255; 00165 v = 255; 00166 } 00167 else if (((h >= 61) && (h <= 110)) && (1)) 00168 { 00169 h = 80; 00170 s = 255; 00171 v = 255; 00172 } 00173 else if (((h >= 101) && (h <= 120)) && (1)) 00174 { 00175 h = 110; 00176 s = 255; 00177 v = 255; 00178 } 00179 else if (((h >= 141) && (h <= 160)) && (1)) 00180 { 00181 h = 160; 00182 s = 255; 00183 v = 255; 00184 } 00185 else h = s = v = 0; 00186 #endif 00187 #if 0 00188 if ((h >= 20) && (h < 80)) h = 50; 00189 if ((h >= 80) && (h < 140)) h = 110; 00190 if ((h >= 140) && (h < 200))h = 170; 00191 if ((h >= 200) && (h < 260))h = 230; 00192 #endif 00193 #if 0 00194 int nearest = 60; 00195 int half = nearest / 2; 00196 int x = h % nearest; 00197 00198 if (x > half) h += (nearest - x); 00199 else if (x < half) h -= x; 00200 #endif 00201 } 00202 imgptr[hi] = h; 00203 imgptr[si] = s; 00204 imgptr[vi] = v; 00205 } 00206 00207 /* End Filter */ 00208 00209 return phSUCCESS; 00210 } 00211 00212
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |