Gabor Filter Learning

Source: Internet
Author: User
Tags abs comparison cos min pow printf sin

The purpose of this paper is to generate Gabor template with C implementation and to convolution the image. and simply mention the application of Gabor filter in texture feature extraction.

One, what is the Gabor function (the following part is translated from Wikipedia)

In image processing, the Gabor function is a linear filter for edge extraction. The frequency and direction of the Gabor filter is similar to that of the human visual system. The study found that Gabor filters are well suited for texture expression and separation. In the spatial domain, a two-dimensional Gabor filter is a Gaussian kernel function modulated by a sinusoidal plane wave.

Also, the biological experiment found that Gabor filter can be very good approximation of the single-cell sensing field function (light intensity stimulated by the transfer function), what the visual cortex of the super-column, Bla...bla, in short, is the bionic mathematical model.

In addition, there is a statement on the internet that Gabor is divided into real and imaginary parts, the image will be smoothed after the real part is filtered, and the imaginary part is filtered to detect the edge. "From Baidu to know the answer of a great God," I checked the literature, found that some people do use Gabor's odd function part of the edge extraction ("Gabor filter-based edge detection algorithm," Radio Engineering 2000 3rd volume 30th). In addition, there are similar findings from my experimental results. Think of this right for the moment.

The impulse response of the Gabor filter can be defined as a sine wave (for a two-dimensional Gabor filter is a sinusoidal plane wave) multiplied by a Gaussian function. Due to the multiplicative convolution properties, the Fourier transform of the Gabor filter's impulse response is the convolution of its harmonic function Fourier transform and Gaussian function Fourier transform. The filter consists of real and imaginary parts, which are orthogonal to each other. An array of Gabor functions in different directions of different frequencies is useful for image feature extraction.


The following is a mathematical representation of the two-dimensional Gabor function:

Plural expression:

Real part:

Imaginary part:

which


And


The following describes the meaning of each parameter in the formula, and how to configure the problem "all from the foreigner that translated":

Wavelength (λ): Its value is specified in pixels, usually greater than or equal to 2. But not greater than one-fifth of the input image size.

Direction (θ): This parameter specifies the direction of the Gabor function parallel stripe, which has a value of 0-360 degrees

Phase offset (φ): It has a value range of 180 degrees to 180 degrees. The 0he180 degrees correspond to the central symmetric center-on function and the Center-off function respectively, while the 90 and 90 degrees correspond to the opposing function.

Aspect ratio (gamma): The spatial aspect ratio determines the ellipse rate (ellipticity) of the Gabor function shape (support, which I translate into shapes). When γ= 1 o'clock, the shape is round. When γ< 1 o'clock, the shape is elongated with the direction of the parallel stripes. Typically, this value is 0.5

Bandwidth (b): The ratio of half-response space frequency bandwidth B and σ/λ for the Gabor filter, where σ represents the standard deviation of the Gaussian factor of the Gabor function, as follows:


The value of σ cannot be set directly, it only changes with bandwidth B. The bandwidth value must be a positive real number, usually 1, when the relationship between the standard deviation and the wavelength is: σ= 0.56λ. The smaller the bandwidth, the larger the standard deviation, the larger the Gabor shape, the more visible the parallel excitation and suppressed stripe number.

The Gabor kernel function renderings under different parameter configurations are given below, and the sizes are 100*100:

A. Wavelength comparison group "direction: 0, Phase offset is: 0, the aspect ratio is: 0.5, the bandwidth is: 1, the following image wavelength is 5,10,15"


B. Direction comparison group "wavelength: 10, Phase offset: 0, the space aspect ratio is: 0.5, the bandwidth is: 1, the direction is: 0,45,90"


C. Phase offset comparison group "wavelength: 10, direction: 0, Space aspect ratio: 0.5, Bandwidth: 1, Phase offset is: 0,180,-90,90"


D. Spatial aspect ratio comparison group "wavelength: 10, Phase offset: 0, Direction: 0, Bandwidth: 1, space aspect ratio: 0.5,1"


E. Bandwidth comparison group "wavelength: 10, Direction: 0, Phase offset: 0, space aspect ratio: 0.5, Bandwidth: 0.5,1,2"


Two, Gabor function implementation:

Matlab version, I found from the Pudn, but his Gabor function, I do not see how to understand:

Gabor functions:

function Gabor_k = Compute (X,y,f0,theta)
r = 1; g = 1;
X1 = X*cos (theta) + y*sin (theta);
Y1 =-x*sin (theta) + Y*cos (theta);

