【发布时间】:2021-08-31 08:53:05
【问题描述】:
我在 Python 中尝试了 5 种不同的 Sobel 运算符实现,其中一种是我自己实现的,结果根本不同。
我的问题类似于this one,但与其他实现仍有一些我不明白的区别。
Sobel算子的定义是否有一致的定义,它是否总是“图像梯度”的同义词?
即使 Sobel 内核的定义因源而异,根据 Wikipedia 是 [[1, 0, -1],[2, 0, -2],[1, 0, -1]],但根据其他来源是 [[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]]。
这是我尝试不同技术的代码:
from scipy import ndimage
import numpy as np
import cv2 as cv
from scipy import ndimage
from PIL import Image, ImageFilter
img = np.random.randint(0, 255, [10, 10]).astype(np.uint8)
def sobel_x(img) :
return ndimage.convolve(img, np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]]))
my_sobel = sobel_x(img)
_, numpy_sobel = np.gradient(img)
opencv_sobel = cv.Sobel(img, cv.CV_8UC1, 1, 0)
ndimage_sobel = ndimage.sobel(img, axis=0, mode="constant")
pil_sobel = np.array(Image.fromarray(img).filter(ImageFilter.Kernel((3, 3), (-1, 0, 1, -2, 0, 2, -1, 0, 1), 1, 0)))
print(my_sobel)
print(numpy_sobel)
print(opencv_sobel)
print(ndimage_sobel)
print(pil_sobel)
【问题讨论】:
-
你可以看看this 了解sobel是如何在opencv中实现的
标签: python opencv image-processing filtering derivative