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

cv_canny_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 <cv_canny_Filter.h>
00033 
00034 #include <math.h>
00035 #include <cv.h>
00036 
00037 #include <phError.h>
00038 #include <phMemory.h>
00039 #include <phPrint.h>
00040 
00041 /* ---------------------------------------------------------------------- */
00042 cv_canny_Filter::cv_canny_Filter( int lowThreshold,
00043                                   int highThreshold,
00044                                   int aperture ) :
00045     phFilter("cv_canny_Filter")
00046 
00047 {
00048     this->m_format = (  phImageRGB24 |
00049                         phImageRGBA32 |
00050                         phImageGREY8 );
00051     this->m_edge = NULL;
00052     this->m_edgeSize = 0;
00053     this->set( lowThreshold, highThreshold, aperture );
00054    
00055     this->m_lastDepth   = 0;
00056     this->m_lastWidth   = 0;
00057     this->m_lastHeight  = 0;
00058     
00059     this->m_cv_input    = NULL;
00060     this->m_cv_gray     = NULL;
00061     this->m_cv_edges    = NULL;
00062     this->m_cv_output   = NULL;
00063 }
00064 
00065 /* ---------------------------------------------------------------------- */
00066 cv_canny_Filter::~cv_canny_Filter()
00067 {
00068     phFUNCTION("cv_canny_Filter::~cv_canny_Filter")
00069     IplImage *tmp = NULL;
00070     
00071     if (this->m_cv_input != NULL)
00072     {
00073         cvSetData( ((IplImage *)this->m_cv_input), NULL, 0 ); 
00074         tmp = ((IplImage *)this->m_cv_input);
00075         cvReleaseImageHeader(&tmp);
00076     }
00077     if (this->m_cv_gray != NULL)
00078     {
00079         tmp = ((IplImage *)this->m_cv_gray);
00080         cvReleaseImage(&tmp);
00081     }
00082     if (this->m_cv_edges != NULL)
00083     {
00084         tmp = ((IplImage *)this->m_cv_edges);
00085         cvReleaseImage(&tmp);
00086     }
00087     if (this->m_cv_output != NULL)
00088     {
00089         tmp = ((IplImage *)this->m_cv_output);
00090         cvReleaseImage(&tmp);
00091     }
00092 }
00093 
00094 /* ------------------------------------------------------------------------ */
00095 phFilter *cv_canny_Filter::cloneFilter()
00096 {
00097     phFUNCTION("cv_canny_Filter::cloneFilter")
00098     int locked = 0;
00099     cv_canny_Filter *cvcan = new cv_canny_Filter( );
00100 
00101     phTHIS_LOOSE_LOCK(locked);
00102     
00103     cvcan->set( this->m_lowThreshold,
00104                 this->m_highThreshold,
00105                 this->m_aperture );
00106 
00107     phTHIS_LOOSE_UNLOCK(locked);
00108     
00109     return (phFilter *)cvcan;
00110 }
00111 
00112 /* ---------------------------------------------------------------------- */
00113 int cv_canny_Filter::set( int lowThreshold,
00114                           int highThreshold,
00115                           int aperture )
00116 {
00117     phFUNCTION("cv_canny_Filter::set")
00118     int locked = 0;
00119 
00120     phTHIS_LOOSE_LOCK(locked);
00121     
00122     this->m_highThreshold   = highThreshold;
00123     this->m_lowThreshold    = lowThreshold;
00124     this->m_aperture        = aperture;
00125 
00126     phTHIS_LOOSE_UNLOCK(locked);
00127     
00128     return phSUCCESS;
00129 }
00130 
00131 /* ---------------------------------------------------------------------- */
00132 int cv_canny_Filter::filter()
00133 {
00134     phFUNCTION("cv_canny_Filter::filter")
00135     
00136     /* MY VARS HERE */
00137     int         step            = 0;
00138     CvSize      size            = {0,0};
00139     int         color_to_gray   = 0;
00140     int         gray_to_color   = 0;
00141     int         convert         = 0;
00142     int         alloc1          = 0;
00143     int         alloc2          = 0;
00144     IplImage   *canny_input     = NULL;
00145     IplImage   *edge_ptr        = NULL;
00146     IplImage   *tmp             = NULL;
00147 
00148     /* Begin Filter */
00149     if ((this->m_lastWidth != width) || (this->m_lastHeight != height))
00150     {
00151         alloc1 = alloc2 = 1;
00152     }
00153         
00154     if ((this->m_cv_input == NULL) || (this->m_lastDepth != depth))
00155     {
00156         alloc1 = 1;
00157     }
00158     
00159     if (alloc1)
00160     {
00161         if (this->m_cv_input != NULL)
00162         {
00163             IplImage *tmp = NULL;
00164             cvSetData( ((IplImage *)this->m_cv_input), NULL, 0 ); 
00165             tmp = ((IplImage *)this->m_cv_input);
00166             cvReleaseImageHeader(&tmp);
00167         }
00168         
00169         this->m_cv_input = 
00170             cvCreateImageHeader( cvSize(width,height),  
00171                                  IPL_DEPTH_8U, 
00172                                  depth );
00173     }
00174     if (alloc2)
00175     {
00176         if (this->m_cv_gray != NULL)
00177         {
00178             tmp = ((IplImage *)this->m_cv_gray);
00179             cvReleaseImage(&tmp);
00180         }
00181         
00182         this->m_cv_gray = cvCreateImage( cvSize(width,height),  IPL_DEPTH_8U, 1);
00183     }
00184     if (alloc2)
00185     {
00186         if (this->m_cv_edges != NULL)
00187         {
00188             tmp = (IplImage *)this->m_cv_edges;
00189             cvReleaseImage(&tmp);
00190         }
00191         
00192         this->m_cv_edges = cvCreateImage( cvSize(width,height),  IPL_DEPTH_8U, 1);
00193     }
00194     if (alloc1)
00195     {
00196         if (this->m_cv_output != NULL)
00197         {
00198             tmp =((IplImage *)this->m_cv_output);
00199             cvReleaseImage(&tmp);
00200         }
00201         
00202         this->m_cv_output = cvCreateImage( cvSize(width,height),  IPL_DEPTH_8U, depth);
00203     }
00204    
00205     this->m_lastDepth   = depth;
00206     this->m_lastWidth   = width;
00207     this->m_lastHeight  = height;
00208     
00209     cvSetData( ((IplImage *)this->m_cv_input), 
00210                Image, 
00211                width * depth * sizeof(uint8_t) ); 
00212     
00213     switch (format)
00214     {
00215         case phImageRGB24:
00216                 convert = 1;
00217                 color_to_gray = CV_RGB2GRAY;
00218                 gray_to_color = CV_GRAY2RGB;
00219                 break;
00220         case phImageRGBA32:
00221                 convert = 1;
00222                 color_to_gray = CV_RGBA2GRAY;
00223                 gray_to_color = CV_GRAY2RGBA;
00224                 break;
00225         case phImageGREY8:
00226                 convert = 0;
00227                 break;
00228     }
00229     
00230     if (convert)
00231     {
00232         cvCvtColor( ((IplImage *)this->m_cv_input), 
00233                     ((IplImage *)this->m_cv_gray), 
00234                     color_to_gray );
00235         canny_input = ((IplImage *)this->m_cv_gray);
00236     }
00237     else
00238     {
00239         canny_input = ((IplImage *)this->m_cv_input);
00240         //cvCopy(input,((IplImage *)this->m_cv_gray));
00241     }
00242             
00243     /* call to OpenCV cvCanny function */
00244 #if 0
00245     cvCanny( ((IplImage *)this->m_cv_gray),
00246              ((IplImage *)this->m_cv_edges),
00247              this->m_lowThreshold,
00248              this->m_highThreshold, 
00249              this->m_aperture );
00250 #else
00251     cvCanny( canny_input,
00252              ((IplImage *)this->m_cv_edges),
00253              this->m_lowThreshold,
00254              this->m_highThreshold, 
00255              this->m_aperture );
00256 #endif
00257    
00258     if (convert)
00259     {
00260         cvCvtColor( ((IplImage *)this->m_cv_edges), 
00261                     ((IplImage *)this->m_cv_output), 
00262                     gray_to_color );
00263         edge_ptr = ((IplImage *)this->m_cv_output);
00264     }
00265     else
00266     {
00267         edge_ptr = ((IplImage *)this->m_cv_edges);
00268     }
00269     cvGetRawData( edge_ptr, (uchar**)&this->m_edge, &step, &size );
00270 
00271     if (edge_ptr->depth != IPL_DEPTH_8U)
00272     {
00273         phPROGRESS("WARNING !!! Unsupported IplImage depth %d\n",
00274                    edge_ptr->depth);
00275     }
00276     
00277     depth = edge_ptr->nChannels;
00278     size    = cvGetSize(edge_ptr);
00279     width   = step / depth;
00280     height  = size.height;
00281     
00282     if (depth == 4)
00283         format = phImageRGBA32;
00284     else if (depth == 3)
00285         format = phImageRGB24;
00286     else if (depth == 1)
00287         format = phImageGREY8;
00288     
00289     this->m_workspaceImage->setImage( width /* width */,
00290                                       height/* height */,
00291                                       format,
00292                                       0, /* size: let the method
00293                                             figure out the size using
00294                                             the format flag */
00295                                       this->m_edge);
00296     /* End Filter */
00297     /* make sure to free up any space here, not including Image */
00298 
00299     return phSUCCESS;
00300 }
00301 
00302 




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