From:https://blog.csdn.net/ibelievesunshine/article/details/105113458
【代码下载】https://gitee.com/zengxy2020/csdn/tree/master/gabor_filter
一般而言,网上gabor滤波器核函数的公式为复数形式,opencv等实际使用仅使用实数部分公式:
参数说明:
- Sigma : σ 表示高斯函数的标准差
- Gamma: γ 表示长宽比,决定这Gabor核函数图像的椭圆率
- Lambda:λ 表示滤波的波长
- Psi : ψ 相位偏移量,取值范围是-180~180
- Theta: θ 表示Gabor核函数图像的倾斜角度
x,y为gabor滤波器核函数,在opencv实现中,x,y的具体值由gabor核的大小决定,目测为经验值:
xmax = ksize.width/2;
ymax = ksize.height/2;
when ksize=9时 : xmin, ymin,xmax, ymax -4 -4 4 4
[[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]
[-4 -3 -2 -1 0 1 2 3 4]]
x:
[[-4 -4 -4 -4 -4 -4 -4 -4 -4]
[-3 -3 -3 -3 -3 -3 -3 -3 -3]
[-2 -2 -2 -2 -2 -2 -2 -2 -2]
[-1 -1 -1 -1 -1 -1 -1 -1 -1]
[ 0 0 0 0 0 0 0 0 0]
[ 1 1 1 1 1 1 1 1 1]
[ 2 2 2 2 2 2 2 2 2]
[ 3 3 3 3 3 3 3 3 3]
[ 4 4 4 4 4 4 4 4 4]]
参考:https://zhuanlan.zhihu.com/p/33311267
import cv2
import numpy as np
def gabor_kernel(ksize, sigma, gamma, lamda, alpha, psi):
'''
reference
https://en.wikipedia.org/wiki/Gabor_filter
'''
sigma_x = sigma
sigma_y = sigma / gamma
ymax = xmax = ksize // 2 # 9//2
xmin, ymin = -xmax, -ymax
# print("xmin, ymin,xmin, ymin",xmin, ymin,ymax ,xmax)
# X(第一个参数,横轴)的每一列一样, Y(第二个参数,纵轴)的每一行都一样
(y, x) = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1)) # 生成网格点坐标矩阵
# print("y\n",y)
# print("x\n",x)
x_alpha = x * np.cos(alpha) + y * np.sin(alpha)
y_alpha = -x * np.sin(alpha) + y * np.cos(alpha)
print("x_alpha[0][0]", x_alpha[0][0], y_alpha[0][0])
exponent = np.exp(-.5 * (x_alpha ** 2 / sigma_x ** 2 +
y_alpha ** 2 / sigma_y ** 2))
# print(exponent[0][0])
# print(x[0],y[0])
kernel = exponent * np.cos(2 * np.pi / lamda * x_alpha + psi)
print(kernel)
# print(kernel[0][0])
return kernel
def gabor_filter(gray_img, ksize, sigma, gamma, lamda, psi):
filters = []
for alpha in np.arange(0, np.pi, np.pi / 4):
print("alpha", alpha)
kern = gabor_kernel(ksize=ksize, sigma=sigma, gamma=gamma,
lamda=lamda, alpha=alpha, psi=psi)
filters.append(kern)
gabor_img = np.zeros(gray_img.shape, dtype=np.uint8)
i = 0
for kern in filters:
fimg = cv2.filter2D(gray_img, ddepth=cv2.CV_8U, kernel=kern)
gabor_img = cv2.max(gabor_img, fimg)
cv2.imwrite("2." + str(i) + "gabor.jpg", gabor_img)
i += 1
p = 1.25
gabor_img = (gabor_img - np.min(gabor_img, axis=None))**p
_max = np.max(gabor_img, axis=None)
gabor_img = gabor_img / _max
print(gabor_img)
gabor_img = gabor_img * 255
return gabor_img.astype(dtype=np.uint8)
def main():
src = cv2.imread("1.jpg")
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gabor_img = gabor_filter(src_gray,
ksize=9,
sigma=1,
gamma=0.5,
lamda=5,
psi=-np.pi / 2)
cv2.imwrite("gabor.jpg", gabor_img)
main()
// #include "opencv2/core.hpp"
#include
#include
#include
#include
using namespace cv;
using namespace std;
void gabor_filter(bool if_opencv_kernal,Mat gray_img, Mat& gabor_img, Mat gabor_tmp,int k=9, float sigma=1.0,float gamma=0.5,float lambda=5.0,float psi=-CV_PI / 2);
Mat gabor_kernal_wiki(Size ksize,double theta,double sigma=1.0,
double lambd=5.0, double gamma=0.5, double psi=-CV_PI / 2, int ktype=CV_32F);
int main()
{
clock_t total_start=clock();
Mat src=imread("1.jpg");
Mat src_gray,src_gray_gabor,gabor_tmp;
cvtColor(src,src_gray,COLOR_BGR2GRAY);
bool if_opencv_kernal=true;//false;
gabor_filter(if_opencv_kernal,src_gray, src_gray_gabor, gabor_tmp);
char save_name[100];
sprintf(save_name, "gabor_filter_img.jpg");
imwrite(save_name,src_gray_gabor);
clock_t total_end=clock();
cout<<"Total Time:"<<(double)(total_end - total_start)/ CLOCKS_PER_SEC*1000 <(y+ymax,x+xmax) = (float)v; // 从
// kernel.at(ymax - y, xmax - x) = (float)v; // opencv的赋值
}
else
kernel.at(y+ymax,x+xmax) = v;
}
// cout<