Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Edge detection with the Sobel filter


The Sobel operator (http://en.wikipedia.org/wiki/Sobel_operator) can be used for edge detection in images. The edge detection is based on performing a discrete differentiation on the image intensity. Since an image is two-dimensional, the gradient also has two components, unless we limit ourselves to one dimension, of course. We will apply the Sobel filter to the picture of Lena Söderberg.

How to do it...

In this section, you will learn how to apply the Sobel filter to detect edges in the Lena image:

  1. To apply the Sobel filter in the x direction, set the axis parameter to 0:

    sobelx = scipy.ndimage.sobel(lena, axis=0, mode='constant')
  2. To apply the Sobel filter in the y direction, set the axis parameter to 1:

    sobely = scipy.ndimage.sobel(lena, axis=1, mode='constant')
  3. The default Sobel filter only requires the input array:

    default = scipy.ndimage.sobel(lena)

    Here are the original and resulting image plots, showing edge detection with the Sobel filter:

    The complete...