我是python的新手,我尝试使用filter2D函数,但出现此错误。

img_out = cv2.filter2D(img_in,-1,cv2.UMat(gb));
TypeError:找不到必需的参数“ranges”(位置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()来处理复杂的内核。 docs for filter2D 指定内核必须是“单通道浮点矩阵”。

要使用OpenCV的功能,您必须分别对内核的实部和虚部进行卷积,并按所需方式组合结果。

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

为了将来参考,使用OpenCV可以通过函数 getGaborFilter() 直接获得Gabor内核,而不用自己计算它,尽管请注意,它仅返回实部。

关于python - TypeError:找不到必需的参数 'ranges'(pos 2),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54386980/

10-12 00:23
查看更多