官术网_书友最值得收藏!

Chapter 2. Working with the NumPy Array As a First Step to SciPy

At the top level, SciPy is basically NumPy, since both the object creation and basic manipulation of these objects are performed by functions of the latter library. This assures much faster computations, since the memory handling is done internally in an optimal way. For instance, if an operation must be made on the elements of a big multidimensional array, a novice user might be tempted to go over columns and rows with as many for loops as necessary. Loops run much faster when they access each consecutive element in the same order in which they are stored in memory. We should not be bothered with considerations of this kind when coding. The NumPy/SciPy operations assure that this is the case. As an added advantage, the names of operations in NumPy/SciPy are intuitive and self explanatory. Code written in this fashion is extremely easy to understand and maintain, faster to correct or change in case of need.

Let's illustrate this point with an introductory example.

The scipy.misc module in the SciPy package contains a classical image called lena, used in the image processing community for testing and comparison purposes. This is a 512 x 512 pixel standard test image, which has been in use since 1973, and was originally cropped from the centerfold of the November 1972 issue of the Playboy magazine. It is a picture of Lena S?derberg, a Swedish model, shot by photographer Dwight Hooker. The image is probably the most widely used test image for all sorts of image processing algorithms (such as compression and noise reduction) and related scientific publications.

This image is stored as a two-dimensional array. Note that the number in the nth column and mth row of this array measures the grayscale value at the pixel position (n+1, m+1) of the image. In the following, we access this picture and store it in the img variable, by issuing the following commands:

>>> import scipy.misc
>>> img=scipy.misc.lena()
>>> import matplotlib.pyplot as plt 
>>> plt.gray() 
>>> plt.imshow(img) 

The image can be displayed by issuing the following command:

>>> plt.show() 
Working with the NumPy Array As a First Step to SciPy

We may take a peek at some of these values; say the 7 x 3 upper corner of the image (7 columns, 3 rows). Instead of issuing for loops, we could slice the corresponding portion of the image. The img[0:3,0:7] command gives us the following:

array([[162, 162, 162, 161, 162, 157, 163],
 [162, 162, 162, 161, 162, 157, 163],
 [162, 162, 162, 161, 162, 157, 163]])

We can use the same strategy to populate arrays or change their values. For instance, let's change all entries of the previous array to hold zeros on the second row between columns 2 to 6:

>>> img[1,1:6]=0
>>> print (img[0:3,0:7])

The output is shown as follows:

[[162 162 162 161 162 157 163]
 [162 0 0 0 0 0 163]
 [162 162 162 161 162 157 163]]
主站蜘蛛池模板: 西峡县| 枣庄市| 资中县| 云和县| 磐石市| 喀喇沁旗| 青铜峡市| 綦江县| 繁昌县| 务川| 桐柏县| 班玛县| 和龙市| 伊金霍洛旗| 牟定县| 琼海市| 独山县| 平原县| 三原县| 介休市| 霍邱县| 信阳市| 西昌市| 巍山| 南昌县| 深圳市| 施秉县| 什邡市| 宣恩县| 新兴县| 新津县| 全南县| 石狮市| 大石桥市| 登封市| 克山县| 应用必备| 合水县| 丰城市| 汪清县| 乌恰县|