python-Opencv或numpy中的非sobel离散渐变

Rab*_*eih 1 python opencv numpy

我想根据以下掩码计算二维numpy图像数组的离散X和Y梯度数组:

import numpy as np
mx = np.array([[-1, 0, 1]])
my = np.array([[-1, 0, 1]]).T
Run Code Online (Sandbox Code Playgroud)

我看着OpenCV的文档中,并没有发现比Sobel算子这我不感兴趣的其他任何内容.什么是计算速度最快的方式说,使用纯numpy的或numpy的用opencv实现/ CV2梯度?

Rab*_*eih 5

知道了,只需使用cv2.filter2D如下:

import numpy as np
import cv2

mx = np.array([[-1, 0, 1]])
my = np.array([[-1, 0, 1]]).T
im = np.array([[1, 2, 3, 4, 5],
               [2, 3, 4, 5, 6],
               [3, 4, 5, 6, 7],
               [4, 5, 6, 7, 8],
               [5, 6, 7, 8, 9],]).astype(np.uint8)

gx = cv2.filter2D(im, cv2.CV_32F, mx)
gy = cv2.filter2D(im, cv2.CV_32F, my)

print gx.shape
print gx.dtype
print gx
Run Code Online (Sandbox Code Playgroud)

这使:

(5, 5)
float32
[[ 0.  2.  2.  2.  0.]
 [ 0.  2.  2.  2.  0.]
 [ 0.  2.  2.  2.  0.]
 [ 0.  2.  2.  2.  0.]
 [ 0.  2.  2.  2.  0.]]
Run Code Online (Sandbox Code Playgroud)

可在以下网址找到文档:http://docs.opencv.org/modules/imgproc/doc/filtering.html#filter2d