创建我的自定义内核垫

时间:2017-06-03 10:05:32

标签: python opencv

如何在OpenCV Python中创建自己的内核?

我试图在下面创建一个sobel内核,但是我收到错误:

  

AttributeError:' module'对象没有属性' mat'

def sobel(src):

    kernelX = [
        [ -1, 0, 1 ],
        [ -2, 0, 2 ],
        [ -1, 0, 1 ]
    ]

    kernelY = [
        [ -1, -2, -1 ],
        [  0,  0,  0 ],
        [  1,  2,  1 ]
    ]

    kernelXMat = cv2.mat(3, 3, cv2.cv2.CV_32FC1, kernelX)
    kernelYMat = cv2.mat(3, 3, cv2.CV_32FC1, kernelY)

    horizGradient = cv2.filter2D(src, -1, kernelXMat)
    vertGradient = cv2.filter2D(src, -1, kernelYMat)

    return [horizGradient, vertGradient]

1 个答案:

答案 0 :(得分:3)

OpenCV Mat的Python等价物是numpy.array。可以在python中按如下方式创建指定的内核。

import numpy as np

kernelXMat = np.array(kernelX, dtype=float);
kernelYMat = np.array(kernelY, dtype=float);