Gaussian high-pass filter

来源:互联网 发布:香港买mac口红多少钱 编辑:程序博客网 时间:2024/06/03 17:38

up vote2down votefavorite
2

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 grayscaleI = Iori;if length(size(I)) == 3    I = rgb2gray(Iori);end% %Determine good padding for Fourier transformPQ = paddedsize(size(I));I = double(I);%Create a Gaussian Highpass filter 5% the width of the Fourier transformD0 = 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 imageHPFS_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?

shareimprove this question
 

1 Answer

activeoldestvotes
up vote7down voteaccepted

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!

shareimprove 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 0
原创粉丝点击