首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SciPy.ndimage中应用Sobel滤波器

在SciPy.ndimage中应用Sobel滤波器
EN

Stack Overflow用户
提问于 2018-10-06 18:59:20
回答 1查看 1.3K关注 0票数 1

我正在尝试应用SciPy.ndimage中的Sobel运算符,并复制Wikipedia上显示的结果,但图像非常不同。

维基百科上的结果显示了更明显的边缘。

下面列出了我正在使用的代码。这段代码可以修改成与维基百科上的结果一致吗?维基百科的原始图片和结果图片都附在下面。

代码语言:javascript
复制
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from scipy.ndimage import filters


# Images from https://en.wikipedia.org/wiki/Sobel_operator
im_original = np.array(Image.open('Valve_original_(1).PNG').convert('L'))
im_sobel = np.array(Image.open('Valve_sobel_(3).PNG').convert('L'))

# Construct two ndarrays of same size as the input image
imx = np.zeros(im_original.shape)
imy = np.zeros(im_original.shape)

# Run the Sobel operator
# See https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.sobel.html
filters.sobel(im_original,1,imx,cval=0.0)  # axis 1 is x
filters.sobel(im_original,0,imy, cval=0.0) # axis 0 is y

magnitude = np.sqrt(imx**2+imy**2)

# Construct the plot

fig = plt.figure(figsize=(10,8))

ax1 = fig.add_subplot(221)
ax1.set_title('Original (Wikipedia)')
ax1.axis('off')
ax1.imshow(im_original, cmap='gray')

ax2 = fig.add_subplot(222)
ax2.set_title('Sobel operator - as shown on Wikipedia')
ax2.axis('off')
ax2.imshow(im_sobel, cmap='gray')

ax3 = fig.add_subplot(224)
ax3.set_title('Sobel operator - from scipy.ndimage')
ax3.axis('off')
ax3.imshow(magnitude, cmap='gray')

plt.savefig('sobel.png')
plt.show()

图片

原图: Valve_original_(1).PNG

维基百科上显示的结果: Valve_sobel_(3).PNG

EN

回答 1

Stack Overflow用户

发布于 2018-10-08 02:14:09

为了解决这个问题,我根据上面的评论发布了一个答案。

Wikipedia上显示的蒸汽机的索贝尔滤波图像已经以一些未指定的附加方式进行了处理,因此无法完全复制。最有可能的情况是,原始RGB图像首先转换为灰度,然后被钳制。

查看从SciPy.ndiamage获得的Sobel滤波图像的强度直方图,见下图,大多数像素集中在强度3.5附近。应用钳制值50产生的图像更接近于维基百科页面上显示的图像。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52678356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档