大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
一、函数说明
创新互联是专业的阳曲网站建设公司,阳曲接单;提供成都做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行阳曲网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
在使用python作图时,应用最广的就是matplotlib包,但我们平时使用matplotlib时主要是画一些简单的图表,很少有涉及分段函数。本次针对数值实验中两个较为复杂的函数,使用其构建分段函数图像。
二、图像代码
2.11、函数公式:
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、运行结果如下:
81036331d721706ae12808beb99b9574.png
2.21、函数公式:
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的编辑器软件,编辑器的选择可以根据自己的喜好,之后准备好一个空白的python文件:
2、接着在空白的python文件上编写python程序,这里假设当x>1的时候,方程为根号下x加4,当x-1时,方程为5乘以x的平方加3。所以在程序的开始需要引入math库,方便计算平方和开方,之后在函数体重写好表达式就可以了,最后调用一下函数,将结果打印出来:
3、最后点击软件内的绿色箭头,运行程序,在下方可以看到最终计算的结果,以上就是python求分段函数的过程:
了解下什么是函数哈
你可以直接写
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
# 以下为输入和调用
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了
要求是灰度图像,所以第一步先把图像转化成灰度图像
# 读入原始图像
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 = 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)
在实现滤波之前先添加高斯噪声和椒盐噪声(代码来源于网络)
不知道这个椒盐噪声的名字是谁起的感觉隔壁小孩都馋哭了
用到了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 = 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)
分段函数的代码用python实现如下:
x=eval(input('输入x的值:'))
if x!=0:
y=1/(2*x-1)
else:
y=0
print(y)