Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 边缘检测未按预期工作_Python_Image Processing_Scipy_Convolution - Fatal编程技术网

Python 边缘检测未按预期工作

Python 边缘检测未按预期工作,python,image-processing,scipy,convolution,Python,Image Processing,Scipy,Convolution,我只是在用SciPy和Python处理卷积和内核。我使用以下内核进行边缘检测,因为它列在中: 这是我使用的图像: 我得到的结果非常令人失望: 我用于卷积的代码: 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

我只是在用SciPy和Python处理卷积和内核。我使用以下内核进行边缘检测,因为它列在中:



这是我使用的图像:


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

我用于卷积的代码:

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
为什么我会得到这些糟糕的结果


TIA。

我认为问题在于,您的图像使用无符号整数。例如,如果从0中减去1,则对于
uint8
,将得到
0-1=255
,因此在实际值应为黑色的地方,将得到白色

但是,您可以通过使用有符号整数(最好具有更高的深度)轻松克服此问题。例如:

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)
从PIL导入图像
将numpy作为np导入
将scipy.signal作为sg导入
img=np.array(Image.open('卷积测试/1.jpg'))
img=img[:,:,0]
img=img.astype(np.int16)
edge=np.数组([[-1,-1,-1],-1,8,-1],-1,-1])
结果=sg.卷积(img,edge,mode='same')
结果[结果>255]=255
结果[结果<0]=0
结果=结果.astype(np.uint8)
对我来说,这会生成以下图像:

img的类型是什么。因为如果它是非齐次的,这可能会导致下溢。如果您将矩阵作为无符号整数加载,那么结果可能会环绕,使得负数实际上是白色值。但是
scipy.convolve
仅适用于1d数组?@WillemVanOnsem非常感谢!我检查了
img
数组的数据类型,你是对的。。。它是
uint8
。我把它改成了
int32
,瞧!如果你写下你的评论作为回答,我会接受@WillemVanOnsem
scipy.signal.convolve
也适用于矩阵。我查过了。谢谢。威廉:p
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)