python去噪算法_Python实现图像去噪方式(中值去噪和均值去噪)
实现对图像进行简单的高斯去噪和椒盐去噪。
代码如下:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import random
import scipy.misc
import scipy.signal
import scipy.ndimage
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10)
def medium_filter(im, 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(im, 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 convert_2d(r):
n = 3
# 3*3 滤波器, 每个系数都是 1/9
window = np.ones((n, n)) / n ** 2
# 使用滤波器卷积图像
# mode = same 表示输出尺寸等于输入尺寸
# boundary 表示采用对称边界条件处理图像边缘
s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm')
return s.astype(np.uint8)
def convert_3d(r):
s_dsplit = []
for d in range(r.shape[2]):
rr = r[:, :, d]
ss = convert_2d(rr)
s_dsplit.append(ss)
s = np.dstack(s_dsplit)
return s
def add_salt_noise(img):
rows, cols, dims = img.shape
R = np.mat(img[:, :, 0])
G = np.mat(img[:, :, 1])
B = np.mat(img[:, :, 2])
Grey_sp = R * 0.299 + G * 0.587 + B * 0.114
Grey_gs = R * 0.299 + G * 0.587 + B * 0.114
snr = 0.9
noise_num = int((1 - snr) * rows * cols)
for i in range(noise_num):
rand_x = random.randint(0, rows - 1)
rand_y = random.randint(0, cols - 1)
if random.randint(0, 1) == 0:
Grey_sp[rand_x, rand_y] = 0
else:
Grey_sp[rand_x, rand_y] = 255
#给图像加入高斯噪声
Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape)
Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs))
Grey_gs = Grey_gs * 255 / np.max(Grey_gs)
Grey_gs = Grey_gs.astype(np.uint8)
# 中值滤波
Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (7, 7))
Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))
# 均值滤波
Grey_sp_me = convert_2d(Grey_sp)
Grey_gs_me = convert_2d(Grey_gs)
plt.subplot(321)
plt.title('加入椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp, cmap='gray')
plt.subplot(322)
plt.title('加入高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs, cmap='gray')
plt.subplot(323)
plt.title('中值滤波去椒盐噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_sp_mf, cmap='gray')
plt.subplot(324)
plt.title('中值滤波去高斯噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_gs_mf, cmap='gray')
plt.subplot(325)
plt.title('均值滤波去椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp_me, cmap='gray')
plt.subplot(326)
plt.title('均值滤波去高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs_me, cmap='gray')
plt.show()
def main():
img = np.array(Image.open('E:/pycharm/GraduationDesign/Test/testthree.png'))
add_salt_noise(img)
if __name__ == '__main__':
main()
效果如下
以上这篇Python实现图像去噪方式(中值去噪和均值去噪)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
时间: 2019-12-18
python去噪算法_Python实现图像去噪方式(中值去噪和均值去噪)相关推荐
- python中值滤波算法_Python扩展库scipy中值滤波算法的应用
中值滤波是数字信号处理.数字图像处理中常用的预处理技术,特点是将信号中每个值都替换为其邻域内的中值,即邻域内所有值排序后中间位置上的值.下面的代码演示了scipy库中signal模块的中值滤波算法的用 ...
- 【图像去噪】中值滤波和均值滤波matlab源码
一.均值滤波 图片中一个方块区域(一般为3*3)内,中心点的像素为全部点像素值的平均值.均值滤波就是对于整张图片进行以上操作. 我们可以看下图的矩阵进行理解 缺陷:均值滤波本身存在着固有的缺陷,即它不 ...
- Python,OpenCV中的非局部均值去噪(Non-Local Means Denoising)
Python,OpenCV中的非局部均值去噪(Non-Local Means Denoising) 1. 效果图 2. 原理 3. 源码 2.1 单彩色图去噪 2.2 多连续彩色帧去噪 参考 这篇博客 ...
- python设计---空域增强之图片去噪(中值滤波、均值滤波、高斯滤波、双边滤波)
空域增强之图片去噪 引言 中值滤波及均值滤波 中值滤波 理论方法 均值滤波 中值滤波和均值滤波的对比 程序 高斯滤波 程序 双边滤波 程序 ui界面 设计程序 后续 引言 我们在日常工作生活中,总是需 ...
- python中值滤波去除反光_Python 实现中值滤波、均值滤波的方法
红包: Lena椒盐噪声图片: # -*- coding: utf-8 -*- """ Created on Sat Oct 14 22:16:47 2017 @auth ...
- python高斯滤波和降噪_python添加高斯噪声和椒盐噪声,实现中值滤波和均值滤波,实现Roberts算子和Sobel算子...
写在前面 HIT大三上学期视听觉信号处理课程中视觉部分的实验一,经过和学长们实验的对比发现每一级实验要求都不一样,因此这里标明了是2019年秋季学期的视觉实验一. 由于时间紧张,代码没有进行任何优化, ...
- python中值滤波介绍_Python 实现中值滤波、均值滤波的方法
红包: Lena椒盐噪声图片: # -*- coding: utf-8 -*- """ Created on Sat Oct 14 22:16:47 2017 @auth ...
- 【Matlab图像去噪】中值+均值+Lee+Kuan图像滤波【含源码 1179期】
一.代码运行视频(哔哩哔哩) [Matlab图像去噪]中值+均值+Lee+Kuan图像滤波[含源码 1179期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] ...
- 大数据算法课程笔记1:寻找中值算法之随机选取,中值的中值,One Pass算法
大数据算法的课程笔记,包括四种中值搜索算法.包括最简单的先搜索后检索,平均算法复杂度为O(N)的简单随机选取算法,确定性的中值的中值算法,大概率返回中值的one pass算法. 1. 最简单的方法:先 ...
最新文章
- 有了这款可视化工具,Java 应用性能分析、调优 so easy...
- K-means聚类算法和模糊C-means聚类算法
- MySQL Proxy实现数据库读写分离
- [LeetCode题解] ZigZag Conversion
- 转帖:关于MongoDB你需要知道的几件事
- php获取一维,二维数组长度的方法(有实例)
- tip for interview
- 操作系统复习笔记 02-03 OS Structure 操作系统结构
- jmeter 生成计数器_使用密码摘要生成器扩展JMeter
- vue 动态显示三级路由
- 可以生成自动文档的注释
- 哪些年给我们留下记忆的IT站点
- vscode-icons插件使用
- S5PV210体系结构与接口08:定时器 计数器编程
- 洛谷P3941入阵曲
- Python编程利用单纯形法和scipy库对比分析求解线性规划最大值和最优解问题
- 对于三极管饱和状态的理解
- win7科学计算机的用法,win7系统自带的计算器使用的修复步骤
- Few-Shot Video Object Detection
- Coursera 机器学习 第9章(下) Recommender Systems 学习笔记