Applying the Sobel filter using scipy

前端 未结 3 875
刺人心
刺人心 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:35

    I couldn't comment on cgohlke's answer so I repeated his answer with a corrction. Parameter 0 is used for vertical derivative and 1 for horizontal derivative (first axis of an image array is y/vertical direction - rows, and second axis is x/horizontal direction - columns). Just wanted to warn other users, because I lost 1 hour searching for mistake in the wrong places.

    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)
    

提交回复
热议问题