本文介绍了使用 scipy 应用 Sobel 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Sobel 过滤器应用于图像以使用 scipy 检测边缘.我在 Windows 7 Ultimate(64 位)上使用 Python 3.2(64 位)和 scipy 0.9.0.目前我的代码如下:

I'm trying to apply the Sobel filter on an image to detect edges using scipy. I'm using Python 3.2 (64 bit) and scipy 0.9.0 on Windows 7 Ultimate (64 bit). Currently my code is as follows:

import scipy
from scipy import ndimage

im = scipy.misc.imread('bike.jpg')
processed = ndimage.sobel(im, 0)
scipy.misc.imsave('sobel.jpg', processed)

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

I don't know what I'm doing wrong, but the processed image does not look anything like what it should. The image, 'bike.jpg' is a greyscale (mode 'L' not 'RGB') image so each pixel has only one value associated with it.

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

Unfortunately I can't post the images here yet (don't have enough reputation) but I've provided links below:

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

Original Image (bike.jpg):http://s2.postimage.org/64q8w613j/bike.jpg

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

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

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

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

I'm obviously going wrong somewhere! Can someone please tell me where. Thanks.

推荐答案

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

1) Use a higher precision. 2) You are only calculating the approximation of the derivative along the zero axis. The 2D Sobel operator is explained on Wikipedia. Try this code:

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)

这篇关于使用 scipy 应用 Sobel 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:11
查看更多