中值滤波1

import numpy as np

import cv2

from PIL import Image

import scipy.signal as signal

import matplotlib.pyplot as plt

# 创建一个500*500的矩阵

input_images = np.zeros((500, 500))

filename = "E:/pycharm/GraduationDesign/Test/testtwo.png"

# convert将当前图像转换为灰度模式,并且返回新的图像。

# 将图片在重新定义的矩阵中再显示,不然可能会只显示部分。

img = Image.open(filename).resize((500, 500)).convert('L')

plt.subplot(221)

plt.title('原图', fontproperties=font_set)

plt.imshow(img)

# 图像的尺寸,按照像素数计算。它的返回值为宽度和高度的二元组(width, height)。

width = img.size[0]

height = img.size[1]

threshold = 130

# 可以改写代码使其成为二值化,此代码可理解为反向二值化

for h in range(height):

for w in range(width):

# getpixel直接获得(h,w)处的像素直接返回这个点三个通道的像素值

# 返回给定位置的像素值。如果图像为多通道,则返回一个元组(r,g,b,阈值)。

# 如果改成(w,h)出现的图像会倒转

if img.getpixel((w, h)) < threshold:

input_images[h, w] = 1

else:

input_images[h, w] = 0

plt.subplot(222)

plt.title('二值化', fontproperties=font_set)

plt.imshow(input_images)

data = signal.medfilt2d(np.array(img), kernel_size=3) # 二维中值滤波

for h in range(0, height):

for w in range(0, width):

if data[h][w] < 128:

input_images[h, w] = 0

else:

input_images[h, w] = 1

plt.subplot(223)

plt.title('中值滤波去噪(3*3)', fontproperties=font_set)

plt.imshow(input_images)

data = signal.medfilt2d(np.array(img), kernel_size=7) # 二维中值滤波

for h in range(0, height):

for w in range(0, width):

if data[h][w] < 128:

input_images[h, w] = 0

else:

input_images[h, w] = 1

plt.subplot(224)

plt.title('中值滤波去噪(7*7)', fontproperties=font_set)

plt.imshow(input_images)

plt.show()

中值滤波2

from PIL import Image

import numpy as np

def MedianFilter(src, dst, k=3, padding=None):

imarray = np.array(Image.open(src))

height, width = imarray.shape

if not padding:

edge = int((k - 1) / 2)

if height - 1 - edge <= edge or width - 1 - edge <= edge:

print("The parameter k is to large.")

return None

new_arr = np.zeros((height, width), dtype="uint8")

for i in range(height):

for j in range(width):

if i <= edge - 1 or i >= height - 1 - edge or j <= edge - 1 or j >= height - edge - 1:

new_arr[i, j] = imarray[i, j]

else:

# nm:neighbour matrix

nm = imarray[i - edge:i + edge + 1, j - edge:j + edge + 1]

max = np.max(nm)

min = np.min(nm)

if imarray[i, j] == max or imarray[i, j] == min:

new_arr[i, j] = np.median(nm)

else:

new_arr[i, j] = imarray[i, j]

new_im = Image.fromarray(new_arr)

new_im.save(dst)

src = "E:/正课/大二上/计算机网络/网络编程/图像去噪声/output2/4.jpg"

dst = "E:/正课/大二上/计算机网络/网络编程/图像去噪声/output2/xxx.jpg"

MedianFilter(src, dst)

