python基础知识——scipy

来自:http://cs231n.github.io/python-numpy-tutorial/#scipy
更多:http://docs.scipy.org/doc/scipy/reference/index.html

图像操作:
    SciPy提供一些基本函数来处理对象。例如,它具有将图像从磁盘读取到numpy数组,将numpy
    数组作为图像写入磁盘以及调整图像大小的功能:
    from scipy.misc import imread, imsave, imresize

    # 将 JPEG 图像读取为 numpy 数组
    img = imread('assets/cat.jpg')
    print(img.dtype, img.shape)  # Prints "uint8 (400, 248, 3)"

    # 我们可以通过用不同的标量常量缩放每个颜色通道来对图像进行着色。
    # 图像的维度为 (400, 248, 3);
    # 用 [1, 0.95, 0.9] 的向量与它相乘;
    # numpy的广播属性使得红色通道的值不会改变,
    # 并且将绿色和蓝色的值分别乘以0.95和0.9
    img_tinted = img * [1, 0.95, 0.9]

    # 并将着色后的图像重塑大小为300×300像素的图像
    img_tinted = imresize(img_tinted, (300, 300))

    # 将图像输出回磁盘
    imsave('assets/cat_tinted.jpg', img_tinted)

MATLAB文件:
    函数 scipy.io.loadmat 和 scipy.io.savemat 提供读和取MATLAB文件
    更多:http://docs.scipy.org/doc/scipy/reference/io.html

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

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

    # 创建一个二维点的数组:
    # [[0 1]
    #  [1 0]
    #  [2 0]]
    x = np.array([[0, 1], [1, 0], [2, 0]])
    print(x)

    # 计算点集中每对点的欧式距离
    # d[i, j] 是点i和点j的欧式距离,
    # 结果为:
    # [[ 0.          1.41421356  2.23606798]
    #  [ 1.41421356  0.          1.        ]
    #  [ 2.23606798  1.          0.        ]]
    d = squareform(pdist(x, 'euclidean'))
    print(d)

    一个类似的函数 scipy.spatial.distance.cdist 计算两个点集中每对点的欧式距离

猜你喜欢

转载自blog.csdn.net/qq_32652679/article/details/80977415
今日推荐