边缘检测无法按预期工作

时间:2017-07-31 00:49:12

标签: python image-processing scipy convolution

我刚刚在SciPy和Python中使用卷积和内核。我使用以下内核进行边缘检测,因为它列在just with on=中:

kernel

这是我用的图像:
this wikipedia article

我得到的结果非常令人失望:
lion

我用于卷积的代码:

edge = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
results = sg.convolve(img, edge, mode='same')
results[results > 255] = 255
results[results < 0] = 0

...以及我用来阅读图片的代码:

img = np.array(Image.open('convolution_test/1.jpg'))
img = img[:, :, 0]

为什么我会得到这些糟糕的结果?

TIA。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您的图片使用无符号整数。因此,如果您从零中减去一个,则0-1 = 255得到uint8,因此您将获得白色,其中值实际上应为黑色。

然而,您可以通过使用有符号整数(最好使用更深入的内容)轻松克服此问题。例如:

from PIL import Image
import numpy as np
import scipy.signal as sg

img = np.array(Image.open('convolution_test/1.jpg'))
img = img[:, :, 0]
img = img.astype(np.int16)

edge = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
results = sg.convolve(img, edge, mode='same')
results[results > 255] = 255
results[results < 0] = 0

results = results.astype(np.uint8)

对我来说,这会产生以下图像: enter image description here

相关问题