以下是使用Sobel operator:
进行边缘检测的代码from PIL import Image
import numpy as np
from scipy import misc
a = np.array([1, 2, 1])
b = np.array([1, 0, -1])
Gy = np.outer(a, b)
Gx = np.rot90(Gy, k=3)
def apply(X):
a = (X * Gx)
b = (X * Gy)
return np.abs(a.sum()) + np.abs(b.sum())
data = np.uint8(misc.lena())
data2 = np.copy(data)
center = offset = 1
for i in range(offset, data.shape[0]-offset):
for j in range(offset, data.shape[1]-offset):
X = data[i-offset:i+offset+1, j-offset:j+offset+1]
data[i, j] = apply(X)
image = Image.fromarray(data)
image.show()
image = Image.fromarray(data2)
image.show()
结果是:
而不是:
对于它的价值,我相当肯定我的for循环和图像内核的一般概念是正确的。例如,我能够生成这个自定义过滤器(高斯减去中心):
我的Sobel滤镜有什么问题?
答案 0 :(得分:0)