How to apply Gaussian filter to DFT output in OpenCV

wolvorinePk picture wolvorinePk · Mar 6, 2012 · Viewed 13k times · Source

I want to create a Gaussian high-pass filter after determining the correct padding size (e.g., if image width and height is 10X10, then should be 20X20).

I have Matlab code that I am trying to port in OpenCV, but I am having difficulty properly porting it. My Matlab code is show below:

f1_seg = imread('thumb1-small-test.jpg');

Iori = f1_seg;


% Iori = imresize(Iori, 0.2);

%Convert to grayscale
I = Iori;
if length(size(I)) == 3
    I = rgb2gray(Iori);
end
% 

%Determine good padding for Fourier transform

PQ = paddedsize(size(I));

I = double(I);

%Create a Gaussian Highpass filter 5% the width of the Fourier transform

D0 = 0.05*PQ(1);

H = hpfilter('gaussian', PQ(1), PQ(2), D0);

% Calculate the discrete Fourier transform of the image.

F=fft2(double(I),size(H,1),size(H,2));

% Apply the highpass filter to the Fourier spectrum of the image

HPFS_I = H.*F;

I know how to use the DFT in OpenCV, and I am able to generate its image, but I am not sure how to create the Gaussian filter. Please guide me to how I can create a high-pass Gaussian filter as is shown above?

Answer

mevatron picture mevatron · Mar 6, 2012

I believe where you are stuck is that the Gaussian filter supplied by OpenCV is created in the spatial (time) domain, but you want the filter in the frequency domain. Here is a nice article on the difference between high and low-pass filtering in the frequency domain.

Once you have a good understanding of how frequency domain filtering works, then you are ready to try to create a Gaussian Filter in the frequency domain. Here is a good lecture on creating a few different (including Gaussian) filters in the frequency domain.

If you are still having difficulty, I will try to update my post with an example a bit later!

EDIT : Here is a short example that I wrote on implementing a Gaussian high-pass filter (based on the lecture I pointed you to):

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>

using namespace cv;
using namespace std;

double pixelDistance(double u, double v)
{
    return cv::sqrt(u*u + v*v);
}

double gaussianCoeff(double u, double v, double d0)
{
    double d = pixelDistance(u, v);
    return 1.0 - cv::exp((-d*d) / (2*d0*d0));
}

cv::Mat createGaussianHighPassFilter(cv::Size size, double cutoffInPixels)
{
    Mat ghpf(size, CV_64F);

    cv::Point center(size.width / 2, size.height / 2);

    for(int u = 0; u < ghpf.rows; u++)
    {
        for(int v = 0; v < ghpf.cols; v++)
        {
            ghpf.at<double>(u, v) = gaussianCoeff(u - center.y, v - center.x, cutoffInPixels);
        }
    }

    return ghpf;
}


int main(int /*argc*/, char** /*argv*/)
{
    Mat ghpf = createGaussianHighPassFilter(Size(128, 128), 16.0);

    imshow("ghpf", ghpf);
    waitKey();

    return 0;
}

This is definitely not an optimized filter generator by any means, but I tried to keep it simple and straight forward to ease understanding :) Anyway, this code displays the following filter:

enter image description here

NOTE : This filter is not FFT shifted (i.e., this filter works when the DC is placed in the center instead of the upper-left corner). See the OpenCV dft.cpp sample (lines 62 - 74 in particular) on how to perform FFT shifting in OpenCV.

Enjoy!