/***************************************************************************
* Copyright (C) 2006 by Mian Zhou *
* M.Zhou@reading.ac.uk *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "cvgabor.h"
CvGabor::CvGabor()
{
}
CvGabor::~CvGabor()
{
cvReleaseMat( &Real );
cvReleaseMat( &Imag );
}
/*!
\fn CvGabor::CvGabor(int iMu, int iNu, double dSigma)
Construct a gabor
Parameters:
iMu The orientation iMu*PI/8,
iNu The scale,
dSigma The sigma value of Gabor,
Returns:
None
Create a gabor with a orientation iMu*PI/8, a scale iNu, and a sigma value dSigma. The spatial frequence (F) is set to sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
CvGabor::CvGabor(int iMu, int iNu, double dSigma)
{
F = sqrt(2.0);
Init(iMu, iNu, dSigma, F);
}
/*!
\fn CvGabor::CvGabor(int iMu, int iNu, double dSigma, double dF)
Construct a gabor
Parameters:
iMu The orientation iMu*PI/8
iNu The scale
dSigma The sigma value of Gabor
dF The spatial frequency
Returns:
None
Create a gabor with a orientation iMu*PI/8, a scale iNu, a sigma value dSigma, and a spatial frequence dF. It calls Init() to generate parameters and kernels.
*/
CvGabor::CvGabor(int iMu, int iNu, double dSigma, double dF)
{
Init(iMu, iNu, dSigma, dF);
}
/*!
\fn CvGabor::CvGabor(double dPhi, int iNu)
Construct a gabor
Parameters:
dPhi The orientation in arc
iNu The scale
Returns:
None
Create a gabor with a orientation dPhi, and with a scale iNu. The sigma (Sigma) and the spatial frequence (F) are set to 2*PI and sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
CvGabor::CvGabor(double dPhi, int iNu)
{
Sigma = 2*PI;
F = sqrt(2.0);
Init(dPhi, iNu, Sigma, F);
}
/*!
\fn CvGabor::CvGabor(double dPhi, int iNu, double dSigma)
Construct a gabor
Parameters:
dPhi The orientation in arc
iNu The scale
dSigma The sigma value of Gabor
Returns:
None
Create a gabor with a orientation dPhi, a scale iNu, and a sigma value dSigma. The spatial frequence (F) is set to sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
CvGabor::CvGabor(double dPhi, int iNu, double dSigma)
{
F = sqrt(2.0);
Init(dPhi, iNu, dSigma, F);
}
/*!
\fn CvGabor::CvGabor(double dPhi, int iNu, double dSigma, double dF)
Construct a gabor
Parameters:
dPhi The orientation in arc
iNu The scale
dSigma The sigma value of Gabor
dF The spatial frequency
Returns:
None
Create a gabor with a orientation dPhi, a scale iNu, a sigma value dSigma, and a spatial frequence dF. It calls Init() to generate parameters and kernels.
*/
CvGabor::CvGabor(double dPhi, int iNu, double dSigma, double dF)
{
Init(dPhi, iNu, dSigma,dF);
}
/*!
\fn CvGabor::IsInit()
Determine the gabor is initilised or not
Parameters:
None
Returns:
a boolean value, TRUE is initilised or FALSE is non-initilised.
Determine whether the gabor has been initlized - variables F, K, Kmax, Phi, Sigma are filled.
*/
bool CvGabor::IsInit()
{
return bInitialised;
}
/*!
\fn CvGabor::mask_width()
Give out the width of the mask
Parameters:
None
Returns:
The long type show the width.
Return the width of mask (should be NxN) by the value of Sigma and iNu.
*/
long CvGabor::mask_width()
{
long lWidth;
if (IsInit() == false) {
perror ("Error: The Object has not been initilised in mask_width()!\n");
return 0;
}
else {
//determine the width of Mask
double dModSigma = Sigma/K;
double dWidth = cvRound(dModSigma*6 + 1);
//test whether dWidth is an odd.
if (fmod(dWidth, 2.0)==0.0) dWidth++;
lWidth = (long)dWidth;
return lWidth;
}
}
/*!
\fn CvGabor::creat_kernel()
Create gabor kernel
Parameters:
None
Returns:
None
Create 2 gabor kernels - REAL and IMAG, with an orientation and a scale
*/
void CvGabor::creat_kernel()
{
if (IsInit() == false) {perror("Error: The Object has not been initilised in creat_kernel()!\n");}
else {
CvMat *mReal, *mImag;
mReal = cvCreateMat( Width, Width, CV_32FC1);
mImag = cvCreateMat( Width, Width, CV_32FC1);
/**************************** Gabor Function ****************************/
int x, y;
double dReal;
double dImag;
double dTemp1, dTemp2, dTemp3;
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Width; j++)
{
x = i-(Width-1)/2;
y = j-(Width-1)/2;
dTemp1 = (pow(K,2)/pow(Sigma,2))*exp(-(pow((double)x,2)+pow((double)y,2))*pow(K,2)/(2*pow(Sigma,2)));
dTemp2 = cos(K*cos(Phi)*x + K*sin(Phi)*y) - exp(-(pow(Sigma,2)/2));
dTemp3 = sin(K*cos(Phi)*x + K*sin(Phi)*y);
dReal = dTemp1*dTemp2;
dImag = dTemp1*dTemp3;
//gan_mat_set_el(pmReal, i, j, dReal);
//cvmSet( (CvMat*)mReal, i, j, dReal );
cvSetReal2D((CvMat*)mReal, i, j, dReal );
//gan_mat_set_el(pmImag, i, j, dImag);
//cvmSet( (CvMat*)mImag, i, j, dImag );
cvSetReal2D((CvMat*)mImag, i, j, dImag );
}
}
/**************************** Gabor Function ****************************/
bKernel = true;
cvCopy(mReal, Real, NULL);
cvCopy(mImag, Imag, NULL);
//printf("A %d x %d Gabor kernel with %f PI in arc is created.\n", Width, Width, Phi/PI);
cvReleaseMat( &mReal );
cvReleaseMat( &mImag );
}
}
/*!
\fn CvGabor::get_image(int Type)
Get the speific type of image of Gabor
Parameters:
Type The Type of gabor kernel, e.g. REAL, IMAG, MAG, PHASE
Returns:
Pointer to image structure, or NULL on failure
Return an Image (gandalf image class) with a specific Type "REAL" "IMAG" "MAG" "PHASE"
*/
IplImage* CvGabor::get_image(int Type)
{
if(IsKernelCreate() == false)
{
perror("Error: the Gabor kernel has not been created in get_image()!\n");
return NULL;
}
else
{
IplImage* pImage;
IplImage *newimage;
newimage = cvCreateImage(cvSize(Width,Width), IPL_DEPTH_8U, 1 );
//printf("Width is %d.\n",(int)Width);
//printf("Sigma is %f.\n", Sigma);
//printf("F is %f.\n", F);
//printf("Phi is %f.\n", Phi);
//pImage = gan_image_alloc_gl_d(Width, Width);
pImage = cvCreateImage( cvSize(Width,Width), IPL_DEPTH_32F, 1 );
CvMat* kernel = cvCreateMat(Width, Width, CV_32FC1);
CvMat* re = cvCreateMat(Width, Width, CV_32FC1);
CvMat* im = cvCreateMat(Width, Width, CV_32FC1);
double ve, ve1,ve2;
CvScalar S;
CvSize size = cvGetSize( kernel );
int rows = size.height;
int cols = size.width;
switch(Type)
{
case 1: //Real
cvCopy( (CvMat*)Real, (CvMat*)kernel, NULL );
//pImage = cvGetImage( (CvMat*)kernel, pImageGL );
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
ve = cvGetReal2D((CvMat*)kernel, i, j);
cvSetReal2D( (IplImage*)pImage, j, i, ve );
}
}
break;
case 2: //Imag
cvCopy( (CvMat*)Imag, (CvMat*)kernel, NULL );
//pImage = cvGetImage( (CvMat*)kernel, pImageGL );
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
- 1
- 2
- 3
- 4
- 5
- 6
前往页