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

import numpy as np
mx = np.array([[-1, 0, 1]])
my = np.array([[-1, 0, 1]]).T

我查看过opencv文档,除了我不感兴趣的Sobel运算符外,没有找到其他东西。使用纯numpy或numpy与opencv / cv2来计算所述梯度的最快方法是什么?

最佳答案

知道了,只需使用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

这使:
(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.]]

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

关于python - python-opencv或numpy中的非sobel离散渐变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21233043/

10-10 13:52
查看更多