边缘检测未按预期工作
Edge detection not working as expected
我只是在 SciPy 和 Python 中玩转卷积和内核。我使用以下内核进行边缘检测,因为它在 this wikipedia article:
中列出
这是我使用的图像:
我得到的结果非常令人失望:
我用于卷积的代码:
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。
我认为问题在于您的图像使用 unsigned 整数。结果,例如,如果您从零中减去 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)
对我来说,这会生成以下图像:
这是我的 Sobel Operator, which works fine, here is my google colab you can run the code online
代码
from PIL import Image
#download png image which is 3D and disable the output
!wget https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png 2>/dev/null
im = Image.open("lena.png");
import numpy as np
arr = np.array(im);
arr.shape
hx = np.array ([[1,0,-1],[2,0,-2],[1,0,-1]])
hy = np.array ([[1,2,1],[0,0,0],[-1,-2,-1]])
import matplotlib.pyplot as plt
from scipy.signal import convolve
ax = arr[:, :, 0] #remove the color
Gx = convolve(ax, hx)
Gy = convolve(ax,hy)
G = np.sqrt(Gx*Gx+Gy*Gy)
plt.imshow(G, cmap='gray')
我只是在 SciPy 和 Python 中玩转卷积和内核。我使用以下内核进行边缘检测,因为它在 this wikipedia article:
中列出
这是我使用的图像:
我得到的结果非常令人失望:
我用于卷积的代码:
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。
我认为问题在于您的图像使用 unsigned 整数。结果,例如,如果您从零中减去 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)
对我来说,这会生成以下图像:
这是我的 Sobel Operator, which works fine, here is my google colab you can run the code online
代码from PIL import Image
#download png image which is 3D and disable the output
!wget https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png 2>/dev/null
im = Image.open("lena.png");
import numpy as np
arr = np.array(im);
arr.shape
hx = np.array ([[1,0,-1],[2,0,-2],[1,0,-1]])
hy = np.array ([[1,2,1],[0,0,0],[-1,-2,-1]])
import matplotlib.pyplot as plt
from scipy.signal import convolve
ax = arr[:, :, 0] #remove the color
Gx = convolve(ax, hx)
Gy = convolve(ax,hy)
G = np.sqrt(Gx*Gx+Gy*Gy)
plt.imshow(G, cmap='gray')