Scipy

scipy

scipy是基于numpy的

import numpy as np
import matplotlib.pyplot as plt

数值积分,求圆周率

# 3.1415926-927
# 通过积分的方式来画圆
# x^2+y^2=r^2

# f(x) = (r^2 - x^2)^0.5

# 假设圆的半径我们知道,r=1
X = np.linspace(-1,1, 1000)

f = (1 - X ** 2) ** 0.5

# 计算三角形的,以0点计算,计算对边
# 我们画圆形需要x和y
plt.figure(figsize=(4, 4))
# 有了三角形,圆形的斜边顶点就找到了,把这2000个顶点给连接起来就是一个圆
plt.plot(X,f)
plt.plot(X,-f)
[<matplotlib.lines.Line2D at 0x933b2b0>]

这里写图片描述

  • 使用scipy.integrate进行积分,调用quad()方法,求圆周率
# integrate
import scipy as sp
import scipy.integrate as integrate

f = lambda x : (1 - x ** 2) ** 0.5

# quad 中第一个参数是一个func y轴的解, a和b代表x轴的范围,分别填入-1,1
pi,error = integrate.quad(f, -1, 1)

# (1.5707963267948986, 1.0002356720661965e-09)
# 第一个数是圆周率,第二个数是误差
print('通过积分求解的圆周率:%s, 误差为:%s' % (pi * 2, error * 2))
通过积分求解的圆周率:3.141592653589797, 误差为:2.000471344132393e-09

scipy中的文件输入和输出

# io 
import scipy.io as io
from scipy.fftpack import fft2, ifft2
moon = plt.imread('./moonlanding.png')
moon_fft2 = fft2(moon)
cond = np.abs(moon_fft2) > 1e3
moon_fft2[cond] = 0
moon_ifft = ifft2(moon_fft2)
res = np.real(moon_ifft)
plt.figure(figsize=(12, 9))
plt.imshow(res, cmap='gray')
<matplotlib.image.AxesImage at 0xaf6f080>

这里写图片描述

# mat 这是一种数据格式
# math matrix
# matlib通用字典
# 文件储存都是以2进制
io.savemat('./moon_clear.mat', {'moon': res})
#load
io.loadmat('./moon_clear.mat')['moon']
  • 输出

    array([[-0.2826645 , 0.08010893, -0.21303105, …, -0.03895291,
    -0.18217105, -0.17954478],
    [-0.00702763, 0.06715892, -0.09148968, …, -0.1856927 ,
    -0.19348538, -0.15636131],
    [-0.03724453, -0.12469123, -0.10263439, …, -0.18584874,
    -0.19098356, 0.08034539],
    …,
    [ 0.07167082, -0.04644684, -0.07860907, …, 0.09754767,
    0.02351329, 0.18534262],
    [ 0.00943622, -0.06645724, -0.02696014, …, 0.05524265,
    -0.00268553, 0.19484597],
    [-0.0551862 , 0.03641299, -0.06190041, …, 0.03356535,
    0.02719305, -0.02497441]], dtype=float32)

plt.imshow(io.loadmat('./moon_clear.mat')['moon'])
<matplotlib.image.AxesImage at 0xb56feb8>

这里写图片描述

  • 读写图片使用scipy中misc.imread()/imsave()
# misc 表示一个库非常的杂乱
import scipy.misc as misc
plt.imread('./moonlanding.png')
  • 输出

    array([[0.04705882, 0. , 0.23921569, …, 0. , 0.00392157,
    0.53333336],
    [0. , 0. , 0.6784314 , …, 0.10196079, 0.2901961 ,
    0. ],
    [0.72156864, 0.10980392, 0.6039216 , …, 0. , 0.21568628,
    1. ],
    …,
    [0.00392157, 0. , 1. , …, 1. , 1. ,
    0.95686275],
    [0. , 0. , 0.15686275, …, 0. , 0. ,
    0.3529412 ],
    [1. , 0.52156866, 0.04705882, …, 0. , 0. ,
    1. ]], dtype=float32)

# 读出来以后做了一个变换把PNG变成了JPG
moon = misc.imread('./moonlanding.png')
plt.figure(figsize=(12, 9))
plt.imshow(moon, cmap='gray')
<matplotlib.image.AxesImage at 0xb0a0ef0>

这里写图片描述

imrotate、imresize、imfilter 旋转 改变大小 滤波

# 旋转
plt.imshow(misc.imrotate(moon,60), cmap='gray')
<matplotlib.image.AxesImage at 0xb0e1128>

这里写图片描述

# imresize int 0.5 = 50+%
plt.imshow(misc.imresize(moon,50), cmap='gray')
<matplotlib.image.AxesImage at 0xce54668>

这里写图片描述

plt.imshow(misc.imresize(moon,0.5), cmap='gray')
<matplotlib.image.AxesImage at 0xcead908>

