使用scipy应用Sobel滤波器

Fea*_*nor 11 python scipy edge-detection

我正在尝试在图像上应用Sobel滤镜以使用scipy来检测边缘.我在Windows 7旗舰版(64位)上使用Python 3.2(64位)和scipy 0.9.0.目前我的代码如下:

import scipy
from scipy import ndimage

im = scipy.misc.imread('bike.jpg')
processed = ndimage.sobel(im, 0)
scipy.misc.imsave('sobel.jpg', processed)
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么,但处理过的图像看起来并不像它应该做的那样.图像'bike.jpg'是灰度(模式'L'而非'RGB')图像,因此每个像素只有一个与之关联的值.

不幸的是我还没有在这里发布图片(没有足够的声誉),但我提供了以下链接:

原始图像(bike.jpg):http://s2.postimage.org/64q8w613j/bike.jpg

Scipy Filtered(sobel.jpg):http://s2.postimage.org/64qajpdlb/sobel.jpg

预期产出:http: //s1.postimage.org/5vexz7kdr/normal_sobel.jpg

我显然在某个地方出错了!有人可以告诉我在哪里.谢谢.

cgo*_*lke 24

1)使用更高的精度.2)您只计算沿零轴的导数的近似值.维基百科上解释了2D Sobel算子.试试这段代码:

import numpy
import scipy
from scipy import ndimage

im = scipy.misc.imread('bike.jpg')
im = im.astype('int32')
dx = ndimage.sobel(im, 0)  # horizontal derivative
dy = ndimage.sobel(im, 1)  # vertical derivative
mag = numpy.hypot(dx, dy)  # magnitude
mag *= 255.0 / numpy.max(mag)  # normalize (Q&D)
scipy.misc.imsave('sobel.jpg', mag)
Run Code Online (Sandbox Code Playgroud)


rec*_*ure 7

我无法评论cgohlke的答案,所以我重复了他的答案.参数0用于垂直导数,1用于水平导数(图像阵列的第一轴是y /垂直方向 - 行,第二轴是x /水平方向 - 列).只是想警告其他用户,因为我在错误的地方找错了1小时.

import numpy
import scipy
from scipy import ndimage

im = scipy.misc.imread('bike.jpg')
im = im.astype('int32')
dx = ndimage.sobel(im, 1)  # horizontal derivative
dy = ndimage.sobel(im, 0)  # vertical derivative
mag = numpy.hypot(dx, dy)  # magnitude
mag *= 255.0 / numpy.max(mag)  # normalize (Q&D)
scipy.misc.imsave('sobel.jpg', mag)
Run Code Online (Sandbox Code Playgroud)

  • 为了清楚起见,梯度与边缘正交,水平导数检测*垂直*边缘. (2认同)