导航:首页 > 编程语言 > python实现滤波器

python实现滤波器

发布时间:2023-02-08 03:09:28

python如何实现类似matlab的小波滤波

  1. T=wpdec(y,5,'db40');
    %信号y进行波包解层数5T波树plot看
    a10=wprcoef(T,[1,0]);
    %a10节点[1,0]进行重构信号貌似没层重构说吧能某层某节点进行重构节点编号波树

  2. %以下为滤波程序(主要调节参数c的大小)
    c=10;
    wn=0.1;
    fs=50000; %采样频率;
    b=fir1(c,wn/(fs/2),hamming(c+1));
    y1=filtfilt(b,1,y);%对y滤波。

② 如何用python实现图像的一维高斯滤波

如何用python实现图像的一维高斯滤波
建议你不要使用高斯滤波。
推荐你使用一维中值滤波
matlab的函数为
y = medfilt1(x,n);
x为数组,是你要处理原始波形,n是中值滤波器的参数(大于零的整数)。y是滤波以后的结果(是数组)
后面再
plot(y);
就能看到滤波以后的结果
经过medfilt1过滤以后,y里储存的是低频的波形,如果你需要高频波形,x-y就是高频波形
顺便再说一点,n是偶数的话,滤波效果比较好。
N越小,y里包含的高频成分就越多,y越大,y里包含的高频成分就越少。
记住,无论如何y里保存的都是整体的低频波。(如果你看不懂的话,滤一下,看y波形,你马上就懂了)

③ 数字图像处理Python实现图像灰度变换、直方图均衡、均值滤波

import CV2

import

import numpy as np

import random

使用的是pycharm

因为最近看了《银翼杀手2049》,里面Joi实在是太好看了所以原图像就用Joi了

要求是灰度图像,所以第一步先把图像转化成灰度图像

# 读入原始图像

img = CV2.imread('joi.jpg')

# 灰度化处理

gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)

CV2.imwrite('img.png', gray)

第一个任务是利用分段函数增强灰度对比,我自己随便写了个函数大致是这样的

def chng(a):

if a < 255/3:

b = a/2

elif a < 255/3*2:

b = (a-255/3)*2 + 255/6

else:

b = (a-255/3*2)/2 + 255/6 +255/3*2

return b

rows = img.shape[0]

cols = img.shape[1]

cover = .deep(gray)

for i in range(rows):

for j in range(cols):

cover[i][j] = chng(cover[i][j])

CV2.imwrite('cover.png', cover)

下一步是直方图均衡化

# histogram equalization

def hist_equal(img, z_max=255):

H, W = img.shape

# S is the total of pixels

S = H * W * 1.

out = img.()

sum_h = 0.

for i in range(1, 255):

ind = np.where(img == i)

sum_h += len(img[ind])

z_prime = z_max / S * sum_h

out[ind] = z_prime

out = out.astype(np.uint8)

return out

covereq = hist_equal(cover)

CV2.imwrite('covereq.png', covereq)

在实现滤波之前先添加高斯噪声和椒盐噪声(代码来源于网络)

不知道这个椒盐噪声的名字是谁起的感觉隔壁小孩都馋哭了

用到了random.gauss()

percentage是噪声占比

def GaussianNoise(src,means,sigma,percetage):

NoiseImg=src

NoiseNum=int(percetage*src.shape[0]*src.shape[1])

for i in range(NoiseNum):

randX=random.randint(0,src.shape[0]-1)

randY=random.randint(0,src.shape[1]-1)

NoiseImg[randX, randY]=NoiseImg[randX,randY]+random.gauss(means,sigma)

if NoiseImg[randX, randY]< 0:

NoiseImg[randX, randY]=0

elif NoiseImg[randX, randY]>255:

NoiseImg[randX, randY]=255

return NoiseImg

def PepperandSalt(src,percetage):

NoiseImg=src

NoiseNum=int(percetage*src.shape[0]*src.shape[1])

for i in range(NoiseNum):

randX=random.randint(0,src.shape[0]-1)

randY=random.randint(0,src.shape[1]-1)

if random.randint(0,1)<=0.5:

NoiseImg[randX,randY]=0

else:

NoiseImg[randX,randY]=255

return NoiseImg

covereqg = GaussianNoise(covereq, 2, 4, 0.8)

CV2.imwrite('covereqg.png', covereqg)

covereqps = PepperandSalt(covereq, 0.05)

CV2.imwrite('covereqps.png', covereqps)

下面开始均值滤波和中值滤波了

就以n x n为例,均值滤波就是用这n x n个像素点灰度值的平均值代替中心点,而中值就是中位数代替中心点,边界点周围补0;前两个函数的作用是算出这个点的灰度值,后两个是对整张图片进行

#均值滤波模板

def mean_filter(x, y, step, img):

sum_s = 0

for k in range(x-int(step/2), x+int(step/2)+1):

for m in range(y-int(step/2), y+int(step/2)+1):

if k-int(step/2) 0 or k+int(step/2)+1 > img.shape[0]

or m-int(step/2) 0 or m+int(step/2)+1 > img.shape[1]:

sum_s += 0

else:

sum_s += img[k][m] / (step*step)

return sum_s

#中值滤波模板

def median_filter(x, y, step, img):

sum_s=[]

for k in range(x-int(step/2), x+int(step/2)+1):

for m in range(y-int(step/2), y+int(step/2)+1):

