为什么scipy.ndimage中的generic_filter与opencv.Sobel得到不同的结果

时间:2019-01-10 17:12:20

标签: python opencv scipy

这是测试代码

    import numpy as np
    import cv2
    from scipy.ndimage import generic_filter
    def sobel_x_filter(P):
        return (P[2] + 2 * P[6] + P[7]) - (P[0] + 2 * P[3] + P[6])
    matrix = np.ones((100, 100))
    matrix[1, 2] = 2
    cv2_result = cv2.Sobel(np.float32(matrix), cv2.CV_32F, 1, 0)
    generic_filter_result = generic_filter(matrix, sobel_x_filter, (3, 3))

cv2_result[1, :][ 0., 2., 0., -2., 0., ..., 0.]

但是generic_filter_result[1, :][0., 0., 0., -2., 0., 0., 0., ..., 0.]

我很困惑为什么结果不同,我试图将函数mode中的generic_filter参数更改为mirrorwrap,但仍然产生相同的结果结果与之前相同,与cv2.Sobel

的结果不一致

1 个答案:

答案 0 :(得分:0)

我认为您的函数应显示为:

def sobel_x_filter(P):
   return (P[2] + 2 * P[5] + P[8]) - (P[0] + 2 * P[3] + P[6])