这里写图片描述

# tuple (100, 200) 是压缩成Y为100, X为200的图片
plt.imshow(misc.imresize(moon,(100,200)), cmap='gray')
<matplotlib.image.AxesImage at 0xcf08c18>

这里写图片描述

imfilter图片过滤

The filter that has to be applied. Legal values are:
‘blur’, ‘contour’, ‘detail’, ‘edge_enhance’, ‘edge_enhance_more’,
‘emboss’, ‘find_edges’, ‘smooth’, ‘smooth_more’, ‘sharpen’.

必须应用的筛选器。法律价值是:
“模糊”、“轮廓”、“细节”、“EdgEl增强”、“EdgEI增强”
“浮雕”、“发现边缘”、“平滑”、“平滑多”、“锐化”。

# imfilter
# 这是一张模糊的图片
cat = plt.imread('./cat.jpg')
plt.imshow(misc.imfilter(cat, 'blur'))
<matplotlib.image.AxesImage at 0xcf889e8>

这里写图片描述

# 轮廓图
plt.imshow(misc.imfilter(cat, 'contour'))
<matplotlib.image.AxesImage at 0xcfd37f0>

这里写图片描述

# 细节
# detail
plt.imshow(misc.imfilter(cat, 'detail'))
<matplotlib.image.AxesImage at 0xafbc128>

这里写图片描述

# edge_enhance 边缘增强
plt.figure(figsize=(12, 9))
plt.imshow(misc.imfilter(cat, 'edge_enhance'))
<matplotlib.image.AxesImage at 0xe257cf8>

这里写图片描述

# edge_enhance_more 边缘超强
plt.imshow(misc.imfilter(cat, 'edge_enhance_more'))
<matplotlib.image.AxesImage at 0xe4d7eb8>

这里写图片描述

# emboss 浮雕
plt.imshow(misc.imfilter(cat, 'emboss'))
<matplotlib.image.AxesImage at 0xe2ce860>

这里写图片描述

# find_edges
plt.imshow(misc.imfilter(cat, 'find_edges'))
<matplotlib.image.AxesImage at 0xe323f98>

这里写图片描述

# smooth 平滑
plt.imshow(misc.imfilter(cat, 'smooth'))
<matplotlib.image.AxesImage at 0xe38a748>

这里写图片描述

# smooth_more 超平滑
plt.imshow(misc.imfilter(cat, 'smooth_more'))
<matplotlib.image.AxesImage at 0xe3ed080>

这里写图片描述

# sharpen 锐化
plt.imshow(misc.imfilter(cat, 'sharpen'))
<matplotlib.image.AxesImage at 0xe44d048>

这里写图片描述

图片处理

使用scipy.misc.face(gray=True)获取图片,

使用ndimage移动坐标、旋转图片、切割图片、缩放图片导包,读取图片显示图片

import scipy.ndimage as ndimage
# 在ndimage中,scipy给我们提供一张图片,misc.face()
face = misc.face(gray=True)
plt.imshow(face,cmap='gray')
<matplotlib.image.AxesImage at 0xf73cb38>

这里写图片描述

shift移动坐标

# 第一个参数input 代表将要被处理的图片
# 第二个参数shift代表你要移动多少 
plt.imshow(ndimage.shift(face,[100, 200]),cmap='gray')
<matplotlib.image.AxesImage at 0xf8474e0>

这里写图片描述

plt.imshow(ndimage.shift(face,[-100, -200]),cmap='gray')
<matplotlib.image.AxesImage at 0xf8a4978>

这里写图片描述

'''
mode : str, optional
    Points outside the boundaries of the input are filled according
    to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
    Default is 'constant'.

一个可选的模式:
外面的点是根据输入的边界填充
在给定的模式(“常数”,“最近”,“反映”、“镜”或“包”)。
默认是“常数”。
'''
"\nmode : str, optional\n    Points outside the boundaries of the input are filled according\n    to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').\n    Default is 'constant'.\n    \n一个可选的模式:\n外面的点是根据输入的边界填充\n在给定的模式(“常数”,“最近”,“反映”、“镜”或“包”)。\n默认是“常数”。\n"
# constant', 'nearest', 'reflect', 'mirror' or 'wrap'
# 镜像 mirror
plt.imshow(ndimage.shift(face,[100,200],mode='mirror'),cmap='gray')
<matplotlib.image.AxesImage at 0xfb866d8>

这里写图片描述

#mirror是镜像的意思,redlect是反射的意思
plt.imshow(ndimage.shift(face,[100,200],mode='reflect'),cmap='gray')
<matplotlib.image.AxesImage at 0xfbe4b70>

这里写图片描述

#rotate旋转图片
picture2 = ndimage.rotate(face,60)
plt.imshow(picture2,cmap = 'gray')
<matplotlib.image.AxesImage at 0x1a25fc1438>

这里写图片描述