% draw the spatial and frequency domain function diagram of a Gabor filter
clear;
x = 0;
theta = 0;
F0 = 0.2;
For i = Linspace ( -15,15,50)
    x = x + 1;
    y = 0;
    for j = linspace ( -15,15,50)
        y = y + 1;
        Z (y,x) =compute (I,j,f0,theta);
    End
End
x = Linspace ( -15,15,50);
y = Linspace ( -15,15,50);
Surf (X,y,real (z))
title (' Gabor filter:real component ');
Xlabel (' x ');
Ylabel (' y ');
Zlabel (' z ');
Figure (2);
Surf (X,y,imag (z))
title (' Gabor filter:imaginary component ');
Xlabel (' x ');
Ylabel (' y ');
Zlabel (' z ');

z = fft2 (z);
U = linspace ( -0.5,0.5,50);
v = linspace ( -0.5,0.5,50);
Figure (3);
Surf (U,v,abs (Fftshift (Z)))
title (' Gabor filter:frequency component ');
Xlabel (' u ');
Ylabel (' V ');
Zlabel (' Z ');

Operation Result:






The Gabo filter in 4 directions shows clear through the image
;
x = 0;
theta = pi*3/4;% with radians 0,pi/4,pi/2,pi*3/4
F0 = 0.2; 
For i = Linspace ( -15,15,50)
    x = x + 1;
    y = 0;
    for j = linspace ( -15,15,50)
        y = y + 1;
        Z (y,x) =compute (I,j,f0,theta);
    End
End
z_real = Real (z);
m = min (Z_real (:));
Z_real = Z_real+abs (m);
M = Max (Z_real (:));
Imshow (1/m*z_real);
Figure (2)
z_imag = Imag (z);
m = min (Z_imag (:));
Z_imag = Z_imag+abs (m);
M = Max (Z_imag (:));
Imshow (1/M*Z_IMAG);

Operating effect:

Real part:

Imaginary part:

% Gabor filter in 4 directions to Lena Filter
clear;
I = Imread ('. \pic\lena.bmp ');
F0 = 0.2; 
Count = 0;
For theta = [0,pi/4,pi/2,pi*3/4];% in radians 0,pi/4,pi/2,pi*3/4
    count = count + 1;
    x = 0;
    For i = Linspace ( -8,8,11)
        x = x + 1;
        y = 0;
        for j = linspace ( -8,8,11)
            y = y + 1;
            Z (y,x) =compute (I,j,f0,theta);
        End
    End Figure
    (count);
    Filtered = Filter2 (z,i, ' valid ');
    F = ABS (filtered);
    Imshow (F/max (f (:)))
end

Operating effect:


Well, whatever he's doing. Probably feel it. Since I did not see how his Gabor function is defined, the parameter settings are not the same, the experimental results are very different, I hope I am right, heaven knows na. I can only write the following C code according to the function given by Wikipedia:

My_gabor.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/ highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include "math.h" #define PI 3.1415926 #define N 4 using
namespace Std;

