python分段函数 python分段函数代码( 二 )


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个像素点灰度值的平均值代替中心点,而中值就是中位数代替中心点,边界点周围补0python分段函数;前两个函数的作用是算出这个点的灰度值,后两个是对整张图片进行
#均值滤波模板
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)+1img.shape[0]
or m-int(step/2)0 or m+int(step/2)+1img.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)+1img.shape[0]
or m-int(step/2)0 or m+int(step/2)+1img.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代码如下python分段函数:
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编写一个求分段函数的值的程序1、首先打开pythonpython分段函数的编辑器软件,编辑器的选择可以根据自己的喜好,之后准备好一个空白的python文件:
2、接着在空白的python文件上编写python程序,这里假设当x>1的时候,方程为根号下x加4,当x-1时,方程为5乘以x的平方加3 。所以在程序的开始需要引入math库,方便计算平方和开方,之后在函数体重写好表达式就可以python分段函数了,最后调用一下函数,将结果打印出来:

推荐阅读