SciPy&Matplotlib

一、SciPy
SciPy提供了一些处理图像的基本函数。例如,它具有将映像从磁盘读入numpy数组、将numpy数组作为映像写入磁盘以及调整映像大小的功能。下面是一个演示这些函数的简单示例:

import numpy as np
from scipy.spatial.distance import pdist, squareform
from scipy.misc import imread, imsave, imresize

img=imread('fish.jpg')
print(img.shape,img.dtype)
print(img)#这里输出的是一个代表图像的数组

img_tinted = img * [1, 0.95, 0.9]#调整图像的颜色
img_tinted = imresize(img_tinted, (300, 300))
imsave('fish_tinted.jpg', img_tinted)

SciPy定义了一些用于计算点集之间距离的有用函数。
函数scipy.spatial.distance.pdist计算给定集合中所有点对之间的距离:

import numpy as np
from scipy.spatial.distance import pdist, squareform

x = np.array([[0, 1], [1, 0], [2, 0]])
print(x)
d1 = squareform(pdist(x, 'euclidean'))#这个输出的是最正常的n*n的矩阵形式的距离
print(d1)
#[[0.         1.41421356 2.23606798]
#[1.41421356 0.         1.        ]
#[2.23606798 1.         0.        ]]
d2 = pdist(x, 'euclidean')#这个输出的是三个距离组成的向量
print(d2)
#[1.41421356 2.23606798 1.        ]

二、Matplotlib
Matplotlib是一个绘图库。本节简要介绍 matplotlib.pyplot 模块。
https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.legend

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,2*np.pi,50)
y_sin=np.sin(x)
y_cos=np.cos(x)
plt.plot(x,y_sin)
plt.plot(x,y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.legend(['sine','cosine'])
plt.title('Sine and Cosine')
plt.show()

可以使用subplot函数在同一个图中绘制不同的东西。

import numpy as np
import matplotlib.pyplot as plt
#生成数据
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
#子图高为1,宽为2,下面画的是第一个位置的子图
plt.subplot(1, 2, 1)
plt.plot(x, y_sin)
plt.title('Sine')
#下面画的是第二个位置的子图
plt.subplot(1, 2, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()

子图怎么画可以使用 imshow 函数来显示一张图片

img = imread('fish.jpg')
img_tinted = img * [1, 0.95, 0.8]

# 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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值