① python如何實現類似matlab的小波濾波
T=wpdec(y,5,'db40');
%信號y進行波包解層數5T波樹plot看
a10=wprcoef(T,[1,0]);
%a10節點[1,0]進行重構信號貌似沒層重構說吧能某層某節點進行重構節點編號波樹
%以下為濾波程序(主要調節參數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『)