我是python的新手,我尝试使用filter2D函数,但我收到此错误。
img_out = cv2.filter2D(img_in,-1,cv2.UMat(gb));类型错误: 找不到必需的参数"范围"(位置 2) ...这是什么意思?
import numpy as np
import cv2
def gabor_fn(sigma, theta, Lambda, psi, gamma):
sigma_x = sigma
sigma_y = float(sigma) / gamma
# Bounding box
nstds = 3 # Number of standard deviation sigma
xmax = max(abs(nstds * sigma_x * np.cos(theta)), abs(nstds * sigma_y * np.sin(theta)))
xmax = np.ceil(max(1, xmax))
ymax = max(abs(nstds * sigma_x * np.sin(theta)), abs(nstds * sigma_y * np.cos(theta)))
ymax = np.ceil(max(1, ymax))
xmin = -xmax
ymin = -ymax
(y, x) = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1))
x_theta = x * np.cos(theta) + y * np.sin(theta)
y_theta = -x * np.sin(theta) + y * np.cos(theta)
gb = np.exp(-0.5 * (np.power(x_theta, 2) / np.add(np.power(sigma_x, 2) , np.power(y_theta, 2)) / np.power(sigma_y, 2)* np.cos(2 * np.pi / Lambda * x_theta + psi[0])));
return gb;
Lambda = 8;
theta = 0;
psi = [0, np.pi / 2];
gamma = 0.5;
sigma = 1.0;
N = 8;
img_in = cv2.imread('1.jpg');
img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY);
for n in range(N):
gb = np.add(gabor_fn(sigma, theta, Lambda, psi, gamma) , 1j *
gabor_fn(sigma, theta, Lambda, psi, gamma));
img_out = cv2.filter2D(img_in,-1,cv2.UMat(gb));
theta = theta + (2 * np.pi) / N
img_out_disp = np.power(sum(np.power(abs(img_out), 2), 3), 0.5);
img_out_disp = np.divide(img_out_disp, max(img_out_disp));
你不能用OpenCV的filter2D()
卷积一个复杂的内核。filter2D
的文档指定内核必须是"单通道浮点矩阵"。
要使用OpenCV的函数,您必须分别卷积内核的实部和虚部,并按照自己的意愿组合结果。
如果你使用的是Python,Scipy是另一种选择,它确实直接支持复杂的卷积。scipy.signal.convolve2d()
的文档显示了一个具有复杂 Scharr 筛选器的示例。
为了将来参考,使用 OpenCV,您可以通过函数 getGaborFilter()
直接获取 Gabor 内核,而不是自己计算,但请注意它只返回真实部分。
这意味着您要使用的函数需要在第二个位置使用一个名为"ranges"的参数。
但是,我不确定这是一个filter2D
错误,因为这是函数的声明:
Python: cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) → dst¶
如您所见 - 这里不需要"范围"......可能还有另一个函数确实需要未提供的"范围"参数。