ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

scipy库(2)

2021-02-08 17:04:57  阅读:298  来源: 互联网

标签:ndimage plt signal face scipy 图像 import


scipy库

信号处理模块signal

  • signal模块包含大量滤波函数、B样条插值算法等
  • 下面代码演示了一维信号的卷积运算
  • 一维卷积是什么
>>> import numpy as np
>>> x=np.array([1,2,3])
>>> h=np.array([4,5,6])
>>> import scipy.signal
>>> scipy.signal.convolve(x,h) #一维卷积运算
array([ 4, 13, 28, 27, 18])
  • 深刻认识卷积
  • 在scipy 1.0.0版本上已经移除了lena图的数据了,所以需要重新读取文件
  • 图像读取
  • 下面代码演示二维图像卷积运算
import numpy as np
from scipy import signal,misc
import matplotlib.pyplot as plt #用于显示图片
import matplotlib.image as mpimg #用于读取图片

image=misc.ascent() #二维数组图像,ascent图像
w=np.zeros((50,50)) #全0二维数组,卷积核
w[0][0]=1 #修改参数,调整滤波器
w[49][25]=1.0 #可以根据需要调整
image_new=signal.fftconvolve(image,w) #使用FFT算法进行卷积

plt.figure()
plt.imshow(image_new) #显示滤波后的图像
plt.gray()
plt.title('Filtered image')
plt.show()

在这里插入图片描述

  • 下面的代码对图像进行模糊处理
import numpy as np
from scipy import signal,misc
import matplotlib.pyplot as plt #用于显示图片
import matplotlib.image as mpimg #用于读取图片

image=misc.ascent() #二维数组图像,ascent图像
w=signal.gaussian(50,10.0)
image_new=signal.sepfir2d(image,w,w)

plt.figure()
plt.imshow(image_new) #显示滤波后的图像
plt.gray()
plt.title('Filtered image')
plt.show()

在这里插入图片描述

  • 中值滤波是数字信号处理、数字图像处理中常用的预处理技术,特点是将信号中每个值特护安慰其领域内的中值,即领域内所有值排序后中间位置上的值
>>> import random
>>> import numpy as np
>>> import scipy.signal as signal
>>> x=np.arange(0,100,10)
>>> random.shuffle(x) #打乱顺序
>>> x
array([60, 50, 20,  0, 40, 80, 90, 30, 10, 70])
>>> signal.medfilt(x,3) #中值滤波
array([50., 50., 20., 20., 40., 80., 80., 30., 30., 10.])
  • signal的medfilt()方法传入两个参数,第一个参数是要作中值滤波的信号,第二个参数是邻域的大小(奇数)。如邻域为3即是每个点自己和左右各一个点成为一个邻域。在每个位置的邻域中选取中位数替换这个位置的数,也就是该函数的返回值数组。如果邻域中出现没有元素的位置,那么以0补齐。

图像处理模块ndimage

  • 模块ndimage提供了大量用于N维图像处理的方法,更多用法可以参考官方文档

图像滤波

>>> from scipy import misc
>>> from scipy import ndimage
>>> import matplotlib.pyplot as plt
>>> face=misc.face()
>>> plt.figure() #创建图形
<Figure size 640x480 with 0 Axes>
>>> plt.imshow(face) #绘制测试图像
<matplotlib.image.AxesImage object at 0x0000008DC513AF28>
>>> plt.show() #原始图像
>>> blurred_face=ndimage.gaussian_filter(face,sigma=7)
>>> plt.imshow(blurred_face) #高斯滤波
<matplotlib.image.AxesImage object at 0x0000008DC56AECC0>
>>> plt.show()
>>> blurred_face1=ndimage.gaussian_filter(face,sigma=1) #以下3行为边缘锐化
>>> blurred_face3=ndimage.gaussian_filter(face,sigma=3)
>>> sharp_face=blurred_face3+6*(blurred_face3-blurred_face1)
>>> plt.imshow(sharp_face)
<matplotlib.image.AxesImage object at 0x0000008DB496CBE0>
>>> plt.show()
>>> median_face=ndimage.median_filter(face,7) #中值滤波
>>> 
>>> plt.imshow(median_face)
<matplotlib.image.AxesImage object at 0x0000008DC83EDB70>
>>> plt.show()

