Python 实现中值滤波、均值滤波的方法

时间:2021-05-22

红包:

Lena椒盐噪声图片:

# -*- coding: utf-8 -*-"""Created on Sat Oct 14 22:16:47 2017@author: Don"""from tkinter import *from skimage import ioimport numpy as npim=io.imread('lena_sp.jpg', as_grey=True)im_copy_med = io.imread('lena_sp.jpg', as_grey=True)im_copy_mea = io.imread('lena_sp.jpg', as_grey=True)#io.imshow(im)for i in range(0,im.shape[0]): for j in range(0,im.shape[1]): im_copy_med[i][j]=im[i][j] im_copy_mea[i][j]=im[i][j]#uiroot = Tk()root.title("lena")root.geometry('300x200')medL = Label(root, text="中值滤波:")medL.pack()med_text = StringVar()med = Entry(root, textvariable = med_text)med_text.set("")med.pack()meaL = Label(root, text="均值滤波:")meaL.pack()mea_text = StringVar()mea = Entry(root, textvariable = mea_text)mea_text.set("")mea.pack()def m_filter(x, y, step): sum_s=[] for k in range(-int(step/2),int(step/2)+1): for m in range(-int(step/2),int(step/2)+1): sum_s.append(im[x+k][y+m]) sum_s.sort() return sum_s[(int(step*step/2)+1)]def mean_filter(x, y, step): sum_s = 0 for k in range(-int(step/2),int(step/2)+1): for m in range(-int(step/2),int(step/2)+1): sum_s += im[x+k][y+m] / (step*step) return sum_sdef on_click(): if(med_text): medStep = int(med_text.get()) for i in range(int(medStep/2),im.shape[0]-int(medStep/2)): for j in range(int(medStep/2),im.shape[1]-int(medStep/2)): im_copy_med[i][j] = m_filter(i, j, medStep) if(mea_text): meaStep = int(mea_text.get()) for i in range(int(meaStep/2),im.shape[0]-int(meaStep/2)): for j in range(int(meaStep/2),im.shape[1]-int(meaStep/2)): im_copy_mea[i][j] = mean_filter(i, j, meaStep) io.imshow(im_copy_med) io.imsave(str(medStep) + 'med.jpg', im_copy_med) io.imshow(im_copy_mea) io.imsave(str(meaStep) + 'mea.jpg', im_copy_mea)Button(root, text="filterGo", command = on_click).pack()root.mainloop()

运行结果截图:

以上这篇Python 实现中值滤波、均值滤波的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章