【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程(3)
作者:互联网
由官方授权的CS231n课程笔记翻译知乎专栏——智能单元,比较详细地翻译了课程笔记,我这里就是参考和总结。
【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程
SciPy
Numpy提供了高性能的多维数组,以及计算和操作数组的基本工具。SciPy基于Numpy,提供了大量的计算和操作数组的函数,这些函数对于不同类型的科学和工程计算非常有用。
熟悉SciPy的最好方法就是阅读文档。我们会强调对于本课程有用的部分。
图像操作
SciPy提供了一些操作图像的基本函数。比如,它提供了将图像从硬盘读入到数组的函数,也提供了将数组中数据写入的硬盘成为图像的函数。下面是一个简单的例子:
from scipy.misc import imread, imsave, imresize
# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')
print(img.dtype, img.shape) # Prints "uint8 (400, 248, 3)"
# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 3);
# we multiply it by the array [1, 0.95, 0.9] of shape (3,);
# numpy broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]
# Resize the tinted image to be 300 by 300 pixels.
img_tinted = imresize(img_tinted, (300, 300))
# Write the tinted image back to disk
imsave('assets/cat_tinted.jpg', img_tinted)
译者注:如果运行这段代码出现类似ImportError: cannot import name imread的报错,那么请利用pip进行Pillow的下载,可以解决问题。命令:pip install Pillow。
左边是原始图片,右边是变色和变形的图片。
————————————————————————————————————————————————————————
MATLAB文件
函数scipy.io.loadmat和scipy.io.savemat能够让你读和写MATLAB文件。具体请查看文档。
点之间的距离
SciPy定义了一些有用的函数,可以计算集合中点之间的距离。
函数scipy.spatial.distance.pdist能够计算集合中所有两点之间的距离:
import numpy as np
from scipy.spatial.distance import pdist, squareform
# Create the following array where each row is a point in 2D space:
# [[0 1]
# [1 0]
# [2 0]]
x = np.array([[0, 1], [1, 0], [2, 0]])
print(x)
# Compute the Euclidean distance between all rows of x.
# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
# and d is the following array:
# [[ 0. 1.41421356 2.23606798]
# [ 1.41421356 0. 1. ]
# [ 2.23606798 1. 0. ]]
d = squareform(pdist(x, 'euclidean'))
print(d)
具体细节请阅读文档。
函数scipy.spatial.distance.cdist可以计算不同集合中点的距离,具体请查看文档。
Matplotlib
Matplotlib是一个作图库。这里简要介绍matplotlib.pyplot模块,功能和MATLAB的作图功能类似。
绘图
matplotlib库中最重要的函数是Plot。该函数允许你做出2D图形,如下:
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
# Plot the points using matplotlib
plt.plot(x, y)
plt.show() # You must call plt.show() to make graphics appear.
运行上面代码会产生下面的作图:
————————————————————————————————————————————————————————
————————————————————————————————————————————————————————
只需要少量工作,就可以一次画不同的线,加上标签,坐标轴标志等。
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()
————————————————————————————————————————————————————————
————————————————————————————————————————————————————————
可以在文档中阅读更多关于plot的内容。
绘制多个图像
可以使用subplot函数来在一幅图中画不同的东西:
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
————————————————————————————————————————————————————————
————————————————————————————————————————————————————————
关于subplot的更多细节,可以阅读文档。
图像
你可以使用imshow函数来显示图像,如下所示:
import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
img = imread('assets/cat.jpg')
img_tinted = img * [1, 0.95, 0.9]
# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)
# Show the tinted image
plt.subplot(1, 2, 2)
# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()
————————————————————————————————————————————————————————
————————————————————————————————————————————————————————
标签:subplot,李飞飞,plt,img,Python,tinted,np,import,Numpy 来源: https://blog.csdn.net/TeFuirnever/article/details/89051702