gabor.cpp
资源名称:gabor.rar [点击查看]
上传用户:soukeisyuu
上传日期:2022-07-03
资源大小:5943k
文件大小:5k
源码类别:
波变换
开发平台:
Visual C++
- #include "gabor.h"
- CvGabor::CvGabor()
- {
- }
- CvGabor::~CvGabor()
- {
- }
- void CvGabor::initGabor(int fre,int ang,int size,float sigma)
- {
- s_fre = fre;
- s_ang = ang;
- aperture_size = size;
- gaussian_sigma = sigma;
- freq = 0.5*PI/pow(sqrt(2.0),s_fre);
- angle = PI*s_ang/8;
- }
- void CvGabor::doGabor(IplImage *src,IplImage *dst)
- {
- CvMat *floatImg;
- CvMat *gaborReal,*gaborImage;
- CvMat *convReal,*convImage;
- int gaborSize = aperture_size;
- int halfSize = (gaborSize-1)/2;
- float sigma = gaussian_sigma;
- floatImg = cvCreateMat(src->height,src->width,CV_32FC1);
- gaborReal = cvCreateMat(gaborSize,gaborSize,CV_32FC1);
- gaborImage = cvCreateMat(gaborSize,gaborSize,CV_32FC1);
- convReal = cvCreateMat(src->height+gaborSize-1,src->width+gaborSize+1,CV_32FC1);
- convImage = cvCreateMat(src->height+gaborSize-1,src->width+gaborSize+1,CV_32FC1);
- char* src_data = src->imageData;
- int src_step = src->widthStep/sizeof(char);
- float* floatImg_data = floatImg->data.fl;
- int floatImg_step = floatImg->step/sizeof(float);
- for(int i=0;i<src->height;i++)
- for(int j=0;j<src->width;j++)
- floatImg_data[i*floatImg_step+j] = (float)src_data[i*src_step+j];
- float gaussPart,realPart,imagePart;
- float* gaborReal_data = gaborReal->data.fl;
- int gaborReal_step = gaborReal->step/sizeof(float);
- float* gaborImage_data = gaborImage->data.fl;
- int gaborImage_step = gaborImage->step/sizeof(float);
- for(int i=0;i<gaborSize;i++)
- for(int j=0;j<gaborSize;j++)
- {
- realPart = cos( freq*cos(angle)*(j-halfSize) + freq*sin(angle)*(i-halfSize) ) - exp( -0.5*sigma*sigma );
- imagePart = sin( freq*cos(angle)*(j-halfSize) + freq*sin(angle)*(i-halfSize) );
- gaussPart = (freq*freq)/(sigma*sigma)*exp( -0.5*(freq*freq)/(sigma*sigma)*((j-halfSize)*(j-halfSize)+(i-halfSize)*(i-halfSize)) );
- gaborReal_data[i*gaborReal_step+j] = gaussPart*realPart;
- gaborImage_data[i*gaborImage_step+j] = gaussPart*imagePart;
- }
- doConv(floatImg,gaborReal,convReal);
- doConv(floatImg,gaborImage,convImage);
- cvPow(convReal,convReal,2.0);
- cvPow(convImage,convImage,2.0);
- char* dst_data = dst->imageData;
- int dst_step = dst->widthStep/sizeof(char);
- float* convReal_data = convReal->data.fl;
- int convReal_step = convReal->step/sizeof(float);
- float* convImage_data = convImage->data.fl;
- int convImage_step = convImage->step/sizeof(float);
- for(int i=0;i<dst->height;i++)
- for(int j=0;j<dst->width;j++)
- {
- dst_data[i*dst_step+j] = min( 255, (int)sqrt(convReal_data[i*convReal_step+j]+convImage_data[i*convImage_step+j]) );
- }
- cvReleaseMat(&floatImg);
- cvReleaseMat(&gaborReal);
- cvReleaseMat(&gaborImage);
- cvReleaseMat(&convReal);
- cvReleaseMat(&convImage);
- }
- void CvGabor::doConv(CvMat *A,CvMat *B,CvMat* conv)
- {
- /*
- float a[] = {1,2,3,4};
- float b[] = {2,3,4,5};
- CvMat *A = new CvMat;
- CvMat *B = new CvMat;
- cvInitMatHeader(A,2,2,CV_32F,a);
- cvInitMatHeader(B,2,2,CV_32F,b);
- */
- /*
- CvMat* conv = cvCreateMat( A->rows + B->rows - 1, A->cols + B->cols - 1,A->type );
- */
- int dftgM = cvGetOptimalDFTSize( A->rows + B->rows - 1 );
- int dftgN = cvGetOptimalDFTSize( A->cols + B->cols - 1 );
- CvMat* dftgA = cvCreateMat( dftgM, dftgN, A->type );
- CvMat* dftgB = cvCreateMat( dftgM, dftgN, B->type );
- CvMat tmp;
- cvGetSubRect( dftgA, &tmp, cvRect(0,0,A->cols,A->rows));
- cvCopy( A, &tmp );
- cvGetSubRect( dftgA, &tmp, cvRect(A->cols,0,dftgA->cols - A->cols,A->rows));
- cvZero( &tmp );
- cvDFT( dftgA, dftgA, CV_DXT_FORWARD, A->rows );
- cvGetSubRect( dftgB, &tmp, cvRect(0,0,B->cols,B->rows));
- cvCopy( B, &tmp );
- cvGetSubRect( dftgB, &tmp, cvRect(B->cols,0,dftgB->cols - B->cols,B->rows));
- cvZero( &tmp );
- cvDFT( dftgB, dftgB, CV_DXT_FORWARD, B->rows );
- cvMulSpectrums( dftgA, dftgB, dftgA, 0);
- cvDFT( dftgA, dftgA, CV_DXT_INV_SCALE, conv->rows );
- cvGetSubRect( dftgA, &tmp, cvRect(0,0,conv->cols,conv->rows) );
- cvCopy( &tmp, conv );
- /*
- for(int i=0;i<2;i++)
- {
- for(int j=0;j<2;j++)
- {
- printf("%ft",cvmGet(A,i,j));
- }
- printf("n");
- }
- printf("n");
- for(int i=0;i<2;i++)
- {
- for(int j=0;j<2;j++)
- {
- printf("%ft",cvmGet(B,i,j));
- }
- printf("n");
- }
- printf("n");
- */
- /*
- for(int i=0;i<3;i++)
- {
- for(int j=0;j<3;j++)
- {
- printf("%ft",cvmGet(conv,i,j));
- }
- printf("n");
- }
- printf("n");
- */
- cvReleaseMat(&dftgA);
- cvReleaseMat(&dftgB);
- }