Applying the Sobel filter using scipy

前端 未结 3 880
刺人心
刺人心 2021-02-04 11:53

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 i

3条回答
  •  星月不相逢
    2021-02-04 12:29

    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)
    

提交回复
热议问题