4

openCV api の GaussianBlur() に似たガウスぼかしを行う関数 Conv2ByFFT() を作成しています。しかし、関数による効果と GaussianBlur() API の効果を比較すると、前者は後者ほど「ぼやけていない」ことがわかり、その理由がわかりません。

これが「正しい」ものです これは

これは私の Conv2ByFFT() を使用した結果です これは私の 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;
}
4

1 に答える 1

5

getGaussianKernel2 次元カーネルではなく、係数のベクトルを返します。

2 次元ガウス カーネルは分離可能であるため、畳み込み法では、このベクトルが両方向に適用され、完全なカーネルを一度に適用するのと同じ効果があります。

FFT 関数は、ベクトルを画像でたたみ込むだけです。よく見ると一方向にしかブレていない気がします。

完全な 2 次元ガウス カーネルを作成し、それを適用する必要があります。または、分離可能性を利用して、ベクトルを 2 回適用することもできると思います。

于 2011-10-12T10:09:43.033 に答える