【发布时间】:2019-06-20 13:40:06
【问题描述】:
我是 python 新手,我尝试使用 filter2D 函数,但出现此错误。
img_out = cv2.filter2D(img_in,-1,cv2.UMat(gb)); TypeError: 未找到必需的参数“范围”(位置 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));
【问题讨论】:
-
你为什么选择
UMat? -
因为我在转换之前遇到了这个错误... TypeError: Expected cv::UMat for argument 'kernel'
-
你需要传递一个numpy数组作为内核。
-
我想在内核参数中传递一个 gabor 内核,因为我正在尝试实现 gabor 过滤器。我是否以错误的语法传递它? 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,gb);
-
什么是
gabor_fn?另外,请将代码添加到您的 OP 的编辑中,而不是在 cmets 中。