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

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]]
主站蜘蛛池模板: 拜城县| 平定县| 成安县| 金溪县| 承德市| 金门县| 平昌县| 东明县| 夏津县| 安溪县| 岳普湖县| 夏河县| 浦县| 洞口县| 江西省| 扎鲁特旗| 灵台县| 余干县| 密云县| 武宣县| 九龙坡区| 宿松县| 靖边县| 诏安县| 洛川县| 金华市| 施甸县| 临西县| 台东市| 抚松县| 绵阳市| 大荔县| 遵义市| 平谷区| 长治县| 东丽区| 松溪县| 宁河县| 阿勒泰市| 镇赉县| 武强县|