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,中值,滤波,均值
0
投稿
猜你喜欢
一文带你了解Go语言实现的并发神库conc
2024-05-13 10:43:22
python分批定量读取文件内容,输出到不同文件中的方法
2021-10-05 21:29:30
如何使用python docx模块操作word文档
2022-04-23 12:16:02
PHP session有效期问题
2023-11-23 20:49:10
简单介绍利用TK在Python下进行GUI编程的教程
2023-01-05 21:24:25
Python设计模式之模板方法模式实例详解
2023-07-21 01:29:19
Python产生一个数值范围内的不重复的随机数的实现方法
2023-09-19 21:48:09
使用shell检查并修复mysql数据库表的脚本
2024-01-27 23:52:35
JavaScript实现父子dom同时绑定两个点击事件,一个用捕获,一个用冒泡时执行顺序的方法
2024-05-02 17:28:51
SQL Server利用sp_spaceused如何查看表记录存在不准确的情况
2024-01-20 07:40:10
DRF之请求与响应的实现
2023-04-06 03:37:09
详述numpy中的np.random.random()系列函数用法
2023-08-08 00:25:09
python使用SMTP发送qq或sina邮件
2021-03-08 01:06:55
Python基本数据类型详细介绍
2021-10-14 07:02:50
Python 带有参数的装饰器实例代码详解
2023-03-22 22:57:44
一文搞懂JSON(JavaScript Object Notation)
2024-04-10 10:58:22
Python使用文件锁实现进程间同步功能【基于fcntl模块】
2022-07-04 17:45:15
js用typeof方法判断undefined类型
2024-04-19 09:53:43
Python股票数据可视化代码详解
2021-11-29 15:48:47
Python Pyperclip模块安装和使用详解
2023-06-25 13:52:11