I am very new in OpenCV and gabor filters and just want to get gabor wavelets like this:

I use this OpenCV code in Java:
double sigma_bar = 40;
double theta_bar = 0.5;
double lambda_bar = 11;
double gamma_bar = 100;
double psi_bar = 90;
double kernel_size = 150;
Mat intermediate = new Mat(150, 150,CvType.CV_8U);
Mat output = Mat.zeros(150, 150, CvType.CV_32F);
Mat gabor_mat = Imgproc.getGaborKernel(new Size(kernel_size, kernel_size),sigma_bar / 10., theta_bar / 180. * Math.PI,lambda_bar, gamma_bar / 100., psi_bar / 180. * Math.PI, CvType.CV_32F);
Imgproc.filter2D(intermediate, output, -1, gabor_mat);
Bitmap temp = Bitmap.createBitmap(intermediate.cols(), intermediate.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(output, temp);
ImageView gabor = (ImageView) findViewById(R.id.gabor);
gabor.setImageBitmap(temp);
and get this kind of output:

I suppose I don't need to apply Imgproc.filter2D, but without it my app crashes because of CvTypes and when I tried to solve that by converting types I get white screen.
Can anyone help me to get this gabor wavelet?
You simply need to:
normalize the gabor filter with NORM_MINMAX so that values fit in the range [0,255]
convert the normalized image to CV_8U (from CV_64F) with convertTo
The result image will be a single channel grayscale image.
I can't give you Java code to do that, but I think that this C++ snippet is still useful, since exists basically 1 to 1 conversion to Java. You then need to give to getGaborKernel the parameters to get the shape you need.
This is the result I get:

Code:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat kernel = getGaborKernel(Size(151, 151), 10, 0.1, 10, 1, CV_PI/2.0);
// kernel is of type CV_64F
Mat normalized;
normalize(kernel, normalized, 0, 255, NORM_MINMAX);
// normalized is of type CV_64F, but with values in [0, 255]
Mat converted;
normalized.convertTo(converted, CV_8U, 1, 0);
// converted is of type CV_8U
imshow("Gabor", converted);
waitKey();
return(0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With