zoom#缩放图片

# resize imresize
plt.imshow(ndimage.zoom(face, 0.5),cmap='gray')
<matplotlib.image.AxesImage at 0xfc45e48>

这里写图片描述

# 序列中的值只能是浮点型的,分别代表对y轴压缩多少,对x轴压缩多少
plt.imshow(ndimage.zoom(face, [0.3, 0.8]),cmap='gray')
<matplotlib.image.AxesImage at 0xfcae320>

这里写图片描述

切割图片

plt.imshow(face,cmap='gray')
<matplotlib.image.AxesImage at 0x102cecc0>

这里写图片描述

plt.imshow(face[:500,512:],cmap='gray')
<matplotlib.image.AxesImage at 0x11305278>

这里写图片描述

如果是彩色的图片怎么办了

face1 = misc.face()
face1.shape
  • 输出

    (768, 1024, 3)

plt.imshow(face1)
<matplotlib.image.AxesImage at 0x1133e940>

这里写图片描述

# plt.imshow(ndimage.shift(face,[100,200]))彩色图片是三维的,少写一维会报错
plt.imshow(ndimage.shift(face1,[100,200,1]))
# rgb中分别代表rgb向右移动,移出的数字补到前面
# rgb(100, 200, 300) -> (300, 100, 200)
<matplotlib.image.AxesImage at 0x116810b8>

这里写图片描述

plt.imshow(ndimage.shift(face1,[100,200,4], mode='mirror'))
# rgb(100, 200, 300)->(100, 100, 200)->(300, 100, 200)
<matplotlib.image.AxesImage at 0x12c04278>

这里写图片描述

# zoom 进行压缩
# 图片压缩一般不压缩颜色
plt.imshow(ndimage.zoom(face1, [0.5, 0.5, 1]),cmap='gray')
<matplotlib.image.AxesImage at 0x14129860>

这里写图片描述

plt.imshow(ndimage.zoom(face1, [0.5, 0.5, 1]),cmap='gray')

图片进行过滤

添加噪声,对噪声图片使用ndimage中的高斯滤波、中值滤波、signal中维纳滤波进行处理
使图片变清楚

face_blur = face + np.random.randn(face_blur.shape[0], face_blur.shape[1]) * face.std()
plt.imshow(face_blur, cmap='gray')
<matplotlib.image.AxesImage at 0x1418abe0>

这里写图片描述

  • 使用了misc.face()的灰色照片添加噪声

  • 高斯滤波sigma:高斯和的标准偏差

# sigma频率
# 频率取值 
# plt.subplot(1, 3, 1) subplot中的值代表 1代表行 显示3张图片 当前是编号1
plt.figure(figsize=(12,9))
axes = plt.subplot(131)
axes.imshow(ndimage.gaussian_filter(face_blur,sigma=1.5), cmap='gray')

axes = plt.subplot(132)
axes.imshow(face_blur, cmap='gray')

axes = plt.subplot(133)
axes.imshow(face, cmap='gray')

# 可以用来还原照片
<matplotlib.image.AxesImage at 0x152ae3c8>

这里写图片描述

  • 将登月图片进行高斯过滤
moon1 = plt.imread('./moonlanding.png')
moon2 = ndimage.gaussian_filter(moon1, sigma=2)
plt.figure(figsize=(8,6))
plt.imshow(moon2, cmap='gray')
<matplotlib.image.AxesImage at 0x11b23358>

这里写图片描述

  • 中值滤波参数size:给出在每个元素上从输入数组中取出的形状位置,定义过滤器功能的输入
plt.figure(figsize=(12,9))
axes = plt.subplot(131)
#中值滤波是一种非线性的过滤算法,把像素周围相邻的波值求出一个平均值
axes.imshow(ndimage.median_filter(face_blur,size=6), cmap='gray')

axes = plt.subplot(132)
axes.imshow(face_blur, cmap='gray')

axes = plt.subplot(133)
axes.imshow(face, cmap='gray')

# 聚 类 不同类的事物
<matplotlib.image.AxesImage at 0x13ad3d68>

这里写图片描述

  • signal维纳滤波mysize:滤镜尺寸的标量
import scipy.signal as signal

#一种基于最小均方误差准则、对平稳过程的最优估计器
face_wiener = signal.wiener(face_blur,mysize=5)
plt.figure(figsize=(12,6))

# 原图
axes1 = plt.subplot(131)
axes1.imshow(face,cmap = 'gray')

# 添加噪声图片
axes2 = plt.subplot(132)
axes2.imshow(face_blur,cmap = 'gray')


# 维纳滤波的结果
axes3 = plt.subplot(133)
axes3.imshow(face_wiener,cmap = 'gray')
<matplotlib.image.AxesImage at 0x1cfe8550>

这里写图片描述

猜你喜欢

转载自blog.csdn.net/beichen0518/article/details/80751004