为什么它说我已经声明了内核

时间:2016-09-18 05:01:05

标签: python numpy

    import numpy as np
    from numpy.fft import fft2, ifft2
    import cv2
    from PIL import Image
    def wiener_filter(img,kernel,K = 10):
        kernel=([3,1],[2,1])
        dummy = np.copy(img)
        kernel = np.pad(kernel, [(0, dummy.shape[0] - kernel.shape[0]), (0, dummy.shape[1] - kernel.shape[1])], 'constant')
        # Fourier Transform
        dummy = fft2(dummy)
        kernel = fft2(kernel)
        kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
        dummy = dummy * kernel
        dummy = np.abs(ifft2(dummy))
        return np.uint8(dummy)

    img = cv2.imread("C:\\Users\\anup\\Desktop\\New folder\\leo.jpg")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #gray1 = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)
    kernel=([3,1],[2,1])
    fm = wiener_filter(img,kernel)
    cv2.imshow("Image", img)
    key = cv2.waitKey(0)

我正在尝试实施Wiener滤镜以对模糊图像执行反卷积。我的实现是这样的

执行此操作时出现错误

Traceback (most recent call last):
  File "C:\Python27\deblur.py", line 21, in <module>
    fm = wiener_filter(img,kernel)
  File "C:\Python27\deblur.py", line 8, in wiener_filter
    kernel = np.pad(kernel, [(0, dummy.shape[0] - kernel.shape[0]), (0, dummy.shape[1] - kernel.shape[1])], 'constant')
AttributeError: 'tuple' object has no attribute 'shape'

1 个答案:

答案 0 :(得分:2)

那里:

def wiener_filter(img,kernel,K = 10):
    kernel=([3,1],[2,1])

您定义了一个以kernel为参数的函数,并直接覆盖它。

然后您尝试使用kernel.shape

中显然不存在的([3,1],[2,1])

显然,shape是用于numpy ndarray个对象的方法。 所以这可能适合你:

def wiener_filter(img,kernel,K = 10):
    kernel=np.array([[3,1],[2,1]])

或者@Eric提到:

def wiener_filter(img,kernel,K = 10):
    kernel=np.array(kernel)