Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

hsvthreshold_Filter.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 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     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #include <hsvthreshold_Filter.h>
00033 
00034 #include <phError.h>
00035 #include <phMemory.h>
00036 #include <phPrint.h>
00037 
00038 /* ---------------------------------------------------------------------- */
00039 /* h,s,v : active threshold arguments
00040  * l* : lower threshold ; u* : upper threshold */
00041 hsvthreshold_Filter::hsvthreshold_Filter(uint32_t h,  uint32_t s,  uint32_t v,
00042                                          uint32_t h1, uint32_t s1, uint32_t v1,
00043                                          uint32_t h2, uint32_t s2, uint32_t v2) :
00044     phFilter("hsvthreshold_Filter")
00045 
00046 {
00047     this->m_format = phImageHSV24;
00048     this->set(h,s,v,h1,s1,v1,h2,s2,v2);
00049 }
00050 
00051 /* ---------------------------------------------------------------------- */
00052 hsvthreshold_Filter::~hsvthreshold_Filter()
00053 {
00054 }
00055 
00056 /* ------------------------------------------------------------------------ */
00058 phFilter *hsvthreshold_Filter::cloneFilter()
00059 {
00060     return (phFilter *)new hsvthreshold_Filter(this->m_h, this->m_s, this->m_v,
00061                                                this->m_h1,this->m_s1,this->m_v1,
00062                                                this->m_h2,this->m_s2,this->m_v2 );
00063 }
00064 
00065 /* ---------------------------------------------------------------------- */
00067 int hsvthreshold_Filter::set(uint32_t h,  uint32_t s,  uint32_t v,
00068                              uint32_t h1, uint32_t s1, uint32_t v1,
00069                              uint32_t h2, uint32_t s2, uint32_t v2)
00070 {
00071 
00072     this->m_h = h;
00073     this->m_s = s;
00074     this->m_v = v;
00075     
00076     this->m_h1 = h1;
00077     this->m_s1 = s1;
00078     this->m_v1 = v1;
00079     
00080     this->m_h2 = h2;
00081     this->m_s2 = s2;
00082     this->m_v2 = v2;
00083 
00084     return phSUCCESS;
00085 }
00086 
00087 /* ---------------------------------------------------------------------- */
00088 int hsvthreshold_Filter::filter()
00089 {
00090     phFUNCTION("hsvthreshold_Filter::filter")
00091     
00092     unsigned int i = 0;
00093     
00094     int32_t h_step = 16;
00095     int32_t s_step = 40;
00096     int32_t v_step = 30;
00097 
00098     uint8_t hi = 0;
00099     uint8_t si = 0;
00100     uint8_t vi = 0;
00101     uint8_t h = 0;
00102     uint8_t s = 0;
00103     uint8_t v = 0;
00104     
00105     uint8_t *imgptr = Image;
00106     const uint32_t limit = width * height;
00107     
00108     phImageFormatIndecies(this->format,&hi,&si,&vi,NULL);
00109     
00110     /* Begin filter */
00111 
00112     if ((!(this->format & phImageHSV24)) || (imgptr == NULL))
00113         return phSUCCESS;
00114         
00115     for (i = 0; i < limit; i++, imgptr += depth)
00116     {
00117         h = imgptr[hi];
00118         s = imgptr[si]; 
00119         v = imgptr[vi]; 
00120         
00121         /* Check to see if Saturation is being thresholded and set it to 0 if
00122          * it's out of bounds */
00123         if (this->m_s)
00124         {
00125             if (this->m_s1 > this->m_s2)
00126             {
00127                 if ((s > this->m_s1) || (s < this->m_s2)) h = s = v = 0;
00128             }
00129             else
00130             {
00131                 if ((s >= this->m_s1) && (s <= this->m_s2)) h = s = v = 0;
00132             }
00133             if (this->m_s == hsvthreshold_Group)
00134                 s = (s - (s % s_step)) + (s_step / 2);
00135         }
00136         /* Check to see if Value is being thresholded and set the pixel to
00137          * 0 if it's out of the bounds */
00138         if (this->m_v)
00139         {
00140             if (this->m_v1 > this->m_v2)
00141             {
00142                 if ((v > this->m_v1) || (v < this->m_v2)) h = s = v = 0;
00143             }
00144             else
00145             {
00146                 if ((v >= this->m_v1) && (v <= this->m_v2)) h = s = v = 0;
00147             }
00148             if (this->m_v == hsvthreshold_Group)
00149                 v = (v - (v % v_step)) + (v_step / 2);
00150         }
00151         
00152         /* Threshold Hue */
00153         if (this->m_h)
00154         {
00155             if (this->m_h1 > this->m_h2)
00156             {
00157                 if ((h > this->m_h1) || (h < this->m_h2)) h = s = v = 0;
00158             }
00159             else
00160             {
00161                 if ((h >= this->m_h1) && (h <= this->m_h2)) h = s = v = 0;
00162             }
00163             
00164             /* If Hue is being thresholded and Saturation/Value have non-zero values
00165              * then we will group the hues together */
00166             if ((this->m_h == hsvthreshold_Group) && (s > 0) && (v > 0))
00167                 h = (h - (h % h_step)) + (h_step / 2);
00168         }
00169         imgptr[hi] = h;
00170         imgptr[si] = s;
00171         imgptr[vi] = v;
00172     }
00173 
00174     /* End Filter */
00175 
00176     return phSUCCESS;
00177 }
00178 
00179 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:09 2007 for phission by  doxygen 1.4.4