if k-int(step/2) 0 or k+int(step/2)+1 > img.shape[0]

or m-int(step/2) 0 or m+int(step/2)+1 > img.shape[1]:

sum_s.append(0)

else:

sum_s.append(img[k][m])

sum_s.sort()

return sum_s[(int(step*step/2)+1)]

def median_filter_go(img, n):

img1 = .deep(img)

for i in range(img.shape[0]):

for j in range(img.shape[1]):

img1[i][j] = median_filter(i, j, n, img)

return img1

def mean_filter_go(img, n):

img1 = .deep(img)

for i in range(img.shape[0]):

for j in range(img.shape[1]):

img1[i][j] = mean_filter(i, j, n, img)

return img1

完整main代码如下:

if __name__ == "__main__":

# 读入原始图像

img = CV2.imread('joi.jpg')

# 灰度化处理

gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)

CV2.imwrite('img.png', gray)

rows = img.shape[0]

cols = img.shape[1]

cover = .deep(gray)

for i in range(rows):

for j in range(cols):

cover[i][j] = chng(cover[i][j])

CV2.imwrite('cover.png', cover)

covereq = hist_equal(cover)

CV2.imwrite('covereq.png', covereq)

covereqg = GaussianNoise(covereq, 2, 4, 0.8)

CV2.imwrite('covereqg.png', covereqg)

covereqps = PepperandSalt(covereq, 0.05)

CV2.imwrite('covereqps.png', covereqps)

meanimg3 = mean_filter_go(covereqps, 3)

CV2.imwrite('medimg3.png', meanimg3)

meanimg5 = mean_filter_go(covereqps, 5)

CV2.imwrite('meanimg5.png', meanimg5)

meanimg7 = mean_filter_go(covereqps, 7)

CV2.imwrite('meanimg7.png', meanimg7)

medimg3 = median_filter_go(covereqg, 3)

CV2.imwrite('medimg3.png', medimg3)

medimg5 = median_filter_go(covereqg, 5)

CV2.imwrite('medimg5.png', medimg5)

medimg7 = median_filter_go(covereqg, 7)

CV2.imwrite('medimg7.png', medimg7)

medimg4 = median_filter_go(covereqps, 7)

CV2.imwrite('medimg4.png', medimg4)

④ python读取txt文件转化为折线图后怎么实现滤波器

需要安装matplotlib库,可以用如下命令安装:

pip install matplotlib
1
txt文本数据如下所示(示例中的每一行内部用空格分开):

100 0.6692215
200 0.57682794
300 0.45037615
400 0.42214713
500 0.45073098
600 0.4728373
700 0.48083866
800 0.3751492
900 0.4249844
1000 0.36427215
1100 0.36209464
1200 0.40490758
1300 0.3774191
1400 0.34719718
1500 0.3648946
1600 0.261855
1700 0.4321903
1800 0.35071397
1900 0.279996
2000 0.30030474
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
适用于Python3的代码如下所示:

import matplotlib.pyplot as plt

input_txt = 'demo.txt'
x = []
y = []

f = open(input_txt)

for line in f:
line = line.strip('\n')
line = line.split(' ')

x.append(float(line[0]))
y.append(float(line[1]))

f.close

plt.plot(x, y, marker='o', label='lost plot')
plt.xticks(x[0:len(x):2], x[0:len(x):2], rotation=45)
plt.margins(0)
plt.xlabel("train step")
plt.ylabel("lost")
plt.title("matplotlip plot")
plt.tick_params(axis="both")

plt.show()

⑤ 如何用python实现图像的一维高斯滤波器

如何用python实现图像的一维高斯滤波器
现在把卷积模板中的值换一下,不是全1了,换成一组符合高斯分布的数值放在模板里面,比如这时中间的数值最大,往两边走越来越小,构造一个小的高斯包。实现的函数为cv2.GaussianBlur()。对于高斯模板,我们需要制定的是高斯核的高和宽(奇数),沿x与y方向的标准差(如果只给x,y=x,如果都给0,那么函数会自己计算)。高斯核可以有效的出去图像的高斯噪声。当然也可以自己构造高斯核,相关函数:cv2.GaussianKernel().
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(‘flower.jpg‘,0) #直接读为灰度图像
for i in range(2000): #添加点噪声
temp_x = np.random.randint(0,img.shape[0])
temp_y = np.random.randint(0,img.shape[1])
img[temp_x][temp_y] = 255
blur = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(1,2,1),plt.imshow(img,‘gray‘)#默认彩色,另一种彩色bgr
plt.subplot(1,2,2),plt.imshow(blur,‘gray‘)

阅读全文

与python实现滤波器相关的资料

热点内容
泰国剧罪孽父亲出海 浏览:7
叶天明柳韵txt 浏览:825
bch主流算法 浏览:767
免费在线国产片 浏览:200
苹果平板为什么连接服务器错误 浏览:89
穿越东晋成为王凝之 浏览:958
手机里的文件夹怎么拉出来 浏览:281
饥荒怎么输入ip进服务器 浏览:470
美国恐怖三极片 浏览:995
四川虚拟主机云服务器 浏览:780
母乳喂养真人版电影 浏览:396
免费看片链接 浏览:492
编程将中断类型号 浏览:597
数字编程学院 浏览:95
程序员不时尚 浏览:746
不付费看网站 浏览:230
《代人受过》训诫文 浏览:258
1n.d5h49m6. 浏览:687
linuxweb目录权限 浏览:945