使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)


Posted in Python onMay 28, 2019

在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声。Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现。

代码:

import numpy as np
import random
import cv2

def sp_noise(image,prob):
  '''
  添加椒盐噪声
  prob:噪声比例 
  '''
  output = np.zeros(image.shape,np.uint8)
  thres = 1 - prob 
  for i in range(image.shape[0]):
    for j in range(image.shape[1]):
      rdn = random.random()
      if rdn < prob:
        output[i][j] = 0
      elif rdn > thres:
        output[i][j] = 255
      else:
        output[i][j] = image[i][j]
  return output


def gasuss_noise(image, mean=0, var=0.001):
  ''' 
    添加高斯噪声
    mean : 均值 
    var : 方差
  '''
  image = np.array(image/255, dtype=float)
  noise = np.random.normal(mean, var ** 0.5, image.shape)
  out = image + noise
  if out.min() < 0:
    low_clip = -1.
  else:
    low_clip = 0.
  out = np.clip(out, low_clip, 1.0)
  out = np.uint8(out*255)
  #cv.imshow("gasuss", out)
  return out

可见,只要我们得到满足某个分布的多维数组,就能作为噪声添加到图片中。

例如:

import cv2
import numpy as np

>>> im = np.empty((5,5), np.uint8) # needs preallocated input image
>>> im
array([[248, 168, 58,  2,  1], # uninitialized memory counts as random, too ? fun ;) 
    [ 0, 100,  2,  0, 101],
    [ 0,  0, 106,  2,  0],
    [131,  2,  0, 90,  3],
    [ 0, 100,  1,  0, 83]], dtype=uint8)
>>> im = np.zeros((5,5), np.uint8) # seriously now.
>>> im
array([[0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]], dtype=uint8)
>>> cv2.randn(im,(0),(99))     # normal
array([[ 0, 76,  0, 129,  0],
    [ 0,  0,  0, 188, 27],
    [ 0, 152,  0,  0,  0],
    [ 0,  0, 134, 79,  0],
    [ 0, 181, 36, 128,  0]], dtype=uint8)
>>> cv2.randu(im,(0),(99))     # uniform
array([[19, 53, 2, 86, 82],
    [86, 73, 40, 64, 78],
    [34, 20, 62, 80, 7],
    [24, 92, 37, 60, 72],
    [40, 12, 27, 33, 18]], dtype=uint8)

然后再:

img = ...
noise = ...

image = img + noise

参考链接:

1、https://stackoverflow.com/questions/22937589/how-to-add-noise-gaussian-salt-and-pepper-etc-to-image-in-python-with-opencv#

2、https://stackoverflow.com/questions/14435632/impulse-gaussian-and-salt-and-pepper-noise-with-opencv#

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之有容乃大的list(4)
Sep 28 Python
Python 中 list 的各项操作技巧
Apr 13 Python
Python数据分析之如何利用pandas查询数据示例代码
Sep 01 Python
Python实现翻转数组功能示例
Jan 12 Python
用TensorFlow实现lasso回归和岭回归算法的示例
May 02 Python
python实现月食效果实例代码
Jun 18 Python
django项目登录中使用图片验证码的实现方法
Aug 15 Python
Python实现手机号自动判断男女性别(实例解析)
Dec 22 Python
6行Python代码实现进度条效果(Progress、tqdm、alive-progress​​​​​​​和PySimpleGUI库)
Jan 06 Python
Python如何通过百度翻译API实现翻译功能
Apr 02 Python
Python实现自动打开电脑应用的示例代码
Apr 17 Python
Python 的 f-string 可以连接字符串与数字的原因解析
Feb 20 Python
Python学习笔记之变量、自定义函数用法示例
May 28 #Python
Python分布式进程中你会遇到的问题解析
May 28 #Python
Python增强赋值和共享引用注意事项小结
May 28 #Python
Django框架用户注销功能实现方法分析
May 28 #Python
Django框架首页和登录页分离操作示例
May 28 #Python
Django框架封装外部函数示例
May 28 #Python
详解Numpy数组转置的三种方法T、transpose、swapaxes
May 27 #Python
You might like
php根据命令行参数生成配置文件详解
2019/03/15 PHP
3Z版基于jquery的图片复选框(asp.net+jquery)
2010/04/12 Javascript
jquery、js操作checkbox全选反选
2014/03/12 Javascript
jQuery及JS实现循环中暂停的方法
2015/02/02 Javascript
用JavaScript获取页面文档内容的实现代码
2016/06/10 Javascript
jQuery实现为LI列表前3行设置样式的方法【2种方法】
2016/09/04 Javascript
Angularjs 实现一个幻灯片示例代码
2016/09/08 Javascript
使用jquery实现的循环连续可停顿滚动实例
2016/11/23 Javascript
Bootstrap基本插件学习笔记之Alert警告框(20)
2016/12/08 Javascript
JavaScript基础之AJAX简单的小demo
2017/01/29 Javascript
vue mounted组件的使用
2018/06/18 Javascript
关于AngularJS中ng-repeat不更新视图的解决方法
2018/09/30 Javascript
vue props对象validator自定义函数实例
2019/11/13 Javascript
Ant-design-vue Table组件customRow属性的使用说明
2020/10/28 Javascript
在elementui中Notification组件添加点击事件实例
2020/11/11 Javascript
[01:12]快闪回顾DOTA2亚洲邀请赛(DAC) 静候2018新征程开启
2018/03/11 DOTA
Python统计列表中的重复项出现的次数的方法
2014/08/18 Python
python修改注册表终止360进程实例
2014/10/13 Python
python实现字典(dict)和字符串(string)的相互转换方法
2017/03/01 Python
PyQt5实现无边框窗口的标题拖动和窗口缩放
2018/04/19 Python
python使用循环打印所有三位数水仙花数的实例
2018/11/13 Python
Python 带有参数的装饰器实例代码详解
2018/12/06 Python
Python多项式回归的实现方法
2019/03/11 Python
Python搭建代理IP池实现接口设置与整体调度
2019/10/27 Python
如何使用Python多线程测试并发漏洞
2019/12/18 Python
python 比较2张图片的相似度的方法示例
2019/12/18 Python
Python根据字符串调用函数过程解析
2020/11/05 Python
详解html2canvas截图不能截取圆角图片的解决方案
2018/01/30 HTML / CSS
TUMI马来西亚官方网站:国际领先的高品质商旅箱包品牌
2018/04/26 全球购物
Hoka One One法国官网:美国专业跑鞋品牌
2018/12/29 全球购物
优秀学生获奖感言
2014/02/15 职场文书
2014年大学生党员自我评议
2014/09/22 职场文书
乡镇三严三实学习心得体会
2014/10/13 职场文书
校运会通讯稿
2015/07/18 职场文书
坚持不是死撑,更重要的是心态
2019/08/19 职场文书
MySQL系列之十 MySQL事务隔离实现并发控制
2021/07/02 MySQL