Opencv中的getGaborKernel函数

getGaborKernel函數介紹

help(cv.getGaborKernel) #查看函数

Help on built-in function getGaborKernel:
getGaborKernel(…)
getGaborKernel(ksize, sigma, theta, lambd, gamma[, psi[, ktype]]) -> retval
. @brief Returns Gabor filter coefficients.
.
. For more details about gabor filter equations and parameters, see: Gabor
. Filter
.
.
. @param ksize Size of the filter returned.
. @param sigma Standard deviation of the gaussian envelope.
. @param theta Orientation of the normal to the parallel stripes of a Gabor function.
. @param lambd Wavelength of the sinusoidal factor.
. @param gamma Spatial aspect ratio.
. @param psi Phase offset.
. @param ktype Type of filter coefficients. It can be CV_32F or CV_64F .

gabor算子由一个高斯函数和一个余弦函数调制而成。 gabor算子由六个参数控制,其中三个根余弦函数有关,两个跟高斯函数有关.

Ksize: 滤波器的尺寸
Sigma : 高斯函数的标准差
Theta: 高斯函数
Lambd: 余弦函数的波长
Gamma: 高斯函数的宽高比,因为是二维高斯函数
Psi: 余弦函数的相位

准备用来傅里叶变换和可视化的函数

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
# 显示图像的函数
import matplotlib.pyplot as plt
def display_imgs(imgs,num_cols=5):
    num=len(imgs)
    num_rows=math.ceil(num/num_cols)
    fig=plt.figure(figsize=(num_cols*5,num_rows*5))
    for i in range(num):
        ax=fig.add_subplot(num_rows,num_cols,i+1)
        ax.imshow(imgs[i],cmap ='gray')
    plt.show()

# 傅里叶变换
def my_fft(img):
    f = np.fft.fft2(img)
#     print(f.shape,f.size,f.dtype)
    fshift = np.fft.fftshift(f).astype(np.float32)
#     fshift=fshift.real
    fshift=np.sqrt(np.power(fshift.imag,2)+np.power(fshift.real,2)) #获得幅值图像
#     print(fshift.shape,fshift.size,fshift.dtype)
    fshift=(fshift-np.min(fshift))/(np.max(fshift)-np.min(fshift)) #最大最小归一化
    return fshift
# help(np.complex128)

高斯函数 不同标准差下滤波核的差别

# 高斯函数 不同标准差的差别
wh=128
kernel1=cv2.getGaborKernel((wh, wh), 5, 0, 5, 0.5, 0, cv2.CV_32F)
kernel2=cv2.getGaborKernel((wh, wh), 10, 0, 5, 0.5, 0, cv2.CV_32F)
kernel3=cv2.getGaborKernel((wh, wh), 20, 0, 5, 0.5, 0, cv2.CV_32F)
kernel4=cv2.getGaborKernel((wh, wh), 40, 0, 5, 0.5, 0, cv2.CV_32F)
imgs=[kernel1,kernel2,kernel3,kernel4, my_fft(kernel1), my_fft(kernel2), my_fft(kernel3), my_fft(kernel4)]
display_imgs(imgs,num_cols=4 )

在这里插入图片描述

余弦函數的不同方向下滤波核的差别

# 余弦函數的方向
wh=128
kernel1=cv2.getGaborKernel((wh, wh), 10, np.degrees(0), 5, 0.5, 0, cv2.CV_32F)
kernel2=cv2.getGaborKernel((wh, wh), 10, np.degrees(30), 5, 0.5, 0, cv2.CV_32F)
kernel3=cv2.getGaborKernel((wh, wh), 10, np.degrees(60), 5, 0.5, 0, cv2.CV_32F)
kernel4=cv2.getGaborKernel((wh, wh), 10, np.degrees(90), 5, 0.5, 0, cv2.CV_32F)
imgs=[kernel1,kernel2,kernel3,kernel4, my_fft(kernel1), my_fft(kernel2), my_fft(kernel3), my_fft(kernel4)]
display_imgs(imgs,num_cols=4 )

在这里插入图片描述

不同余弦函數波长下的滤波核

# 余弦函數波长
wh=128

kernel1=cv2.getGaborKernel((wh, wh), 10, 0, 5, 0.5, 0, cv2.CV_32F)
kernel2=cv2.getGaborKernel((wh, wh), 10, 0, 10, 0.5, 0, cv2.CV_32F)
kernel3=cv2.getGaborKernel((wh, wh), 10, 0, 15, 0.5, 0, cv2.CV_32F)
kernel4=cv2.getGaborKernel((wh, wh), 10, 0, 20, 0.5, 0, cv2.CV_32F)
imgs=[kernel1,kernel2,kernel3,kernel4, my_fft(kernel1), my_fft(kernel2), my_fft(kernel3), my_fft(kernel4)]
display_imgs(imgs,num_cols=4 )

在这里插入图片描述

不同高斯函数宽高比情况下滤波核

(猜想 :二维高斯分布的两个标准差不同,导致宽高比不同?)

# 高斯函数宽高比(猜想 :二维高斯分布的两个标准差不同,导致宽高比不同?)
wh=128
kernel1=cv2.getGaborKernel((wh, wh), 10, 0, 5, 0.25, 0, cv2.CV_32F)
kernel2=cv2.getGaborKernel((wh, wh), 10, 0, 5, 0.5, 0, cv2.CV_32F)
kernel3=cv2.getGaborKernel((wh, wh), 10, 0, 5, 2, 0, cv2.CV_32F)
kernel4=cv2.getGaborKernel((wh, wh), 10, 0, 5, 4, 0, cv2.CV_32F)
imgs=[kernel1,kernel2,kernel3,kernel4, my_fft(kernel1), my_fft(kernel2), my_fft(kernel3), my_fft(kernel4)]
display_imgs(imgs,num_cols=4 )

在这里插入图片描述

不同余弦函数的相位偏移下的滤波核

# 余弦函数的相位偏移
wh=128
kernel1=cv2.getGaborKernel((wh, wh), 10, 0, 20, 1, 0, cv2.CV_32F)
kernel2=cv2.getGaborKernel((wh, wh), 10, 0,20, 1, 20, cv2.CV_32F)
kernel3=cv2.getGaborKernel((wh, wh), 10, 0, 20, 1, 40, cv2.CV_32F)
kernel4=cv2.getGaborKernel((wh, wh), 10, 0, 20, 1, 60, cv2.CV_32F)
imgs=[kernel1,kernel2,kernel3,kernel4, my_fft(kernel1), my_fft(kernel2), my_fft(kernel3), my_fft(kernel4)]
display_imgs(imgs,num_cols=4 )

在这里插入图片描述

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值