Gaussian high-pass filter


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?

share improve this question
 

1 Answer

up vote 7 down vote accepted

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!

share improve this answer
 
 
Thanks for the answer i am thinking i am very near to the result by your excellent guidance but I am not sure how can i produce the filter like this one which is produced in matlab... Edituploading.com/files/498a6e9b/filter.png –  wolvorinePk  Mar 7 '12 at 13:25 
 
this one is the output from matlab ... does it mean i need to create 4 different planes for this type of filter and merge all of them? i44.tinypic.com/r0twf7.png –  wolvorinePk  Mar 7 '12 at 13:38 
 
Actually, what is occurring there is that the filter has been shifted using fftshift in Matlab. As described in the first article I mentioned this, swaps quadrants 1 <-> 4 and 2 <-> 3. Also, I believe the dft.cpp OpenCV sample shows how to do this. –  mevatron  Mar 7 '12 at 13:50 
 
mevatron thanks a lots , you have saved my day and by using quads i am able to rearrange them according to the matlab displayed filter. –  wolvorinePk  Mar 10 '12 at 10:59
 
No problem! Glad I could help out! :) –  mevatron  Mar 10 '12 at 15:43
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值