python中值滤波器_python中值滤波相关推荐

  1. (MATLAB/C/Python)快速中值滤波

    (MATLAB/C/Python)快速中值滤波 一.中值滤波 二.快速中值滤波 介绍 原理 优化 三.代码 MATLAB C Python 四.测试 其他 by HPC_ZY 最近一个项目中需要用到中 ...

  2. python中值滤波去除椒盐噪声_python 中值滤波,椒盐去噪,图片增强实例

    受光照.气候.成像设备等因素的影响,灰度化后的图像存在噪声和模糊干扰,直接影响到下一步的文字识别,因此,需要对图像进行增强处理.图片预处理中重要一环就是椒盐去澡,通常用到中值滤波器进行处理,效果很好. ...

  3. 基于python+openCV的中值滤波

    先直接来一个3×3的吧 def median(src_img, filter_size, channels):# 首先,定义一个大小为9的0数组# list = [[0, 0, 0, 0, 0, 0, ...

  4. 数字图像处理——中值滤波及其改进算法

    一.算法介绍 中值滤波器是非线性滤波器的一个例子,它在保留图像特征方面非常有效. 但是,滤波器的窗口大小直接影响中值滤波器的性能. 较小的窗口保留了特征,但会导致噪声抑制的减少. 在较大窗口的情况下, ...

  5. matlab实现 中值滤波去除基线漂移,快速中值滤波在滤除心电信号基线漂移中的应用...

    [摘要]文中给出了一种非线性的滤除心电信号基线漂移的滤波方法,把基于排序统计理论的快速中值滤波方法应用于处理心电信号,通过多次对心电信号中选择的窗口数据进行排序,然后取中值的方法来达到滤波的效果.试验 ...

  6. python实现中值滤波_Python实现中值滤波去噪方式

    中值滤波器去噪: 中值滤波的主要原理是将数字图像中的某点用该点的邻域中各个像素值的中值所来代替,这样就能让目标像素周围能够更好的接近真实值,比如一张白纸上有一个黑点时,黑点的像素值比较大,经过中值滤波 ...

  7. python中值滤波去除反光_Python 实现中值滤波、均值滤波的方法

    红包: Lena椒盐噪声图片: # -*- coding: utf-8 -*- """ Created on Sat Oct 14 22:16:47 2017 @auth ...

  8. python中值滤波介绍_Python 实现中值滤波、均值滤波的方法

    红包: Lena椒盐噪声图片: # -*- coding: utf-8 -*- """ Created on Sat Oct 14 22:16:47 2017 @auth ...

  9. python高斯滤波和降噪_python添加高斯噪声和椒盐噪声,实现中值滤波和均值滤波,实现Roberts算子和Sobel算子...

    写在前面 HIT大三上学期视听觉信号处理课程中视觉部分的实验一,经过和学长们实验的对比发现每一级实验要求都不一样,因此这里标明了是2019年秋季学期的视觉实验一. 由于时间紧张,代码没有进行任何优化, ...

最新文章

  1. IDEA实用插件和技巧
  2. chrome插件infinity_5款超好用Chrome插件,快试试看!
  3. Msfvenonm生成后门
  4. C#对window 硬件类操作,ManagementObjectSearcher
  5. Hadoop的安装与配置——搭建完全分布式集群
  6. 中国长租公寓市场白皮书
  7. emlog程序音乐歌曲网源码
  8. macos远程桌面连接_如何在macOS中使用Microsoft远程桌面连接Amazon EC2
  9. leetcode力扣75. 颜色分类
  10. Netty源码解读(一)概述
  11. BS 百度Blog的字节限制!!!!!!!!!
  12. Windows 系统环境变量大全
  13. Python程序员都会喜欢的6个库
  14. SharePoint读取和设置列表栏的内容
  15. 一、HTML和CSS基础--HTML+CSS基础课程--第2部分
  16. java poi 只能创建?,Java POI使用SS模型创建新的工作簿?
  17. 韩语在线翻译图片识别_3个OCR文字识别工具,最后一个许多人都不知道!
  18. svn clean up 失败
  19. 点击计算机 不支持此接口
  20. JAVA缴税_根据工资和税率计算应缴税(java)

热门文章

  1. 2020 KALI 设置壁纸
  2. 强化学习大牛Sergey Levine:将RL作为可扩展自监督学习的基础
  3. Linux系统SPI驱动总结
  4. 【IVI】CarService启动
  5. GitHub推出开发者赚钱新利器,100%全给开发者!
  6. 为嵌入式linux添加网络功能并用tftp传输文件(以ZYNQ为例)
  7. paas平台即服务_平台即服务
  8. linux换行符 r,\r \n 回车换行符详解
  9. C# BeginInvoke实现异步编程
  10. “金三银四”春招旺季,HR如何快速抢人才?