原始图像
原始图像
高斯滤波图像
高斯滤波图像
边缘锐化图像
边缘锐化图像
中值滤波图像
中值滤波图像

图像测量

>>> ndimage.measurements.maximum(face) #最大值
255
>>> ndimage.measurements.maximum_position(face) #最大值位置
(242, 560, 2)
>>> ndimage.measurements.mean(face) #平均值
110.16274388631184
>>> ndimage.measurements.median(face) #中值
109.0
>>> ndimage.measurements.sum(face)
259906521
>>> ndimage.measurements.variance(face) #方差
3307.17544034096
>>> ndimage.measurements.standard_deviation(face) #标准差
57.508046744268405
>>> ndimage.measurements.histogram(face,0,255,256) #直方图
array([ 3473,   613,   748,   993,  1651,  2468,  4198,  6781,  9301,
       11777, 11084, 10206, 10011, 10491, 11335, 11698, 11788, 11448,
       11383, 10860, 10563,  9743,  9104,  8692,  7939,  7940,  8069,
        8219,  8251,  8270,  8066,  7901,  7913,  7855,  7974,  8021,
        8221,  8374,  8316,  8217,  8338,  8300,  8440,  8540,  8936,
        9005,  9226,  9497,  9406,  9802, 10126, 10137, 10167, 10208,
       10246, 10093, 10508, 10457, 10465, 10743, 10773, 11127, 11355,
       11534, 11320, 11650, 11945, 12006, 12397, 12379, 12515, 12555,
       12872, 12890, 12985, 12954, 12930, 13172, 13270, 13374, 13404,
       13550, 13414, 13428, 13370, 13488, 13706, 13875, 13756, 13947,
       13838, 14063, 14321, 14395, 14274, 14291, 14411, 14727, 14612,
       14588, 14443, 14612, 14499, 14375, 14574, 14450, 14461, 14665,
       14674, 14620, 14569, 14488, 14708, 14898, 14600, 14573, 14482,
       14632, 14178, 14190, 14347, 14201, 14193, 14049, 13809, 13720,
       13711, 13716, 13557, 13614, 13393, 13126, 13205, 12860, 12995,
       12818, 12797, 12697, 12900, 12832, 12799, 12737, 12670, 12448,
       12388, 12099, 12131, 12087, 11859, 11676, 11863, 11837, 11747,
       11593, 11574, 11579, 11566, 11457, 11391, 11372, 11567, 11262,
       11226, 10994, 10903, 10812, 10576, 10699, 10501, 10425, 10368,
       10081,  9969,  9871,  9952,  9815,  9575,  9523,  9268,  9219,
        9070,  8997,  8826,  8761,  8467,  8351,  8069,  8141,  7992,
        7822,  7672,  7763,  7383,  7293,  7177,  6988,  6848,  6782,
        6623,  6447,  6344,  6080,  6290,  6231,  6061,  6085,  6017,
        5845,  5897,  5789,  5568,  5416,  5303,  5139,  4786,  4753,
        4519,  4448,  4087,  4034,  3977,  3764,  3467,  3191,  3130,
        3000,  2631,  2591,  2406,  2231,  2070,  1995,  1751,  1520,
        1369,  1267,  1121,   981,   920,   889,   803,   764,   747,
         720,   648,   568,   539,   539,   405,   390,   349,   318,
         282,   253,   181,   589], dtype=int64)

标签:ndimage,plt,signal,face,scipy,图像,import
来源: https://blog.csdn.net/xuezhe_____/article/details/113757063

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有