Python 实现中值滤波、均值滤波的方法
作者:bllddee 时间:2022-05-09 08:58:38
红包:
Lena椒盐噪声图片:
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 14 22:16:47 2017
@author: Don
"""
from tkinter import *
from skimage import io
import numpy as np
im=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]
#ui
root = 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_s
def 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()
运行结果截图:
来源:https://blog.csdn.net/Dooonald/article/details/78260299
标签:Python,中值,滤波,均值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Python当中的字符串和编码
2021-05-26 07:07:39
![](https://img.aspxhome.com/file/2023/8/101488_0s.png)
Python动刷新抢12306火车票的代码(附源码)
2021-04-27 08:13:24
![](https://img.aspxhome.com/file/2023/2/105272_0s.jpg)
简单了解python列表和元组的区别
2022-02-11 17:14:43
![](https://img.aspxhome.com/file/2023/4/65494_0s.png)
(小技巧)复制消息对话框的内容
2008-10-21 10:51:00
python遍历类中所有成员的方法
2022-11-16 22:29:00
Python OpenCV识别行人入口进出人数统计
2023-09-18 13:08:06
![](https://img.aspxhome.com/file/2023/7/85077_0s.jpg)
ASP实现上传图片自动压缩图片大小
2010-01-25 12:47:00
FrontPage XP设计制作网页小技巧八则
2008-06-04 12:43:00
ASP字符串16进制互转
2008-06-24 12:28:00
如何对PHP程序中的常见漏洞进行攻击
2023-11-14 19:46:25
asp使用 sql_dmo 给表添加索引
2010-03-17 20:57:00
Python编程基础之输入与输出
2021-03-03 10:06:07
![](https://img.aspxhome.com/file/2023/3/68883_0s.png)
利用Psyco提升Python运行速度
2021-05-02 19:02:50
Python嵌入C/C++进行开发详解
2024-01-02 06:39:48
Python 数据结构之树的概念详解
2021-06-24 17:08:25
![](https://img.aspxhome.com/file/2023/8/78798_0s.png)
Windows下Python使用Pandas模块操作Excel文件的教程
2021-01-05 23:07:40
![](https://img.aspxhome.com/file/2023/1/69021_0s.jpg)
浅谈在django中使用filter()(即对QuerySet操作)时踩的坑
2022-05-03 10:24:49
![](https://img.aspxhome.com/file/2023/6/101736_0s.jpg)
简评:JavaScript将成Silverlight的最大对手?
2008-10-17 10:29:00
PHP同时连接多个mysql数据库示例代码
2023-11-23 21:12:28
Python树莓派学习笔记之UDP传输视频帧操作详解
2023-04-21 10:03:20