using namespace CV;
	void M_filer (double *src,int height,int width,double *mask_rel,double *mask_img,int mw,int mh,int k) {IplImage *tmp;
	Double a,b,c;		Char res[20];

	Saved image Name TMP = Cvcreateimage (Cvsize (width,height), ipl_depth_8u,1);
			for (int i = 0;i < height;i++) {for (int j = 0;j < width;j++) {a = 0.0;
			b = 0.0;
			c = 0.0; Remove the rows near the boundary, cannot be filtered, out of range if (i > Int (MH/2) && i < Height-int (MH/2) && J > Int (mW) && J < Width-int (MW/2)) {for (int m = 0;m < mh;m++) {for (int n = 0;n < mw;n++) {//prin
						TF ("%f\n", src[(I+m-int (MH/2)) *width+ (J+n-int (MW))]); A + = src[(I+m-int (MH/2)) *width+ (J+n-int (MW))]*mask_rel[m*mw+n];
						B + = src[(I+m-int (MH/2)) *width+ (J+n-int (MW))]*mask_img[m*mw+n];
					printf ("%f,%f\n", A, b);
			}}} c = sqrt (a*a+b*b);
			C/= MW*MH;
		TMP-&GT;IMAGEDATA[I*WIDTH+J] = (unsigned char) c;
	}} sprintf (res, "result%d.jpg", K);
	Cvsaveimage (RES,TMP);
Cvreleaseimage (&AMP;TMP);
	} int _tmain (int argc, _tchar* argv[]) {iplimage *src;
	Double *rel,*img,*src_data,xtmp,ytmp,tmp1,tmp2,tmp3,re,im;		Double Theta,sigma,gamma,lambda,phi;

	5 parameters in the formula int gabor_height,gabor_width,x,y;
	src = cvloadimage ("test.jpg", Cv_load_image_grayscale);
	Gabor_height = 10;
	Gabor_width = 10;
	Gamma = 1.0;
	LAMBDA = 10.0;
	Sigma = 100;

	Phi = 0; rel = (double *) malloc (sizeof (double) *gabor_width*gabor_height);//REAL part of IMG = (double *) malloc (sizeof (double) *gabor_	Width*gabor_height);//Imaginary part Src_data = (double *) malloc (sizeof (double) *src->widthstep*src->height); Image data for (int. i=0;i<src->height;i++) {for (int j=0;j<src->widthstep;j++) {src_data[i*src->widthstep+j]= (unsigned char) src->imagedata[i*src->widthstep+j];
		printf ("%f\n", Src_data[i*src->widthstep+j]);
		}}//Construct Gabor function for (int k = 0;k < n;k++)//define N Direction {Theta = pi* ((double) k/n); for (int i = 0;i < gabor_height;i++)//define template size {for (int j = 0;j < gabor_width;j++) {x = J-gabor_widt
				H/2;

				y = I-GABOR_HEIGHT/2;
				Xtmp = (double) X*cos (Theta) + (double) y*sin (Theta);

				Ytmp = (double) Y*cos (Theta)-(double) x*sin (Theta);
				TMP1 = exp (-(POW (xtmp,2) +pow (ytmp*gamma,2))/(2*pow (sigma,2)));
				tmp2 = cos (2*pi*xtmp/lambda + Phi);
				
				Tmp3 = sin (2*pi*xtmp/lambda + Phi);
				re = TMP1*TMP2;

				im = Tmp1*tmp3;
				REL[I*GABOR_WIDTH+J] = re;
				IMG[I*GABOR_WIDTH+J] = im;
			printf ("%f,%f\n", Re,im);
	}}//Use the Gabor function in different directions to filter the image and save the picture M_filer (src_data,src->height,src->width,rel,img,10,10,k);
	} free (rel), Free (IMG), free (src_data);
return 0;
 }

Operating effect:


It's probably just a way to live. My side of the real and imaginary part of the processing is to use the method of modulo. If there is a problem, please bring it to the masses.


Iii. the idea of extracting texture features with Gabor "copy someone else's paper"

The main idea of Gabor filtering method is: different textures generally have different center frequency and bandwidth, according to these frequency and bandwidth can design a set of Gabor filter to filter the texture image, each Gabor filter only allows the texture of its frequency corresponding to pass smoothly, and the other texture of the energy is suppressed, The texture features are analyzed and extracted from the output results of each filter for subsequent classification or segmentation tasks. The extraction of texture features from Gabor filters consists of two processes: ① design filters (such as functions, numbers, orientations, and intervals), and ② extracts effective texture feature sets from the output of the filter. The Gabor filter is a band-pass filter, and its unit impulse response function (Gabor function) is the product of the Gaussian function and the complex exponential function. It is a function to reach the lower bound of the time-frequency measurement, which has the best consideration of the signal resolution in the time-frequency domain.

Implementation steps:

(1) The input image is divided into 3x3 (9 blocks) and 4x4 (16 blocks) of the image block;

(2) To establish Gabor filter group: Select 4 Scales, 6 directions, so that 24 Gabor filters are formed;

(3) Gabor Filter group and each image block in the spatial convolution, each image block can be obtained 24 filter output, these output is the image block size image, if directly as a feature vector, the dimension of the feature space will be very large, so need "enrichment";

(4) Each image block through the Gabor Filter group 24 output, to "condense" (the article mentions "average filter responses within the block" I understand is to take the gray mean) for a 24x1 column vector as the image block Texture features. Consult the relevant literature and find that the variance can also be used.

Using a real image, the image texture information can be obtained effectively by using Gabor Filter group of 4scales*6orientations to extract texture feature according to the original document. Where the results of a group of the same scale are taken out separately, the display is shown below. "The results of other people's experiments, no code, I didn't do it."


Well, today wrote here, hurriedly DotA went, have the problem must timely tell me AH:)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.