【问题标题】:TypeError: Required argument 'ranges' (pos 2) not foundTypeError:找不到所需的参数“范围”(位置 2)
【发布时间】: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 中。

标签: python opencv cv2


【解决方案1】:

您不能使用 OpenCV 的 filter2D() 与复杂内核进行卷积。 docs for filter2D 指定内核必须是“单通道浮点矩阵”。

要使用 OpenCV 的函数,您必须分别对内核的实部和虚部进行卷积,然后按照您的意愿组合结果。

如果您使用的是 Python,Scipy 是另一种选择,它直接支持复杂卷积。 docs for scipy.signal.convolve2d() 显示了一个带有复杂 Scharr 过滤器的示例。

为了将来参考,使用 OpenCV,您可以通过函数 getGaborFilter() 直接获取 Gabor 内核,而不是自己计算它,但请注意它只返回真实部分。

【讨论】:

  • 非常感谢!它与“getGaborFilter()”一起使用。
【解决方案2】:

这意味着您要使用的函数在第二个位置需要一个名为“范围”的参数。

但是,我不确定这是 filter2D 错误,因为这是函数的 declaration

Python: cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) → dst¶

如您所见 - 这里不需要“范围”... 可能还有另一个函数确实需要未提供的“范围”参数。

【讨论】:

  • 我正在尝试在 gabor 内核和输入图像之间进行卷积,这就是代码。 “gabor_fn”中不需要“范围”。你知道我该怎么解决这个问题吗?对于范围内的 n (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,gb); theta = theta + (2 * np.pi) / N;
  • TypeError: 找不到所需的参数“范围”(位置 2)
  • img_out = cv2.filter2D(img_in,-1,cv2.UMat(gb));在这条线上
猜你喜欢
  • 2020-08-13
  • 2019-05-10
  • 2019-07-10
  • 2021-04-09
  • 2017-05-26
  • 2015-10-16
  • 2019-09-03
  • 2011-09-22
  • 2015-05-03
相关资源
最近更新 更多