我正在编写一个函数 Conv2ByFFT() 来执行类似于 openCV api 中的 GaussianBlur() 的高斯模糊。但是当我比较 by function 和 GaussianBlur() api 之间的效果时,我发现前者不像后者那样“模糊”,我不知道为什么。
这是“正确”的
这是使用我的 Conv2ByFFT() 的结果
这是一些代码
void Conv2ByFFT(const Mat& f,const Mat& g,Mat& result)
{
result.create(abs(f.rows-g.rows)+1,abs(f.cols-g.cols)+1,f.type());
//pad the images and get optimal FFT size
Size dftSize;
dftSize.width = getOptimalDFTSize(f.cols + g.cols - 1);
dftSize.height = getOptimalDFTSize(f.rows + g.cols - 1);
Mat tmpF(dftSize,f.type(),Scalar::all(0));
Mat tmpG(dftSize,g.type(),Scalar::all(0));
Mat roiF(tmpF, Rect(0,0,f.cols,f.rows));
f.copyTo(roiF);
Mat roiG(tmpG, Rect(0,0,g.cols,g.rows));
g.copyTo(roiG);
//perform Fourier Transform
dft(tmpF,tmpF,0,f.rows);
dft(tmpG,tmpG,0,g.rows);
//perform per-element multiplication of two Fourier spectrums
mulSpectrums(tmpF,tmpG,tmpF,0);
//perform inverse Fourier Transform
dft(tmpF,tmpF,DFT_INVERSE+DFT_SCALE,result.rows);
tmpF(Rect(0,0,result.cols,result.rows)).copyTo(result);
}
int main()
{
//read image
const char* imagename = "c:\\lena.bmp";
Mat img = imread(imagename);
//check image
if(img.empty())
{ fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
if( !img.data )
return -1;
Mat src;
//convert the rgbimage into grayimage
cvtColor(img,src,CV_BGR2GRAY);
//save the grayimage
imwrite("lenagray.bmp",src);
//convert the image into float type
src.convertTo(src,CV_64FC1);
//******************************************************************************
// use GaussianBlur() in openCV
//******************************************************************************
//use Gaussian filter to blur the image
Mat dst = src.clone();
GaussianBlur(src,dst,Size(11,11),2);
//show and save the result
dst.convertTo(dst,CV_8U);
imshow("image",dst);
imwrite("lenablur.bmp",dst);
//******************************************************************************
// use GaussianBlur() in openCV
//******************************************************************************
//******************************************************************************
// use self-defining Conv2ByFFT()
//******************************************************************************
Mat result;
Mat gaussianFilter = getGaussianKernel(11,2,CV_64FC1);
//do the convolution to blur the image
Conv2ByFFT(src,gaussianFilter,result);
//show and save the result
result.convertTo(result,CV_8U);
//imshow("image1",result);
imwrite("lenablur1.bmp",result);
//******************************************************************************
// use self-defining Conv2ByFFT()
//******************************************************************************
cvWaitKey();
return 0;
}