cvgabor.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Mian Zhou   *
00003  *   M.Zhou@reading.ac.uk   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <saliency_detection/cvgabor.h>
00021 
00022 #undef DEBUG
00023 //#define DEBUG
00024 CvGabor::CvGabor()
00025 {
00026 }
00027 
00028 
00029 CvGabor::~CvGabor()
00030 {
00031 cvReleaseMat( &Real );
00032 cvReleaseMat( &Imag );
00033 }
00034 
00035 
00049  CvGabor::CvGabor(int iMu, int iNu)
00050 {
00051     //Initilise the parameters 
00052     
00053     Sigma = 2*PI;
00054     F = sqrt(2.0);
00055     Init(iMu, iNu, Sigma, F);
00056     
00057 }
00058 
00073 CvGabor::CvGabor(int iMu, int iNu, float dSigma)
00074 { 
00075     F = sqrt(2.0);
00076     Init(iMu, iNu, dSigma, F);
00077 }
00078 
00079 
00095  CvGabor::CvGabor(int iMu, int iNu, float dSigma, float dF)
00096 {
00097 
00098     Init(iMu, iNu, dSigma, dF);
00099     
00100 }
00101 
00102 
00116  CvGabor::CvGabor(float dPhi, int iNu)
00117 {
00118 
00119     Sigma = 2*PI;
00120     F = sqrt(2.0);
00121     Init(dPhi, iNu, Sigma, F);
00122 }
00123 
00124 
00139  CvGabor::CvGabor(float dPhi, int iNu, float dSigma)
00140 {
00141 
00142     F = sqrt(2.0);
00143     Init(dPhi, iNu, dSigma, F);
00144 }
00145 
00146 
00162  CvGabor::CvGabor(float dPhi, int iNu, float dSigma, float dF)
00163 {
00164 
00165    Init(dPhi, iNu, dSigma,dF);
00166 }
00167 
00180 bool CvGabor::IsInit()
00181 {
00182 
00183     return bInitialised;
00184 }
00185 
00198 long CvGabor::mask_width()
00199 {
00200 
00201     long lWidth;
00202     if (IsInit() == FALSE)  {
00203        perror ("Error: The Object has not been initilised in mask_width()!\n");
00204        return 0;
00205     }
00206     else {
00207        //determine the width of Mask
00208       float dModSigma = Sigma/K;
00209       float dWidth = cvRound(dModSigma*6 + 1);
00210       //test whether dWidth is an odd.
00211       if (fmod(dWidth, (float)2.0)==0.0) dWidth++;
00212       lWidth = (long)dWidth;
00213 
00214       return lWidth;
00215     }
00216 }
00217 
00218 
00231 void CvGabor::creat_kernel()
00232 {
00233     
00234     if (IsInit() == FALSE) {perror("Error: The Object has not been initilised in creat_kernel()!\n");}
00235     else {
00236       CvMat *mReal, *mImag;
00237       mReal = cvCreateMat( Width, Width, CV_32FC1);
00238       mImag = cvCreateMat( Width, Width, CV_32FC1);
00239       
00240       /**************************** Gabor Function ****************************/ 
00241       int x, y;
00242       float dReal;
00243       float dImag;
00244       float dTemp1, dTemp2, dTemp3;
00245 
00246       for (int i = 0; i < Width; i++)
00247       {
00248           for (int j = 0; j < Width; j++)
00249           {
00250               x = i-(Width-1)/2;
00251               y = j-(Width-1)/2;
00252               dTemp1 = (pow(K,2)/pow(Sigma,2))*exp(-(pow((float)x,2)+pow((float)y,2))*pow(K,2)/(2*pow(Sigma,2)));
00253               dTemp2 = cos(K*cos(Phi)*x + K*sin(Phi)*y) - exp(-(pow(Sigma,2)/2));
00254               dTemp3 = sin(K*cos(Phi)*x + K*sin(Phi)*y);
00255               dReal = dTemp1*dTemp2;
00256               dImag = dTemp1*dTemp3; 
00257               //gan_mat_set_el(pmReal, i, j, dReal);
00258               cvmSet( (CvMat*)mReal, i, j, dReal );
00259               //gan_mat_set_el(pmImag, i, j, dImag);
00260               cvmSet( (CvMat*)mImag, i, j, dImag );
00261 
00262           } 
00263        }
00264        /**************************** Gabor Function ****************************/
00265        bKernel = TRUE;
00266        cvCopy(mReal, Real, NULL);
00267        cvCopy(mImag, Imag, NULL);
00268        #ifdef DEBUG
00269        printf("A %d x %d Gabor kernel with %f PI in arc is created.\n", Width, Width, Phi/PI);
00270        #endif
00271        cvReleaseMat( &mReal );
00272        cvReleaseMat( &mImag );
00273      }
00274 }
00275 
00276 
00289 IplImage* CvGabor::get_image(int Type)
00290 {
00291 
00292     if(IsKernelCreate() == FALSE)
00293     { 
00294       perror("Error: the Gabor kernel has not been created in get_image()!\n");
00295       return NULL;
00296     }
00297     else
00298     {  
00299     IplImage* pImage;
00300     IplImage *newimage;
00301     newimage = cvCreateImage(cvSize(Width,Width), IPL_DEPTH_8U, 1 );
00302     //printf("Width is %d.\n",(int)Width);
00303     //printf("Sigma is %f.\n", Sigma);
00304     //printf("F is %f.\n", F);
00305     //printf("Phi is %f.\n", Phi);
00306     
00307     //pImage = gan_image_alloc_gl_d(Width, Width);
00308     pImage = cvCreateImage( cvSize(Width,Width), IPL_DEPTH_32F, 1 );
00309     
00310     
00311     CvMat* kernel = cvCreateMat(Width, Width, CV_32FC1);
00312     double ve;
00313     switch(Type)
00314     {
00315         case 1:  //Real
00316 
00317            cvCopy( (CvMat*)Real, (CvMat*)kernel, NULL );
00318             //pImage = cvGetImage( (CvMat*)kernel, pImageGL );
00319            for (int i = 0; i < kernel->rows; i++)
00320            {
00321               for (int j = 0; j < kernel->cols; j++)
00322               {
00323                    ve = cvGetReal2D((CvMat*)kernel, i, j);
00324                    cvSetReal2D( (IplImage*)pImage, j, i, ve );
00325               }
00326            }
00327            break;
00328         case 2:  //Imag
00329            cvCopy( (CvMat*)Imag, (CvMat*)kernel, NULL );
00330            //pImage = cvGetImage( (CvMat*)kernel, pImageGL );
00331            for (int i = 0; i < kernel->rows; i++)
00332            {
00333               for (int j = 0; j < kernel->cols; j++)
00334               {
00335                    ve = cvGetReal2D((CvMat*)kernel, i, j);
00336                    cvSetReal2D( (IplImage*)pImage, j, i, ve );
00337               }
00338            }
00339            break; 
00340         case 3:  //Magnitude
00342            break;
00343         case 4:  //Phase
00345            break;
00346     }
00347    
00348     cvNormalize((IplImage*)pImage, (IplImage*)pImage, 0, 255, CV_MINMAX, NULL );
00349 
00350 
00351     cvConvertScaleAbs( (IplImage*)pImage, (IplImage*)newimage, 1, 0 );
00352 
00353     cvReleaseMat(&kernel);
00354 
00355     cvReleaseImage(&pImage);
00356 
00357     return newimage;
00358     }
00359 }
00360 
00361 
00374 bool CvGabor::IsKernelCreate()
00375 {
00376 
00377     return bKernel;
00378 }
00379 
00380 
00391 long CvGabor::get_mask_width()
00392 {
00393   return Width;
00394 }
00395 
00396 
00411 void CvGabor::Init(int iMu, int iNu, double dSigma, double dF)
00412 {
00413   //Initilise the parameters 
00414     bInitialised = FALSE;
00415     bKernel = FALSE;
00416 
00417     Sigma = dSigma;
00418     F = dF;
00419     
00420     Kmax = PI/2;
00421     
00422     // Absolute value of K
00423     K = Kmax / pow(F, (float)iNu);
00424     Phi = PI*iMu/8;
00425     bInitialised = TRUE;
00426     Width = mask_width();
00427     Real = cvCreateMat( Width, Width, CV_32FC1);
00428     Imag = cvCreateMat( Width, Width, CV_32FC1);
00429     creat_kernel();
00430 }
00431 
00432 
00452 void CvGabor::Init(double dPhi, int iNu, double dSigma, double dF)
00453 {
00454 
00455     bInitialised = FALSE;
00456     bKernel = FALSE;
00457     Sigma = dSigma;
00458     F = dF;
00459     
00460     Kmax = PI/2;
00461     
00462     // Absolute value of K
00463     K = Kmax / pow(F, (float)iNu);
00464     Phi = dPhi;
00465     bInitialised = TRUE;
00466     Width = mask_width();
00467     Real = cvCreateMat( Width, Width, CV_32FC1);
00468     Imag = cvCreateMat( Width, Width, CV_32FC1);
00469     creat_kernel();
00470 }
00471 
00472 
00473 
00486 CvMat* CvGabor::get_matrix(int Type)
00487 {
00488     if (!IsKernelCreate()) {perror("Error: the gabor kernel has not been created!\n"); return NULL;}
00489     switch (Type)
00490     {
00491       case CV_GABOR_REAL:
00492         return Real;
00493         break;
00494       case CV_GABOR_IMAG:
00495         return Imag;
00496         break;
00497       case CV_GABOR_MAG:
00498         return NULL;
00499         break;
00500       case CV_GABOR_PHASE:
00501         return NULL;
00502         break;
00503     }
00504     return NULL;
00505 }
00506 
00507 
00508 
00509 
00523 void CvGabor::output_file(const char *filename, int Type)
00524 {
00525   IplImage *pImage;
00526   pImage = get_image(Type);
00527   if(pImage != NULL)
00528   {
00529     if( cvSaveImage(filename, pImage )) printf("%s has been written successfully!\n", filename);
00530     else printf("Error: writting %s has failed!\n", filename);
00531   }
00532   else 
00533     perror("Error: the image is empty in output_file()!\n"); 
00534 
00535   cvReleaseImage(&pImage);
00536 }
00537 
00538 
00539 
00540 
00541 
00542 
00546 void CvGabor::show(int Type)
00547 {
00548     if(!IsInit()) {
00549         perror("Error: the gabor kernel has not been created!\n");
00550     }
00551     else {
00552     IplImage *pImage;
00553     pImage = get_image(Type);
00554     cvNamedWindow("Testing",1);
00555     cvShowImage("Testing",pImage);
00556     cvWaitKey(0);
00557     cvDestroyWindow("Testing");
00558     cvReleaseImage(&pImage);
00559     }
00560 
00561 }
00562 
00563 
00564 
00565 
00569 void CvGabor::conv_img(IplImage *src, IplImage *dst, int Type)
00570 {
00571     double ve, re,im;
00572 
00573     CvMat *mat = cvCreateMat(src->width, src->height, CV_32FC1);
00574     for (int i = 0; i < src->width; i++)
00575     {
00576        for (int j = 0; j < src->height; j++)
00577        {
00578               ve = cvGetReal2D((IplImage*)src, j, i);
00579               cvSetReal2D( (CvMat*)mat, i, j, ve );
00580        }
00581     }
00582 
00583     CvMat *rmat = cvCreateMat(src->width, src->height, CV_32FC1);
00584     CvMat *imat = cvCreateMat(src->width, src->height, CV_32FC1);
00585 
00586     CvMat *kernel = cvCreateMat( Width, Width, CV_32FC1 );
00587 
00588     switch (Type)
00589     {
00590       case CV_GABOR_REAL:
00591         cvCopy( (CvMat*)Real, (CvMat*)kernel, NULL );
00592         cvFilter2D( (CvMat*)mat, (CvMat*)mat, (CvMat*)kernel, cvPoint( (Width-1)/2, (Width-1)/2));
00593         break;
00594       case CV_GABOR_IMAG:
00595         cvCopy( (CvMat*)Imag, (CvMat*)kernel, NULL );
00596         cvFilter2D( (CvMat*)mat, (CvMat*)mat, (CvMat*)kernel, cvPoint( (Width-1)/2, (Width-1)/2));
00597         break;
00598       case CV_GABOR_MAG:
00599         /* Real Response */
00600         cvCopy( (CvMat*)Real, (CvMat*)kernel, NULL );
00601         cvFilter2D( (CvMat*)mat, (CvMat*)rmat, (CvMat*)kernel, cvPoint( (Width-1)/2, (Width-1)/2));
00602         /* Imag Response */
00603         cvCopy( (CvMat*)Imag, (CvMat*)kernel, NULL );
00604         cvFilter2D( (CvMat*)mat, (CvMat*)imat, (CvMat*)kernel, cvPoint( (Width-1)/2, (Width-1)/2));
00605         /* Magnitude response is the square root of the sum of the square of real response and imaginary response */
00606         for (int i = 0; i < mat->rows; i++)
00607         {
00608            for (int j = 0; j < mat->cols; j++)
00609            {
00610                re = cvGetReal2D((CvMat*)rmat, i, j);
00611                im = cvGetReal2D((CvMat*)imat, i, j);
00612                ve = sqrt(re*re + im*im);
00613                cvSetReal2D( (CvMat*)mat, i, j, ve );
00614            }
00615         }       
00616         break;
00617       case CV_GABOR_PHASE:
00618         break;
00619     }
00620     
00621     if (dst->depth == IPL_DEPTH_8U)
00622     {
00623         cvNormalize((CvMat*)mat, (CvMat*)mat, 0, 255, CV_MINMAX);
00624         for (int i = 0; i < mat->rows; i++)
00625         {
00626             for (int j = 0; j < mat->cols; j++)
00627             {
00628                 ve = cvGetReal2D((CvMat*)mat, i, j);
00629                 ve = cvRound(ve);
00630                 cvSetReal2D( (IplImage*)dst, j, i, ve );
00631             }
00632         }
00633      }
00634 
00635      if (dst->depth == IPL_DEPTH_32F)
00636      {
00637          for (int i = 0; i < mat->rows; i++)
00638          {
00639             for (int j = 0; j < mat->cols; j++)
00640             {
00641                 ve = cvGetReal2D((CvMat*)mat, i, j);
00642                 cvSetReal2D( (IplImage*)dst, j, i, ve );
00643             }
00644          }
00645      }       
00646 
00647     cvReleaseMat(&kernel);
00648     cvReleaseMat(&imat);   
00649     cvReleaseMat(&rmat);
00650     cvReleaseMat(&mat);
00651 }


saliency_detection
Author(s): Joris van de Weem
autogenerated on Sun Jan 5 2014 11:07:22