How to apply gabor filter to images in opencv?

c++, image, opencv

Solution

basically, you convert your img to float,

then construct a kernel:

cv::Mat kernel = cv::getGaborKernel(cv::Size(kernel_size,kernel_size), sig, th, lm, gm, ps);

and apply it with filter2D:

cv::filter2D(src_f, dest, CV_32F, kernel);

[edit]

** i'm not sure, but you'll probably need a 1channel image as input.

** imshow sees, your image is float, and just saturates anything beyond 1.0, so you get an all white image.

(this is just a visualization problem, needs a bit of conversion/scaling to cure it)

Mat in = imread("XfNal.jpg",0);          // load grayscale
Mat dest;
Mat src_f;
in.convertTo(src_f,CV_32F);

int kernel_size = 31;
double sig = 1, th = 0, lm = 1.0, gm = 0.02, ps = 0;
cv::Mat kernel = cv::getGaborKernel(cv::Size(kernel_size,kernel_size), sig, th, lm, gm, ps);
cv::filter2D(src_f, dest, CV_32F, kernel);

cerr << dest(Rect(30,30,10,10)) << endl; // peek into the data

Mat viz;
dest.convertTo(viz,CV_8U,1.0/255.0);     // move to proper[0..255] range to show it
imshow("k",kernel);
imshow("d",viz);
waitKey();

Problem

I've got some wavelets with the gabor filter code, it's something like this.. but i don't know how to use it on my image? i know there are some ways with matlab,i.e matlab way. but I'm using `opencv`, and I'm very new to this field and `matlab`, I don't know how to write the opencv code from the matlab code, so , waht am I supposed to do this with `opencv`? thanks very much! ****Update**** I've tried @berak's way, and this is the original image and this is after I applied the filter just all white and nothing left,below is my params, ``` int kernel_size = 31; double sig = 1, th = 0, lm = 1.0, gm = 0.02, ps = 0; cv::Mat kernel = cv::getGaborKernel(cv::Size(kernel_size, kernel_size), sig, th, lm, gm, ps); cv::filter2D(src_f, dest, CV_32F, kernel); ``` is there anything wrong with my setting?

Original source

Related problems