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

mask_Filter.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2005 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 <mask_Filter.h>
00033 
00034 #include <phError.h>
00035 #include <phMemory.h>
00036 #include <phPrint.h>
00037 
00038 /* ------------------------------------------------------------------------ */
00039 mask_Filter::mask_Filter(   uint8_t     threshold, 
00040                             float       mask_rate,
00041                             float       unmask_rate ) : phFilter("mask_filter")
00042 {
00043     this->m_mask_buffer     = NULL;
00044     this->m_mask_buffer_size= 0;
00045     this->m_mask_image      = NULL;
00046     this->m_mask_image_size = 0;
00047     this->m_format          = phImageValidFormatMask;
00048 
00049     this->m_image = new phImage();
00050 
00051     this->set( threshold, mask_rate, unmask_rate );
00052 }
00053 
00054 /* ------------------------------------------------------------------------ */
00055 mask_Filter::~mask_Filter()
00056 {
00057     phFUNCTION("mask_Filter::~mask_Filter")
00058     int locked = 0;
00059     
00060     phTHIS_LOOSE_LOCK(locked);
00061     
00062     phFree(this->m_mask_image);
00063     this->m_mask_image_size = 0;
00064     
00065     phFree(this->m_mask_buffer);
00066     this->m_mask_buffer_size = 0;
00067 
00068     phDelete(this->m_image);
00069     
00070     phTHIS_LOOSE_UNLOCK(locked);
00071 }
00072 
00073 /* ------------------------------------------------------------------------ */
00074 int mask_Filter::set(   uint8_t     threshold,
00075                         float       mask_rate,
00076                         float       unmask_rate )
00077 {
00078     phFUNCTION("mask_Filter::set")
00079     int locked = 0;
00080     
00081     phTHIS_LOOSE_LOCK(locked);
00082     
00083     if (mask_rate > 1.0f) 
00084         mask_rate = 1.0f;
00085     
00086     if (mask_rate < 0.0f) 
00087         mask_rate = 0.01f; /* default */
00088     
00089     if (unmask_rate > 1.0f) 
00090         unmask_rate = 1.0f;
00091     
00092     if (unmask_rate < 0.0f) 
00093         unmask_rate = 0.01f; /* default */
00094     
00095     this->m_threshold = threshold;
00096     this->m_mask_rate = mask_rate;
00097     this->m_unmask_rate = unmask_rate;
00098     
00099     phTHIS_LOOSE_UNLOCK(locked);
00100 
00101     return phSUCCESS;
00102 }
00103 
00104 /* ------------------------------------------------------------------------ */
00105 uint8_t mask_Filter::getThreshold()
00106 {
00107     phFUNCTION("mask_Filter::getThreshold")
00108     int     locked = 0;
00109     uint8_t retval = 0;
00110 
00111     phTHIS_LOOSE_LOCK(locked);
00112 
00113     retval = this->m_threshold;
00114     
00115     phTHIS_LOOSE_UNLOCK(locked);
00116 
00117     return retval;
00118 }
00119 
00120 /* ------------------------------------------------------------------------ */
00121 float mask_Filter::getMaskRate()
00122 {
00123     phFUNCTION("mask_Filter::getMaskRate")
00124     int     locked = 0;
00125     float   retval = 0.0;
00126 
00127     phTHIS_LOOSE_LOCK(locked);
00128 
00129     retval = this->m_mask_rate;
00130     
00131     phTHIS_LOOSE_UNLOCK(locked);
00132 
00133     return retval;
00134 }
00135 
00136 /* ------------------------------------------------------------------------ */
00137 float mask_Filter::getUnmaskRate()
00138 {
00139     phFUNCTION("mask_Filter::getUnmaskRate")
00140     int     locked = 0;
00141     float   retval = 0.0;
00142 
00143     phTHIS_LOOSE_LOCK(locked);
00144 
00145     retval = this->m_unmask_rate;
00146     
00147     phTHIS_LOOSE_UNLOCK(locked);
00148 
00149     return retval;
00150 }
00151 
00152 /* ------------------------------------------------------------------------ */
00153 phImage *mask_Filter::getMaskImage()
00154 {
00155     phFUNCTION("mask_Filter::getMaskImage")
00156     int     locked = 0;
00157     phImage *retval = NULL;
00158 
00159     phTHIS_LOOSE_LOCK(locked);
00160 
00161     retval = this->m_image;
00162     
00163     phTHIS_LOOSE_UNLOCK(locked);
00164 
00165     return retval;
00166 }
00167 /* ------------------------------------------------------------------------ */
00168 phFilter *mask_Filter::cloneFilter()
00169 {
00170     return (phFilter *)(new mask_Filter(this->m_threshold,
00171                                         this->m_mask_rate,
00172                                         this->m_unmask_rate ));
00173 }
00174 
00175 /* ------------------------------------------------------------------------ */
00176 int mask_Filter::filter()
00177 {
00178     phFUNCTION("mask_Filter::filter")
00179 
00180     uint32_t    float_bufsize= width * height * depth * sizeof(float);
00181     uint32_t    mask_bufsize = width * height * depth * sizeof(uint8_t);
00182     uint32_t    i       = 0;
00183     uint32_t    limit   = 0;
00184     float      *fptr    = NULL;
00185     uint8_t    *iptr    = NULL;
00186     uint8_t    *mptr    = NULL;
00187     float       weight  = 0.0;
00188     float       f_adjust= 0.0;
00189     uint8_t     b_adjust= 0;
00190     int         do_mask_image = 0;
00191     
00192     if (this->m_image->hasClients() > 0)
00193         do_mask_image = 1;
00194     
00195     /* defined before being called:
00196      *  width
00197      *  height
00198      *  depth
00199      *  Image
00200      */
00201     phDALLOC_RESIZE(this->m_mask_buffer,
00202                     this->m_mask_buffer_size,
00203                     float_bufsize,
00204                     float);
00205     if (do_mask_image)
00206     {
00207         phDALLOC_RESIZE(this->m_mask_image,
00208                         this->m_mask_image_size,
00209                         mask_bufsize,
00210                         uint8_t);
00211     }
00212 
00213     /* Begin Filter */
00214     limit = width * height * depth;
00215 
00216     /* Update the mask and mask out the pixels at the same time */
00217     iptr = Image;
00218     fptr = this->m_mask_buffer;
00219     if (do_mask_image)
00220     {
00221         mptr = this->m_mask_image;
00222     }
00223     for (i = 0; i < limit; i++)
00224     {
00225         /* get the current weight */
00226         weight = *fptr;
00227 
00228         /* Calculate whether the pixel is greater than the threshold
00229          * and adjust the weight by the mask_rate */
00230         if (*iptr > this->m_threshold) 
00231             weight += this->m_mask_rate;
00232         else
00233             weight -= this->m_unmask_rate;
00234         
00235         /* make sure we don't go below 0.0 */
00236         if (weight < 0.0) weight = 0.0;
00237         
00238         /* Adjust the value of the pixel */
00239         f_adjust = ((*iptr) * (*fptr));
00240         if (f_adjust > 255.0)   f_adjust = 255.0;
00241         if (f_adjust < 0.0)     f_adjust = 0.0;
00242         b_adjust = (uint8_t)f_adjust;
00243         *iptr -= b_adjust;
00244         
00245         /* save the new weight */
00246         *fptr = weight;
00247         if (mptr != NULL) *mptr = (uint8_t)(255.0 * weight);
00248         
00249         iptr++;
00250         fptr++;
00251         if (mptr != NULL) mptr++;
00252     }
00253     
00254     /* End Filter */
00255 
00256     if (do_mask_image)
00257     {
00258         rc = this->m_image->setImage(width,height,format,0,this->m_mask_image);
00259         phPRINT_RC(rc,NULL,"this->m_image->set");
00260     }
00261     /* make sure to free up any space here, not including Image */
00262     
00263     return phSUCCESS;
00264  
00265 error:
00266     return phFAIL;
00267 }
00268 
00269 




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