MATLAB的imgaborfilt和imgaussfilt函数的python版本是什么?

2024-05-16 23:58:03 发布

您现在位置:Python中文网/ 问答频道 /正文

试图用python实现任务here。我需要对灰度图像进行Garbor滤波,然后对Garbor滤波后的图像进行高斯滤波。在观察Python代码和MATLAB代码的结果时,我发现它们完全不同。你知道吗

python中有用于Garbor和GaussianBlur的函数cv2.filter2D cv2.GaussianBlur,就像MATLAB中有imgaborfilt和imgaussfilt一样。使用代码片段中所示的python函数返回的矩阵仅为0和1,而MATLAB返回的矩阵为浮点数。你知道吗


def garbor_filters(wavelength, orientation):
  ksize = 39
  kern = cv2.getGaborKernel(ksize=(ksize, ksize), sigma = 0.5, theta=orientation, lambd=wavelength, gamma=math.sqrt(2), psi=0, ktype=cv2.CV_32F)
  return kern


def compute_garbor(img, filter):
 #filter: kernel obtained from the method above 
 filtered_img = cv2.filter2D(img, cv2.CV_8UC3, filter)
 return filtered_img

def getgaussianFiltrdImg(imageGarbors, filters):
  #filters: a tuple of wavelength and orientation  
  sigma = 0.5*filters[0]; #get wavelength
  K = 3;
  filtersize=2*math.ceil(2*K*sigma)+1
  gau_img = cv2.GaussianBlur(imageGarbors, (filtersize,filtersize), K*sigma)
  return gau_img


# call the methods
realimage = cv2.imread('kobi.png')
realimage = cv2.cvtColor(realimage, cv2.COLOR_BGR2GRAY)
img = rescale(realimage, 0.25, anti_aliasing=False)

imageGarbors=getImageGabor(img, (2.82,0))
imageGauss=getgaussianFiltrdImg(imageGarbors, (2.82,0))

Python只返回矩阵中的0和1,而MATLAB则继续返回值。我做错什么了?你知道吗


Tags: 代码imgdef矩阵cv2sigmafiltersmatlab