一、函數(shù)說明
創(chuàng)新互聯(lián)是專業(yè)的陽曲網(wǎng)站建設(shè)公司,陽曲接單;提供成都做網(wǎng)站、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行陽曲網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
在使用python作圖時,應(yīng)用最廣的就是matplotlib包,但我們平時使用matplotlib時主要是畫一些簡單的圖表,很少有涉及分段函數(shù)。本次針對數(shù)值實驗中兩個較為復(fù)雜的函數(shù),使用其構(gòu)建分段函數(shù)圖像。
二、圖像代碼
2.11、函數(shù)公式:
y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)
2.12、代碼如下:
import numpy as np
import matplotlib.pyplot as plt
def sgn(x):
if x0:
return 1
elif x0:
return -1
else:
return 0
t=np.arange(0,1,0.01)
y=[]
for i in t:
y_1=4*np.sin(4*np.pi*i)-sgn(i-0.3)-sgn(0.72-i)
y.append(y_1)
plt.plot(t,y)
plt.xlabel("t")
plt.ylabel("y")
plt.title("Heavsine")
plt.show()
2.13、運行結(jié)果如下:
81036331d721706ae12808beb99b9574.png
2.21、函數(shù)公式:
479029.html
2.22、代碼如下:
import numpy as np
import matplotlib.pyplot as plt
def g(x):
if x0:
return x
else:
return 0
t=np.arange(0,1,0.01)
y=[]
for i in t:
y_1=g(i*(1-i))*np.sin((2*np.pi*1.05)/(i+0.05))
y.append(y_1)
plt.plot(t,y)
plt.xlabel("t")
plt.ylabel("y")
plt.title("TimeSine")
plt.show()
1、首先打開python的編輯器軟件,編輯器的選擇可以根據(jù)自己的喜好,之后準(zhǔn)備好一個空白的python文件:
2、接著在空白的python文件上編寫python程序,這里假設(shè)當(dāng)x>1的時候,方程為根號下x加4,當(dāng)x-1時,方程為5乘以x的平方加3。所以在程序的開始需要引入math庫,方便計算平方和開方,之后在函數(shù)體重寫好表達(dá)式就可以了,最后調(diào)用一下函數(shù),將結(jié)果打印出來:
3、最后點擊軟件內(nèi)的綠色箭頭,運行程序,在下方可以看到最終計算的結(jié)果,以上就是python求分段函數(shù)的過程:
了解下什么是函數(shù)哈
你可以直接寫
def f(x):
if x 5:
return x
if 1 x = 5:
return x + 1
if -3 x = 1:
return 0.5 * x + 1
return x - 1
# 以下為輸入和調(diào)用
x = int(inpit())
res = f(x)
print(res)
幾個繪圖的例子,來自API手冊:
1、最簡單的圖:
代碼:
[python] view plain copy print?
#!/usr/bin/env python
import matplotlib.pyplot as plt
plt.plot([10, 20, 30])
plt.xlabel('tiems')
plt.ylabel('numbers')
plt.show()
import CV2
import copy
import numpy as np
import random
使用的是pycharm
因為最近看了《銀翼殺手2049》,里面Joi實在是太好看了所以原圖像就用Joi了
要求是灰度圖像,所以第一步先把圖像轉(zhuǎn)化成灰度圖像
# 讀入原始圖像
img = CV2.imread('joi.jpg')
# 灰度化處理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
第一個任務(wù)是利用分段函數(shù)增強灰度對比,我自己隨便寫了個函數(shù)大致是這樣的
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 = copy.deepcopy(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.copy()
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)
在實現(xiàn)濾波之前先添加高斯噪聲和椒鹽噪聲(代碼來源于網(wǎng)絡(luò))
不知道這個椒鹽噪聲的名字是誰起的感覺隔壁小孩都饞哭了
用到了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個像素點灰度值的平均值代替中心點,而中值就是中位數(shù)代替中心點,邊界點周圍補0;前兩個函數(shù)的作用是算出這個點的灰度值,后兩個是對整張圖片進(jìn)行
#均值濾波模板
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 = copy.deepcopy(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 = copy.deepcopy(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 = copy.deepcopy(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)
分段函數(shù)的代碼用python實現(xiàn)如下:
x=eval(input('輸入x的值:'))
if x!=0:
y=1/(2*x-1)
else:
y=0
print(y)