python求分段函数 python函数和方法区别
自定义函数实现根据年龄推荐合理的睡眠时间。 Python?
没有什么合理的睡眠时间,参数都是死的,你能让电脑知道你今天想吃什么吗?这些都是靠大数据推算出来的。如果是作业的话也没这么难
创新互联网络公司拥有10多年的成都网站开发建设经验,上1000家客户的共同信赖。提供网站设计制作、成都网站制作、网站开发、网站定制、外链、建网站、网站搭建、响应式网站建设、网页设计师打造企业风格,提供周到的售前咨询和贴心的售后服务
def sleep(age):
if age10:
return '8个小时'
else:
return '6个小时'
要多少睡眠时间都是由编写者决定的
python初学求解答
a = int (raw_input('totally second is:'))
hour = a/3600
minutes = (a%3600)/60
sc = a - hour - minutes #hour有3600秒,minutes有60秒,hour和minute应转换成秒计算
print hour,minutes,sc
s = int ( raw_input('totally second is:') )
h = s/3600
m = ( s - h * 3600 ) / 60
ss = s - h * 3600 - m * 60
print str ( h )+' '+str ( m )+' 'str ( ss ) # ' 'str(ss)最后的单引号和str(ss)中间应有个加号,没加号肯定出错,不可能不出错
Python简单问题?
阶梯型的计算规则。
根据算法200度以下是一个算法,[200,500)是一个算法,[500,∞)是另一种算法。但是都是使用函数e_check(n)来计算的。
所以是带入不同月份的电量的度数来计算的。
书中题目解析的部分应该是出版校验错误了,300度和600度写错了,根据多的那一项来看,应该是第三个月600度。
用python怎么求元角分
符号积分:通过integrate功能facility,SymPy对基本和特殊函数定与不定积分有卓越的支持,该功能使用有力的扩展Risch-Norman算法,启发算法和模式匹配,这样就可以求出元角分来。以下是具体方法,输入以下指令from sympy import integrate, exp, sin, log, oo, pi,symbols,然后再通过x, y = symbols('x,y')#定义符号变量x,y,再输入元角分的指令后通过integrate6*x**5, x以及integrate(log(x), x就可以求出。
引入刚刚的数学符号库from sympy import *定义一个符号变量x = symbols('x') 现在求x在区间[1,2]的定积分。
获取最小值
if x y,smaller = y
else,smaller = xfor i in range(1,smaller + 1),if((x % i == 0) and (y % i == 0),hcf = i
return hcf
# 用户输入两个数字num1 = int(input("输入第一个数字: "))num2 = int(input("输入第二个数字,这样就完成了求元角分的方法了。
数字图像处理Python实现图像灰度变换、直方图均衡、均值滤波
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求分段函数 python函数和方法区别
网站路径:http://azwzsj.com/article/dossige.html