Butterworth filter

来源:互联网 发布:毛体 知乎 编辑:程序博客网 时间:2024/06/05 12:06

Butterworth filter

// Butterworth filter
//fc = cut frequency
//n filter order
//type: if tipo <0, low pass, if >=0 high pass
//the image shoul be square


IplImage *cinza = cvCreateImage(cvSize(img->width,img->height),
IPL_DEPTH_8U, 1);
IplImage *re64 = cvCreateImage(cvSize(img->width,img->height),
IPL_DEPTH_64F, 1);
IplImage *im64 = cvCreateImage(cvSize(img->width,img->height),
IPL_DEPTH_64F, 1);


CvMat *mask = cvCreateMat( img->height,img->width, CV_64FC1 );


int Xmin = -img->width/2;
int Ymin = -img->height/2;
int i, j;


// function transferece filter matrix
for(i=0; iwidth; i++)
for(j=0; jheight; j++)
{
double dxy = sqrt(pow(Xmin+i,2)+pow(Ymin+j,2));
double H;
if(tipo < 0)
{ H = (1/(1 + pow(dxy/fc,2*n)));} //low pass


if(tipo >= 0)
{
if(dxy==0){ H=0;}
else
H = (1/(1 + pow(fc/dxy,2*n))); //high pass
}


cvSetReal2D(mask, j, i, H);
}


cvCvtColor(img, cinza, CV_BGR2GRAY);
cvCvtScale(cinza, re64,1./255);
cvSetZero(im64);


cvFftw(re64,im64,true); //fft


cvMul(re64, mask, re64, 1);
cvMul(im64, mask, im64, 1);


cvFftw(re64,im64,false); //ifft


cvCvtScale(re64,dst ,255.);


cvReleaseImage(&re64);
cvReleaseImage(&im64);
cvReleaseImage(&cinza);